Create a test-case table for use with Jest's test.each tagged template literal
npm install jest-each-tableTake advantage of jest's test.each tagged template literal functionality
without needing to hardcode the test-cases as template literals.
``js
import createTestTable from 'jest-each-table';
const testcases = createTestTable(
[...Array(10)].map((_, i) => ({
inputs: { left: i, right: i },
output: i + i
}))
);
const testsuite = test.each(...testcases);
testsuite('$inputs.left + $inputs.right = $output', ({ inputs, output }) => {
expect(inputs.left + inputs.right).toBe(output);
});
`
Jest's test.each functionality can be used in two ways:
1. test.each (function):
This allows an Array of Arrays with the arguments that are passed intotest.each
the test fn for each row.
2. (tagged template literal):
This allows a table of test-cases to be defined as an ES2015 string template.
Unfortunately test.each (the function) uses printf formatting for creatingtest.each
the test title which has serious drawbacks compared to how the title is
formatted using (the tagged template literal) which supports$variable`.