jest-each

Jest Parameterised Testing

version downloads MIT License

A parameterised testing library for Jest inspired by mocha-each.

jest-each allows you to provide multiple arguments to your test/describe which results in the test/suite being run once per row of parameters.

Features


Demo

Tests without jest-each

Current jest tests

Tests can be re-written with jest-each to:

.test

Current jest tests

.test with Tagged Template Literals

Current jest tests

.describe

Current jest tests

Installation

npm i --save-dev jest-each

yarn add -D jest-each

Importing

jest-each is a default export so it can be imported with whatever name you like.

js // es6 import each from 'jest-each';

js // es5 const each = require('jest-each').default;

Array of rows

API

each([parameters]).test(name, testFn)

each:
.test:

each([parameters]).describe(name, suiteFn)

each:
.describe:

Usage

.test(name, fn)

Alias: .it(name, fn)

js each([[1, 1, 2], [1, 2, 3], [2, 1, 3]]).test( 'returns the result of adding %d to %d', (a, b, expected) => { expect(a + b).toBe(expected); }, );

.test.only(name, fn)

Aliases: .it.only(name, fn) or .fit(name, fn)

js each([[1, 1, 2], [1, 2, 3], [2, 1, 3]]).test.only( 'returns the result of adding %d to %d', (a, b, expected) => { expect(a + b).toBe(expected); }, );

.test.skip(name, fn)

Aliases: .it.skip(name, fn) or .xit(name, fn) or .xtest(name, fn)

js each([[1, 1, 2], [1, 2, 3], [2, 1, 3]]).test.skip( 'returns the result of adding %d to %d', (a, b, expected) => { expect(a + b).toBe(expected); }, );

Asynchronous .test(name, fn(done))

Alias: .it(name, fn(done))

js each([['hello'], ['mr'], ['spy']]).test( 'gives 007 secret message: %s', (str, done) => { const asynchronousSpy = message => { expect(message).toBe(str); done(); }; callSomeAsynchronousFunction(asynchronousSpy)(str); }, );

.describe(name, fn)

``js each([[1, 1, 2], [1, 2, 3], [2, 1, 3]]).describe( '.add(%d, %d)', (a, b, expected) => { test(returns ${expected}`, () => { expect(a + b).toBe(expected); });

test('does not mutate first arg', () => {
  a + b;
  expect(a).toBe(a);
});

test('does not mutate second arg', () => {
  a + b;
  expect(b).toBe(b);
});

}, ); ```

.describe.only(name, fn)

Aliases: .fdescribe(name, fn)

js each([[1, 1, 2], [1, 2, 3], [2, 1, 3]]).describe.only( '.add(%d, %d)', (a, b, expected) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); }, );

.describe.skip(name, fn)

Aliases: .xdescribe(name, fn)

js each([[1, 1, 2], [1, 2, 3], [2, 1, 3]]).describe.skip( '.add(%d, %d)', (a, b, expected) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); }, );


Tagged Template Literal of rows

API

each[tagged template].test(name, suiteFn)

js each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} `.test('returns $expected when adding $a to $b', ({a, b, expected}) => { expect(a + b).toBe(expected); });

each takes a tagged template string with:
.test:

each[tagged template].describe(name, suiteFn)

``js each a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} .describe('$a + $b', ({a, b, expected}) => { test(returns ${expected}`, () => { expect(a + b).toBe(expected); });

test('does not mutate first arg', () => { a + b; expect(a).toBe(a); });

test('does not mutate second arg', () => { a + b; expect(b).toBe(b); }); }); ```

each takes a tagged template string with:
.describe:

Usage

.test(name, fn)

Alias: .it(name, fn)

js each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} `.test('returns $expected when adding $a to $b', ({a, b, expected}) => { expect(a + b).toBe(expected); });

.test.only(name, fn)

Aliases: .it.only(name, fn) or .fit(name, fn)

js each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} `.test.only('returns $expected when adding $a to $b', ({a, b, expected}) => { expect(a + b).toBe(expected); });

.test.skip(name, fn)

Aliases: .it.skip(name, fn) or .xit(name, fn) or .xtest(name, fn)

js each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} `.test.skip('returns $expected when adding $a to $b', ({a, b, expected}) => { expect(a + b).toBe(expected); });

Asynchronous .test(name, fn(done))

Alias: .it(name, fn(done))

js each` str ${'hello'} ${'mr'} ${'spy'} `.test('gives 007 secret message: $str', ({str}, done) => { const asynchronousSpy = message => { expect(message).toBe(str); done(); }; callSomeAsynchronousFunction(asynchronousSpy)(str); });

.describe(name, fn)

``js each a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} .describe('$a + $b', ({a, b, expected}) => { test(returns ${expected}`, () => { expect(a + b).toBe(expected); });

test('does not mutate first arg', () => { a + b; expect(a).toBe(a); });

test('does not mutate second arg', () => { a + b; expect(b).toBe(b); }); }); ```

.describe.only(name, fn)

Aliases: .fdescribe(name, fn)

js each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} `.describe.only('$a + $b', ({a, b, expected}) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); });

.describe.skip(name, fn)

Aliases: .xdescribe(name, fn)

js each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} `.describe.skip('$a + $b', ({a, b, expected}) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); });

License

MIT