📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

jest-test-matrix

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-test-matrix

A small utility for Jest to test all combinations of parameters for a function at once.

1.2.0
latest
npm
Version published
Weekly downloads
1.2K
4.11%
Maintainers
1
Weekly downloads
 
Created
Source

jest-test-matrix

A plugin for Jest which helps you test functions accepting different combinations of inputs. Instead of manually writing a test for each possible combination, you will only need to write one.

A quick example:

// file.test.js
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;
}

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

will result in the following snapshot:

// file.test.snap
exports[`should only allow valid payments 1`] = `
.-------------------------------------.
|               canPay                |
|-------------------------------------|
| balance | amount | status  | result |
|---------|--------|---------|--------|
|      50 |     20 | active  | true   |
|         |        | locked  | false  |
|         |        | expired | false  |
|         |    100 | active  | false  |
|         |        | locked  | false  |
|         |        | expired | false  |
'-------------------------------------'
`;

Installation

With npm

npm install -D jest-test-matrix

With yarn

yarn add -D jest-test-matrix

Usage

testMatrix(fn, input) where

  • fn: (params: Record<'string', any>) => any: The function you want to test. By default, all the parameters in input are passed as an object. If your function has a different arity, you can always pass an anonymous function wrapping your own function, like so testMatrix({a, b} => fn(a, b), {a: [...], b: [...]}). If your function has a name, it will be used as a title in the matrix snapshot.
  • input: Record<'string', any[]>: An object where keys are the names of the parameters you want to test (they will be used as column headers in the snapshot), and values are an array of different possible values for each parameter.

The call to testMatrix should we wrapped in a jest.expect call to generate a snapshot:

expect(
  testMatrix(canPay, {
    amount: [20, 100],
    balance : [50],
    status: ['active', 'locked', 'expired']
  })
).toMatchSnapshot();

Keywords

jest

FAQs

Package last updated on 29 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts