Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
jest-each
Advanced tools
The jest-each npm package is an extension for Jest, a popular JavaScript testing framework. It allows you to run a single test multiple times with different sets of data, making it easier to test functions with various inputs and expected outputs.
Parameterized Tests
This feature allows you to run the same test with different sets of data. In this example, the test checks if the sum of two numbers equals the expected result for multiple sets of inputs.
const each = require('jest-each').default;
each([ [1, 1, 2], [1, 2, 3], [2, 1, 3] ]).test('adds %d and %d to equal %d', (a, b, expected) => {
expect(a + b).toBe(expected);
});
Table-Driven Tests
This feature allows you to define test cases in a tabular format, making it easier to read and manage multiple test cases. The example demonstrates how to use a table to test the addition of two numbers.
const each = require('jest-each').default;
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.test('adds $a and $b to equal $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser. It allows for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Mocha can be used with various assertion libraries, and while it doesn't have built-in support for parameterized tests like jest-each, it can achieve similar functionality with the help of additional libraries like 'mocha-each'.
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation. It supports running tests concurrently, which can lead to faster test execution. AVA supports parameterized tests through its 'test.each' method, similar to jest-each, making it a good alternative for those looking for a minimalistic and fast testing framework.
Tape is a minimalist JavaScript test runner that works in both Node.js and the browser. It does not have built-in support for parameterized tests, but you can achieve similar functionality by writing loops within your test cases. Tape is known for its simplicity and ease of use, making it a good choice for smaller projects or those who prefer a lightweight testing solution.
A parameterised testing library for Jest inspired by mocha-each.
jest-each allows you to provide multiple arguments to your test
which results in the test being run once per row of parameters.
.test
to runs multiple tests with parameterised data
.it
.test.skip
to skip the parameterised tests
.it.skip
.test.only
to only run the parameterised tests
.it.only
done
npm i --save-dev jest-each
yarn add -D jest-each
jest-each is a default export so it can be imported with whatever name you like.
// es6
import each from 'jest-each';
// es5
const each = require('jest-each');
each([parameters]).test(name, fn)
each
:Array
the arguments that are passed into the fn
.test
:String
the title of the test
, use %s
in the name string to positionally inject parameter values into the test titleFunction
the test logic, this is the function that will receive the parameters as function arguments.test(name, fn)
Alias: .it(name, fn)
import each from 'jest-each';
import add from './add';
each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
]).test('returns the result of adding %s to %s', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});
.test.only(name, fn)
Alias: .it.only(name, fn)
each([ [1, 1, 2] ]).test.only('returns the result of adding %s to %s', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});
.test.skip(name, fn)
Alias: .it.skip(name, fn)
each([ [1, 1, 2] ]).test.skip('returns the result of adding %s to %s', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});
.test(name, fn(done))
Alias: .it(name, fn(done))
each([
['hello'],
['mr'],
['spy'],
]).test('gives 007 secret message ', (str, done) => {
const asynchronousSpy = (message) => {
expect(message).toBe(str);
done();
};
callSomeAsynchronousFunction(asynchronousSpy)(str);
});
MIT
FAQs
Parameterised tests for Jest
The npm package jest-each receives a total of 23,425,636 weekly downloads. As such, jest-each popularity was classified as popular.
We found that jest-each demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers 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
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.