Jest In Case: Parameterized Tests
jest-in-case is a utility for the Jest testing framework that simplifies the creation of parameterized tests. It allows developers to define a single test function (`tester`) and supply multiple sets of input data (`testCases`) as either an array of objects or an object whose keys serve as test names. This dramatically reduces boilerplate when testing a function or component with numerous input variations. The package helps organize tests by automatically generating named test cases using Jest's `test` function, and supports `test.only` and `test.skip` via special `only` and `skip` properties within test cases. The latest version, 1.0.2, was published on September 8, 2017, making it an effectively abandoned project. This means it has not been updated in over nine years and is likely incompatible with recent major versions of Jest (e.g., Jest 30) and modern Node.js environments. There is no active release cadence, and it lacks contemporary features like native TypeScript support.
Common errors
-
TypeError: cases is not a function
cause Attempting to use `jest-in-case` with incorrect import syntax (e.g., named import for a default export) or in an environment where module resolution fails due to its age.fixEnsure `cases` is imported as a default export: `import cases from 'jest-in-case';` for ESM or `const cases = require('jest-in-case');` for CommonJS. Also, verify that `jest-in-case` is compatible with your project's module system and Jest version. -
Error: expect is not defined
cause This error can occur if Jest is not correctly configured or if a very old version of Jest is used, where `expect` might not be globally available or set up within the test environment when `jest-in-case` runs its callback.fixEnsure Jest is properly installed (`npm install --save-dev jest`), configured, and that `jest-in-case` is running within a standard Jest test file. Given the package's age, this might indicate fundamental incompatibility with your Jest setup.
Warnings
- breaking This package is effectively abandoned, with its last release (1.0.2) in September 2017. It is highly unlikely to be compatible with modern versions of Jest (e.g., Jest 30), Node.js, or contemporary module systems (ESM). Using it may lead to critical failures or unexpected behavior in newer environments.
- gotcha The `name`, `only`, and `skip` properties are reserved keywords within test case objects passed to `jest-in-case`. If you include these properties with custom values, they will be overridden or used by `jest-in-case` for test naming and conditional execution, potentially leading to unexpected test behavior or data loss.
- gotcha Due to its age, `jest-in-case` does not ship with TypeScript type definitions, nor does it officially support TypeScript. While it might be used in a TypeScript project with `@ts-ignore` or manually created declaration files, this is not officially supported and can lead to type safety issues.
Install
-
npm install jest-in-case -
yarn add jest-in-case -
pnpm add jest-in-case
Imports
- cases
import { cases } from 'jest-in-case';import cases from 'jest-in-case';
- cases
const { cases } = require('jest-in-case');const cases = require('jest-in-case');
Quickstart
/* file: math.js */
export function add(augend, addend) {
return augend + addend;
}
export function subtract(minuend, subtrahend) {
return minuend - subtrahend;
}
/* file: math.test.js */
import cases from 'jest-in-case';
import { add, subtract } from './math';
describe('Math operations', () => {
cases('add(augend, addend) should return correct total', 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 },
]);
cases('subtract(minuend, subtrahend) should return correct difference', opts => {
expect(subtract(opts.minuend, opts.subtrahend)).toBe(opts.difference);
}, {
'5 - 2 = 3': { minuend: 5, subtrahend: 2, difference: 3 },
'10 - 7 = 3': { minuend: 10, subtrahend: 7, difference: 3 }
});
cases('async test example', async ({ input, expected }) => {
const result = await Promise.resolve(input * 2);
expect(result).toBe(expected);
}, [
{ name: 'doubles 5 to 10', input: 5, expected: 10 },
{ name: 'doubles 10 to 20', input: 10, expected: 20 }
]);
});