npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-chain

v1.1.6

Published

Chain Jest matchers together to create one powerful assertion

Downloads

120,688

Readme

🃏⛓

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