
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
jest-test-matrix
Advanced tools
A small utility for Jest to test all combinations of parameters for a function at once.
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 |
'-------------------------------------'
`;
With npm
npm install -D jest-test-matrix
With yarn
yarn add -D jest-test-matrix
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();
FAQs
A small utility for Jest to test all combinations of parameters for a function at once.
The npm package jest-test-matrix receives a total of 1,161 weekly downloads. As such, jest-test-matrix popularity was classified as popular.
We found that jest-test-matrix demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.