expect-more-jest
Write Beautiful Specs with Custom Matchers for Jest

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
The simplest way to integrate is to set the setupFilesAfterEnv
array of
Jest's jest.config.js to include require.resolve('expect-more-jest')
.
Note: If your Editor does not recognise that you are using custom matchers, add a global.d.ts
file
at the root of your project containing:
import 'expect-more-jest';
🧪 Generators
expect-more/gen
helps you assert how your application under test behaves when parts of its input data are missing or
null
.
import { withMissingNodes } from 'expect-more/gen';
it('should return null if any part of the API contract is broken', () => {
for (let brokenContract of withMissingNodes(contract)) {
expect(fn(brokenContract)).toBeNull();
}
});
🔬 Matchers
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([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(8).toBeNumber();
expect({}).toBeObject();
expect(5).toBeOddNumber();
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({}).toBeWalkable();
expect(' ').toBeWhitespace();
expect(8).toBeWholeNumber();
expect(7).toBeWithinRange(0, 10);
expect('JavaScript').toEndWith('Script');
expect({ child: { grandchild: [true, false, new Boolean(true)] } }).toHaveArrayOfBooleans(
'child.grandchild',
);
expect({ child: { grandchild: [12, 0, 14] } }).toHaveArrayOfNumbers('child.grandchild');
expect({ child: { grandchild: [{}, new Object()] } }).toHaveArrayOfObjects('child.grandchild');
expect({ child: { grandchild: ['i', 'contain', 4, 'items'] } }).toHaveArrayOfSize(
'child.grandchild',
4,
);
expect({ child: { grandchild: ['we', 'are', 'all', 'strings'] } }).toHaveArrayOfStrings(
'child.grandchild',
);
expect({ child: { grandchild: [2, true, 'string'] } }).toHaveArray('child.grandchild');
expect({
child: {
grandchild: async () => {
await fetch('...');
},
},
}).toHaveAsyncFunction('child.grandchild');
expect({ child: { grandchild: false } }).toHaveBoolean('child.grandchild');
expect({ child: { grandchild: '100' } }).toHaveCalculable('child.grandchild');
expect({ child: { grandchild: new Date('2020-01-01') } }).toHaveDateAfter(
'child.grandchild',
new Date('2019-12-31'),
);
expect({ child: { grandchild: new Date('2019-12-31') } }).toHaveDateBefore(
'child.grandchild',
new Date('2020-01-01'),
);
expect({ child: { grandchild: new Date('2019-12-31') } }).toHaveDate('child.grandchild');
expect({ child: { grandchild: 12.55 } }).toHaveDecimalNumber('child.grandchild');
expect({ child: { grandchild: 12 } }).toHaveDivisibleBy('child.grandchild', 2);
expect({ child: { grandchild: [] } }).toHaveEmptyArray('child.grandchild');
expect({ child: { grandchild: {} } }).toHaveEmptyObject('child.grandchild');
expect({ child: { grandchild: '' } }).toHaveEmptyString('child.grandchild');
expect({ child: { grandchild: 'JavaScript' } }).toHaveEndingWith('child.grandchild', 'Script');
expect({ child: { grandchild: 2 } }).toHaveEvenNumber('child.grandchild');
expect({ child: { grandchild: false } }).toHaveFalse('child.grandchild');
expect({
child: {
grandchild: function* gen() {
yield 'i am a generator';
},
},
}).toHaveGeneratorFunction('child.grandchild');
expect({ child: { grandchild: 10 } }).toHaveGreaterThanOrEqualTo('child.grandchild', 5);
expect({ child: { grandchild: '1999-12-31T23:59:59' } }).toHaveIso8601('child.grandchild');
expect({ child: { grandchild: '{"i":"am valid JSON"}' } }).toHaveJsonString('child.grandchild');
expect({ child: { grandchild: 8 } }).toHaveLessThanOrEqualTo('child.grandchild', 12);
expect({ child: { grandchild: ['i', 'have', 3] } }).toHaveLongerThan('child.grandchild', [
2,
'items',
]);
expect({ child: { grandchild: () => 'i am a function' } }).toHaveMethod('child.grandchild');
expect({ child: { grandchild: ['i', 'am not empty'] } }).toHaveNonEmptyArray('child.grandchild');
expect({ child: { grandchild: { i: 'am not empty' } } }).toHaveNonEmptyObject('child.grandchild');
expect({ child: { grandchild: 'i am not empty' } }).toHaveNonEmptyString('child.grandchild');
expect({ child: { grandchild: null } }).toHaveNull('child.grandchild');
expect({ child: { grandchild: 4.8 } }).toHaveNumberNear('child.grandchild', 5, 0.5);
expect({ child: { grandchild: 7 } }).toHaveNumberWithinRange('child.grandchild', 0, 10);
expect({ child: { grandchild: 8 } }).toHaveNumber('child.grandchild');
expect({ child: { grandchild: {} } }).toHaveObject('child.grandchild');
expect({ child: { grandchild: 5 } }).toHaveOddNumber('child.grandchild');
expect({ child: { grandchild: new RegExp('i am a regular expression') } }).toHaveRegExp(
'child.grandchild',
);
expect({ child: { grandchild: ['i also have', '2 items'] } }).toHaveSameLengthAs(
'child.grandchild',
['i have', '2 items'],
);
expect({ child: { grandchild: ['i have one item'] } }).toHaveShorterThan('child.grandchild', [
'i',
'have',
4,
'items',
]);
expect({ child: { grandchild: 'JavaScript' } }).toHaveStartingWith('child.grandchild', 'Java');
expect({ child: { grandchild: 'i am a string' } }).toHaveString('child.grandchild');
expect({ child: { grandchild: true } }).toHaveTrue('child.grandchild');
expect({ child: { grandchild: undefined } }).toHaveUndefined('child.grandchild');
expect({ child: { grandchild: new Date('2020-01-01') } }).toHaveValidDate('child.grandchild');
expect({ child: { grandchild: {} } }).toHaveWalkable('child.grandchild');
expect({ child: { grandchild: ' ' } }).toHaveWhitespace('child.grandchild');
expect({ child: { grandchild: 8 } }).toHaveWholeNumber('child.grandchild');
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.