Git Product home page Git Product logo

jest-in-case's Introduction

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);

cases 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': { ... },
});

jest-in-case's People

Contributors

haroenv avatar jamiebuilds avatar sudo-suhas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.