Jest Test Matrix

1.2.0 · active · verified Tue Apr 21

jest-test-matrix is a utility for Jest that streamlines the process of testing functions across all possible combinations of input parameters. Instead of manually writing individual test cases for each scenario, it enables developers to define a matrix of inputs, and the utility automatically generates and executes tests for every permutation. The current stable version is 1.2.0, and as a specialized testing utility, it typically sees infrequent but stable updates. This package is particularly useful for verifying the behavior of functions with multiple arguments where comprehensive combinatorial testing is desired, significantly reducing boilerplate code. It integrates directly with Jest's snapshot testing, producing a well-formatted, tabular snapshot that clearly displays inputs and corresponding outputs for each tested combination, aiding in quick visual verification and regression detection. This approach simplifies test maintenance and ensures thorough coverage for complex input scenarios.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `testMatrix` to generate all combinations for `amount`, `balance`, and `status` parameters and verify the output using Jest's snapshot testing.

import testMatrix from 'jest-test-matrix';

interface CanPayOpts {
  amount: number;
  balance: number;
  status: 'active' | 'locked' | 'expired';
}

function canPay({amount, balance, status}: CanPayOpts){
  if (status === 'locked' || status == 'expired'){
    return false;
  }
  return balance > amount;
}

describe('Payment eligibility', () => {
  it('should only allow valid payments based on various conditions', () => {
    expect(
      testMatrix(canPay, {
        amount: [20, 100],
        balance : [50],
        status: ['active', 'locked', 'expired']
      })
    ).toMatchSnapshot();
  });
});

view raw JSON →