• Stars
    star
    274
  • Rank 150,274 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Chain Jest matchers together to create one powerful assertion 🃏⛓

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"
}

Configure Typescript

Add the following entry to your tsconfig to enable Typescript support.

  "files": ["node_modules/jest-chain/types/index.d.ts"],

Example typescript project here

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

More Repositories

1

deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
JavaScript
1,006
star
2

jest-expect-message

Add custom message to Jest expects 🃏🗯
JavaScript
357
star
3

babel-plugin-console

Babel Plugin that adds useful build time console functions 🎮
JavaScript
295
star
4

babel-jest-assertions

Babel Plugin that adds safety to your tests by verifying assertions are actually ran 🃏⁉️
JavaScript
96
star
5

jest-each

A parameterised testing library for Jest. https://www.npmjs.com/package/jest-each 🏃
JavaScript
92
star
6

babel-plugin-gwt

Data Driven Testing babel plugin inspired by Groovy's Spock framework 🖖
JavaScript
14
star
7

react-point-break

React CSS media queries with breakpoint render props Component and Provider. 🔫
JavaScript
7
star
8

oss-box

Open source project boilerplate generator 📦
JavaScript
4
star
9

ts-prelude

TypeScript
3
star
10

io-ts-graphql-codegen

TypeScript
2
star
11

flow-data-structures

Basic data structures implements using Flowtype
JavaScript
2
star
12

redux-identity

A simple redux reducer that returns the given state
JavaScript
1
star
13

jest-workspace-projects

JavaScript
1
star
14

trait-contract

Utility to build a Trait (Interface) for a given Type and supply an implementation which is validated against the Trait contract
JavaScript
1
star
15

eslint-plugin-jest-extended

ESLint plugin for jest-extended matchers
JavaScript
1
star
16

data-structures

Groovy
1
star
17

reason-refs

C++
1
star
18

es6-prototype-boilerplate

Boilerplate setup for quick prototyping with es6 using budo to build and mocha for testing.
JavaScript
1
star
19

react-native-counter

JavaScript
1
star