Coder Social home page Coder Social logo

dhampik / jest-chain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sepehr/jest-chain

0.0 1.0 0.0 208 KB

Chain Jest matchers together to create one powerful assertion ๐Ÿƒโ›“

Home Page: https://www.npmjs.com/package/jest-chain

License: MIT License

JavaScript 100.00%

jest-chain's Introduction

jest-chain

๐Ÿƒโ›“

Chain Jest matchers together to create one powerful assertion


Build Status Code Coverage version downloads MIT License PRs Welcome Roadmap Examples

  • ๐Ÿธ Less code duplication
  • ๐Ÿค— Chain core and custom matchers together
  • ๐Ÿ‘พ Expressive assertions
  • ๐Ÿšจ Fail fast assertions

Problem

Often in Jest when you are writing tests you may want to perform multiple assertions on the same variable. Currently to achieve this you have to write an individual expect for each assertion.

For example:

it('add 1 and 1', () => {
  const actual = 1 + 1;
  expect(actual).toBe(2);
  expect(actual).toBeGreaterThan(1);
  expect(actual).toBeLessThan(3);
});

With jest-chain this can instead be written by chaining the matchers together:

it('add 1 and 1', () => {
  expect(1 + 1)
    .toBe(2)
    .toBeGreaterThan(1)
    .toBeLessThan(3);
});

Installation

With npm:

npm install --save-dev jest-chain

With yarn:

yarn add -D jest-chain

Setup

Add jest-chain to your Jest setupFilesAfterEnv configuration. See for help

Jest >v24

"jest": {
  "setupFilesAfterEnv": ["jest-chain"]
}

Jest <v23

"jest": {
  "setupTestFrameworkScriptFile": "jest-chain"
}

If you are already using another test framework, like jest-extended, then you should create a test setup file and require each of the frameworks you are using (including jest-chain ๐Ÿ˜‰)

For example:

// ./testSetup.js
require('jest-chain');
require('any other test framework libraries you are using');

Then in your Jest config:

"jest": {
  "setupTestFrameworkScriptFile": "./testSetup.js"
}

Typescript

If your editor does not recognise the chained jest matchers, add a global.d.ts file to your project with:

import 'jest-chain';

Note: if you are using any other custom matcher libraries then make sure that the jest-chain type import is at the bottom so that the types can chain core matchers with your customer matcher library.

Usage

Use Jest's expect function the same way you would normally but with the ability to chain any matcher to another, including nested matchers such as: .not, .resolves and .rejects.

jest-chain supports custom Jest matchers, like jest-extended, in the usual way with expect.extend(matcher). Each of these custom matchers are also chainable.

Some examples:

expect([1, 2, 3])
  .toHaveLength(3)
  .toEqual([1, 2, 3]);
// with jest-extended
expect([1, 2, 3])
  .toBeArray()
  .toBeArrayOfSize(3)
  .toEqual([1, 2, 3])
  .toIncludeAnyMembers([1, 2]);

expect(100)
  .toBePositive()
  .toBeGreaterThan(99)
  .toBeLessThan(101)
  .toBeNumber()
  .not.toBeNaN()
  .toBe(100);

expect('hello world')
  .toBeString()
  .toEqualCaseInsensitive('HELLO WORLD')
  .toStartWith('hello')
  .toEndWith('world')
  .not.toInclude('!')
  .toBe('hello world');

Matcher failures will fail fast from left to right, they have no impact on each other. ๐ŸŽ‰

Note: jest-chain does not currently support asymmetric matcher chaining, if you want this please send a PR ๐Ÿ˜Š

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.