Neverending Perpetual Motion
Wondering what’s next for npm?Check out our public roadmap! »

expect-more-jestTypeScript icon, indicating that this package has built-in type declarations

5.2.1 • Public • Published

expect-more-jest

Write Beautiful Specs with Custom Matchers for Jest

NPM version NPM downloads Build Status Maintainability Follow JamieMason on GitHub Follow fold_left on Twitter

Table of Contents

Overview

expect-more-jest is a huge library of test matchers for a range of common use-cases, to make tests easier to read and produce relevant and useful messages when they fail.

Avoid vague messages such as "expected false to be true" in favour of useful cues like "expected 3 to be even number", and avoid implementation noise such as expect(paws.length % 2 === 0).toEqual(true) in favour of simply stating that you expect(paws.length).toBeEvenNumber().

🌩 Installation

npm install expect-more-jest --save-dev

🕹 Configuration

Include one of the following at the top of your test file.

import 'expect-more-jest';
// or
require('expect-more-jest');

🔬 Matchers

describe('expect-more-jest', () => {
  it('makes your tests and output easier to read', () => {
    expect(new Date('2020-01-01')).toBeAfter(new Date('2019-12-31'));
    expect([true, false, new Boolean(true)]).toBeArrayOfBooleans();
    expect([12, 0, 14]).toBeArrayOfNumbers();
    expect([{}, new Object()]).toBeArrayOfObjects();
    expect(['i', 'contain', 4, 'items']).toBeArrayOfSize(4);
    expect(['we', 'are', 'all', 'strings']).toBeArrayOfStrings();
    expect([{ name: 'Guybrush' }, { name: 'Elaine' }]).toBeArrayOf({
      name: expect.toBeNonEmptyString(),
    });
    expect([2, true, 'string']).toBeArray();
    expect(async () => {
      await fetch('...');
    }).toBeAsyncFunction();
    expect(new Date('2019-12-31')).toBeBefore(new Date('2020-01-01'));
    expect(false).toBeBoolean();
    expect('100').toBeCalculable();
    expect(new Date('2019-12-31')).toBeDate();
    expect(12.55).toBeDecimalNumber();
    expect(12).toBeDivisibleBy(2);
    expect([]).toBeEmptyArray();
    expect({}).toBeEmptyObject();
    expect('').toBeEmptyString();
    expect(2).toBeEvenNumber();
    expect(false).toBeFalse();
    expect(() => 'i am a function').toBeFunction();
    expect(function* gen() {
      yield 'i am a generator';
    }).toBeGeneratorFunction();
    expect('1999-12-31T23:59:59').toBeIso8601();
    expect('{"i":"am valid JSON"}').toBeJsonString();
    expect(['i', 'have', 3]).toBeLongerThan([2, 'items']);
    expect(['i', 'am not empty']).toBeNonEmptyArray();
    expect({ i: 'am not empty' }).toBeNonEmptyObject();
    expect('i am not empty').toBeNonEmptyString();
    expect({ x: 12, y: 22 }).toBeNullableOf({
      x: expect.toBeNumber(),
      y: expect.toBeNumber(),
    });
    expect(null).toBeNullableOf({
      x: expect.toBeNumber(),
      y: expect.toBeNumber(),
    });
    expect(8).toBeNumber();
    expect({}).toBeObject();
    expect(5).toBeOddNumber();
    expect({ x: 12, y: 22 }).toBeOptionalOf({
      x: expect.toBeNumber(),
      y: expect.toBeNumber(),
    });
    expect(undefined).toBeOptionalOf({
      x: expect.toBeNumber(),
      y: expect.toBeNumber(),
    });
    expect(new RegExp('i am a regular expression')).toBeRegExp();
    expect(['i also have', '2 items']).toBeSameLengthAs(['i have', '2 items']);
    expect(['i have one item']).toBeShorterThan(['i', 'have', 4, 'items']);
    expect('i am a string').toBeString();
    expect(true).toBeTrue();
    expect(new Date('2020-01-01')).toBeValidDate();
    expect('i am visible').toBeVisibleString();
    expect({}).toBeWalkable();
    expect(' ').toBeWhitespace();
    expect(8).toBeWholeNumber();
    expect(7).toBeWithinRange(0, 10);
    expect('JavaScript').toEndWith('Script');
    expect('JavaScript').toStartWith('Java');
  });
});

🙋🏽‍♂️ Getting Help

Get help with issues by creating a Bug Report or discuss ideas by opening a Feature Request.

👀 Other Projects

If you find my Open Source projects useful, please share them ❤️

🤓 Author

I'm Jamie Mason from Leeds in England, I began Web Design and Development in 1999 and have been Contracting and offering Consultancy as Fold Left Ltd since 2012. Who I've worked with includes Sky Sports, Sky Bet, Sky Poker, The Premier League, William Hill, Shell, Betfair, and Football Clubs including Leeds United, Spurs, West Ham, Arsenal, and more.

Follow JamieMason on GitHub Follow fold_left on Twitter

Install

npm i expect-more-jest

DownloadsWeekly Downloads

5,308

Version

5.2.1

License

MIT

Unpacked Size

117 kB

Total Files

100

Last publish

Collaborators

  • avatar