jest-tc

Test cases for Jest with built-in TypeScript support.

Usage no npm install needed!

<script type="module">
  import jestTc from 'https://cdn.skypack.dev/jest-tc';
</script>

README

jest-cases

Test cases for Jest with built-in TypeScript support.

Getting Started

It's super easy to install and to use - you'll love it!

Install from NPM as a dev dependency:

npm i -D jest-tc

Include in your test files where you need it:

import cases from 'jest-tc'   // for ES6
const cases = require('jest-tc')  // for CommonJS

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

cases(
  title: string,
  tester: (args: TestCase) => void,
  testCases: TestCase[] | { [name: string]: TestCase }): void

TestCase

A test case is an object that has this structure:

  • name?: string - the name of the test case
  • skip?: boolean - modifier, when true skips the test case
  • only?: boolean - modifier, when true on any cases, only those are run
  • [key: string]: any - any extra args to be consumed by tester

tester: (args: TestCase) => void

For every test case, the tester function is run with the TestCase object as its first parameter.

cases('tester Example', args => {
  console.log(args) // logs { name: 'First', whatever: 'args', you: 'want' }, ...
}, [
  { name: 'First', whatever: 'args', you: 'want' },
  { name: 'Second', whatever: 'args', you: 'want' }
])

testCases: TestCase[]

testCases can either be an array of TestCase or an object whose keys are the names of the tests.

As an array, testCases: TestCase[], test cases can optionally have a name property which is defined as the array index when excluded:

cases('Array Example', args => {
  expect(args.value).toBe(args.expected)
}, [
  { name: 'First', value: 60, expected: 65 },   // "First" will fail
  { value: 55, expected: 55 },                  // "1" will pass
  { value: 40, expected: 42 }                   // "2" will fail
])

testCases: { [name: string]: TestCase }

Here's an example of the testCases param as an object:

cases('Object Example', args => {
  expect(args.value).toBe(args.expected)
}, {
  'First': { value: 60, expected: 65 },   // "First" will fail
  '1': { value: 55, expected: 55 },       // "1" will pass
  '2': { value: 40, expected: 42 }        // "2" will fail
}

Modifiers

Following are the available modifiers and examples for how to use them.

skip

Takes precedence over other modifiers.

cases('Only Example', args => {
  expect(args.value).toBe(args.expected)
}, [
  { ...args },              // Run
  { skip: true, ...args },    // Not Run
  { ...args }               // Run
]

only

cases('Only Example', args => {
  expect(args.value).toBe(args.expected)
}, [
  { ...args },                          // Not Run
  { only: true, ...args },              // Run
  { only: true, skip: true, ...args }   // Not Run
]

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE.md file for details