GitXplorerGitXplorer
a

jest-in-case

public
1058 stars
19 forks
14 issues

Commits

List of commits on branch master.
Verified
78fb05fd0a021fb183d0c78f0d13bc65c5dbfae3

Merge pull request #7 from atlassian/dependabot/npm_and_yarn/sshpk-1.16.1

rrjatkins committed 5 years ago
Verified
8642056dd1f5964bcd93668fdc1e8577091b3e00

Merge pull request #9 from atlassian/dependabot/npm_and_yarn/diff-3.5.0

rrjatkins committed 5 years ago
Verified
4183c80fdb350e1ef6e7cf6b713a6266f6816734

Merge pull request #5 from atlassian/dependabot/npm_and_yarn/handlebars-4.5.1

rrjatkins committed 5 years ago
Verified
9e546d8ffe9462f0485d573af470a7a01a7c3f97

Merge pull request #6 from atlassian/dependabot/npm_and_yarn/lodash-4.17.15

rrjatkins committed 5 years ago
Verified
0029e4ce41266a6f3020bc62d50068113f83d00d

Bump sshpk from 1.13.1 to 1.16.1

ddependabot[bot] committed 5 years ago
Verified
02d5f0d6a63c6e521f3d058d01e0e5a11e5b1564

Merge pull request #8 from atlassian/dependabot/npm_and_yarn/merge-1.2.1

rrjatkins committed 5 years ago

README

The README file for this repository.

jest-in-case

Jest utility for creating variations of the same test

Example

import { add, subtract } from './math';
import cases from 'jest-in-case';

cases('add(augend, addend)', opts => {
  expect(add(opts.augend, opts.addend)).toBe(opts.total);
}, [
  { name: '1 + 1 = 2', augend: 1, addend: 1, total: 2 },
  { name: '2 + 1 = 3', augend: 2, addend: 1, total: 3 },
  { name: '3 + 1 = 4', augend: 3, addend: 1, total: 4 },
]);

Installation

yarn add --dev jest-in-case

Usage

In your Jest tests, import cases from jest-in-case.

import cases from 'jest-in-case';
// or
const cases = require('jest-in-case');

Then you can call cases with a title, a tester, and some testCases.

cases(title, tester, testCases);

testCases can either be an array of objects with a name property:

cases('add(augend, addend)', opts => {
  expect(add(opts.augend, opts.addend)).toBe(opts.total);
}, [
  { name: '1 + 1 = 2', augend: 1, addend: 1, total: 2 },
  { name: '2 + 1 = 3', augend: 2, addend: 1, total: 3 },
  { name: '3 + 1 = 4', augend: 3, addend: 1, total: 4 },
]);

Or an object of objects with the names as the keys:

cases('subtract(minuend, subtrahend)', opts => {
  expect(subtract(opts.minuend, opts.subtrahend)).toBe(opts.difference);
}, {
  '1 - 1 = 0': { minuend: 1, subtrahend: 1, difference: 0 },
  '2 - 1 = 1': { minuend: 2, subtrahend: 1, difference: 1 },
  '3 - 1 = 2': { minuend: 3, subtrahend: 1, difference: 2 },
});

Inside of a test case you can put whatever properties you want, except for name, only, or skip:

cases('title', fn, [
  { name: 'reserved 1', only: true, skip: true, whatever: 'you', want: 'here' },
  { name: 'reserved 2', only: true, skip: true, whatever: 'you', want: 'here' },
  { name: 'reserved 3', only: true, skip: true, whatever: 'you', want: 'here' },
]);
  • name is passed to test(name, fn) to become the name of your test
  • When only is set to true it will use Jest's test.only function
  • When skip is set to true it will use Jest's test.skip function

The tester function is called on each test case with your options:

cases('title', opts => {
  console.log('passed: ', opts);
}, {
  'test 1': { foo: 1 },
  'test 2': { bar: 2 },
  'test 3': { baz: 3 },
});

// passed: { foo: 1 }
// passed: { bar: 2 }
// passed: { baz: 3 }

Your tester function works just like functions passed to Jest's test function do (Just with a prepended argument):

cases('async functions', async opts => {
  let result = await somethingAsync(opts.input);
  expect(result).toEqual(opts.result);
}, {
  'test 1': { ... },
  'test 2': { ... },
});

cases('done callback', (opts, done) => {
  somethingAsync(opts.input, result => {
    expect(result).toEqual(result);
    done();
  });
}, {
  'test 1': { ... },
  'test 2': { ... },
});