Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
jest-each-case
Advanced tools
This is a fork of the awesome jest-in-case package. I will mark stuff that is different from this repo.
Jest utility for creating variations of the same test
import { add, subtract } from './math';
import cases from 'jest-in-case';
cases('add(augend, addend)', opts => {
expect(add(opts.augend, opts.addend)).toBe(opts.total);
}, [
{ name: '1 + 1 = 2', augend: 1, addend: 1, total: 2 },
{ name: '2 + 1 = 3', augend: 2, addend: 1, total: 3 },
{ name: '3 + 1 = 4', augend: 3, addend: 1, total: 4 },
]);
yarn add --dev jest-in-case
In your Jest tests, import cases
from
jest-in-case
.
import cases from 'jest-in-case';
// or
const cases = require('jest-in-case');
Then you can call cases
with a title
, a tester
, and some testCases
.
cases(title, tester, testCases);
cases
can either be an array of objects with a name
property:
cases('add(augend, addend)', opts => {
expect(add(opts.augend, opts.addend)).toBe(opts.total);
}, [
{ name: '1 + 1 = 2', augend: 1, addend: 1, total: 2 },
{ name: '2 + 1 = 3', augend: 2, addend: 1, total: 3 },
{ name: '3 + 1 = 4', augend: 3, addend: 1, total: 4 },
]);
Or an object of objects with the names as the keys:
cases('subtract(minuend, subtrahend)', opts => {
expect(subtract(opts.minuend, opts.subtrahend)).toBe(opts.difference);
}, {
'1 - 1 = 0': { minuend: 1, subtrahend: 1, difference: 0 },
'2 - 1 = 1': { minuend: 2, subtrahend: 1, difference: 1 },
'3 - 1 = 2': { minuend: 3, subtrahend: 1, difference: 2 },
});
Inside of a test case you can put whatever properties you want, except for
name
, only
, or skip
:
cases('title', fn, [
{ name: 'reserved 1', only: true, skip: true, whatever: 'you', want: 'here' },
{ name: 'reserved 2', only: true, skip: true, whatever: 'you', want: 'here' },
{ name: 'reserved 3', only: true, skip: true, whatever: 'you', want: 'here' },
]);
name
is passed to test(name, fn)
to become the name of your testonly
is set to true
it will use Jest's test.only
functionskip
is set to true
it will use Jest's test.skip
functionThe tester
function is called on each test case with your options:
cases('title', opts => {
console.log('passed: ', opts);
}, {
'test 1': { foo: 1 },
'test 2': { bar: 2 },
'test 3': { baz: 3 },
});
// passed: { foo: 1 }
// passed: { bar: 2 }
// passed: { baz: 3 }
Your tester function works just like functions passed to Jest's test
function
do (Just with a prepended argument):
cases('async functions', async opts => {
let result = await somethingAsync(opts.input);
expect(result).toEqual(opts.result);
}, {
'test 1': { ... },
'test 2': { ... },
});
cases('done callback', (opts, done) => {
somethingAsync(opts.input, result => {
expect(result).toEqual(result);
done();
});
}, {
'test 1': { ... },
'test 2': { ... },
});
You can also omit the name property entirely and the index of the test case will be used for the name automatically:
cases('', opts => { ... }, {
{ foo: 1 },
{ bar: 2 }
});
If you want to just pipe in values you can do so directly:
cases('', value => {
console.log('Value:', value);
}, [1,'foo',undefined,null,() => 1]);
// Value: 1
// Value: foo
// Value: undefined
// Value: null
// Value: () => 1
FAQs
Jest utility for creating variations of the same test
The npm package jest-each-case receives a total of 0 weekly downloads. As such, jest-each-case popularity was classified as not popular.
We found that jest-each-case 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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.