Jest Theories

1.5.1 · maintenance · verified Tue Apr 21

jest-theories is a utility library for the Jest testing framework that enables data-driven test cases, inspired by concepts from XUnit and Jasmine Theories. It allows developers to write a single test function and execute it multiple times with varying inputs, known as 'theories.' This significantly reduces boilerplate and improves test maintainability by centralizing test logic while parameterizing data. The current stable version is 1.5.1, and its release cadence is typically driven by community contributions and specific feature or bug fix requirements, rather than a fixed schedule, indicating a maintenance-focused approach given its last publish date. A key differentiator is its use of `string-format` for flexible test naming, including the ability to use theory properties, `$idx` (index), and `$no` (number) within the test description string, or even provide a custom function for dynamic naming. It seamlessly integrates with Jest's `describe` and `test` structure, shipping with TypeScript types for enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `theoretically` with an array of objects to run a single test block with multiple data inputs, using property names for dynamic test descriptions.

import theoretically from 'jest-theories';

// Mock function for demonstration purposes
const NumberToLongString = (num: number): string => {
  if (num === 100) return 'One hundred';
  if (num === 1000) return 'One thousand';
  if (num === 10000) return 'Ten thousand';
  if (num === 100000) return 'One hundred thousand';
  return 'Unknown';
};

describe('NumberToLongString conversion', () => {
    const theories = [
        { input: 100, expected: 'One hundred' },
        { input: 1000, expected: 'One thousand' },
        { input: 10000, expected: 'Ten thousand' },
        { input: 100000, expected: 'One hundred thousand' }
    ];

    theoretically('the number {input} is correctly translated to string', theories, theory => {
        const output = NumberToLongString(theory.input);
        expect(output).toBe(theory.expected);
    });
});

view raw JSON →