
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
@fast-check/jest
Advanced tools
@fast-check/jest is a package that integrates fast-check, a property-based testing library, with Jest, a popular JavaScript testing framework. It allows developers to write tests that automatically generate a wide range of inputs to ensure code behaves correctly under various conditions.
Property-based Testing
This feature allows you to define properties that your code should satisfy for a wide range of automatically generated inputs. The code sample demonstrates a simple property-based test that checks if the absolute value of any integer is always non-negative.
const { testProp, fc } = require('@fast-check/jest');
testProp('should always return a positive number', [fc.integer()], (n) => {
expect(Math.abs(n)).toBeGreaterThanOrEqual(0);
});
Custom Arbitraries
This feature allows you to create custom arbitraries, which are generators for your test inputs. The code sample shows how to create a custom arbitrary that generates tuples of integers and strings, and then tests a property using these tuples.
const { testProp, fc } = require('@fast-check/jest');
const customArbitrary = fc.tuple(fc.integer(), fc.string());
testProp('should handle custom arbitrary', [customArbitrary], ([num, str]) => {
expect(typeof num).toBe('number');
expect(typeof str).toBe('string');
});
jest-check is another package that integrates property-based testing with Jest. It is similar to @fast-check/jest in that it allows for property-based testing within Jest, but it is less popular and not as actively maintained as fast-check.
testcheck is a property-based testing library inspired by QuickCheck from Haskell. It can be used with Jest, but it requires more setup compared to @fast-check/jest, which is specifically designed to integrate seamlessly with Jest.
@fast-check/jest
Bring the power of property based testing framework fast-check
into Jest.
@fast-check/jest
simplifies the integration of fast-check
into Jest testing framework.
Install @fast-check/jest
:
npm install --save-dev @fast-check/jest
In order to work properly, @fast-check/jest
requires jest
to be installed.
We also highly recommend users to launch their tests using the --show-seed
option provided by Jest. It ensures Jest will always print the seed by itself (requires Jest ≥29.2.0).
jest --show-seed
import { test, fc } from '@fast-check/jest';
// for all a, b, c strings
// b is a substring of a + b + c
test.prop([fc.string(), fc.string(), fc.string()])('should detect the substring', (a, b, c) => {
return (a + b + c).includes(b);
});
// Or the exact same test but based on named parameters
test.prop({ a: fc.string(), b: fc.string(), c: fc.string() })('should detect the substring', ({ a, b, c }) => {
return (a + b + c).includes(b);
});
The it
and test
functions returned by @fast-check/jest
are just enriched versions of the ones coming from jest
itself. They both come with .prop
.
Please note that the properties accepted by @fast-check/jest
as input can either be synchronous or asynchronous (even just PromiseLike
instances). In other words, the predicate passed as the last argument can be asynchronous.
Remark: it
and test
have been introduced in 1.4.0. You have to refer to Deprecated API if you are using a version of @fast-check/jest
<1.4.0.
test
and it
If you want to forward custom parameters to fast-check
, test.prop
and its variants accept an optional fc.Parameters
(more).
@fast-check/jest
also comes with support for .only
, .skip
, .todo
and .concurrent
from jest
. It also accepts more complex ones such as .concurrent.failing
or .concurrent.only.failing
.
import { it, test, fc } from '@fast-check/jest';
// With custom `fc.Parameters`, here { seed: 4242 }
test.prop([fc.nat(), fc.nat()], { seed: 4242 })('should replay the test for the seed 4242', (a, b) => {
return a + b === b + a;
});
// With .skip
test.skip.prop([fc.string()])('should be skipped', (text) => {
return text.length === [...text].length;
});
// With it version
describe('with it', () => {
it.prop([fc.nat(), fc.nat()])('should run too', (a, b) => {
return a + b === b + a;
});
});
The following feature is experimental! When used it makes runners able to kill long running synchonous code. Meaning that it will make fast-check able to kill infinite loops blocking the main thread. So far, the feature does not fully support transformations performed via transform steps defined with jest.
The CommonJS approach would be:
const { init, fc } = require('@fast-check/jest/worker');
const { pathToFileURL } = require('node:url');
const { test, expect } = init(pathToFileURL(__filename));
// can also be passed options such as isolationLevel: init(pathToFileURL(__filename), {})
test.prop([fc.constant(null)])('should pass', (value) => {
expect(value).toBe(null);
});
The ES Modules approach would be:
import { init, fc } from '@fast-check/jest/worker';
const { test, expect } = await init(new URL(import.meta.url));
// can also be passed options such as isolationLevel: init(new URL(import.meta.url), {})
test.prop([fc.constant(null)])('should pass', (value) => {
expect(value).toBe(null);
});
⚠️ Do not forget to add the await
before init
for the ES Module version!
@fast-check/jest | jest | fast-check | node |
---|---|---|---|
^2.0.0 | >=26.5.0(1)(2) | ^3.0.0 | >=14.15.0(3) and <18, >=18.17.0 and <19(4), >=20 |
^1.0.0 | >=26.5.0(1)(2) | ^3.0.0 | >=14.15.0(3) and <18, >=18.17.0 and <19(4), >=20 |
jest
should be greater or equal than 26.5.0 if you are using commonjs
esm
build, you may need to enable experimental features of node, see herejest
, >=12.17.0 is the one for @fast-check/jest
FAQs
Property based testing for Jest based on fast-check
The npm package @fast-check/jest receives a total of 120,820 weekly downloads. As such, @fast-check/jest popularity was classified as popular.
We found that @fast-check/jest demonstrated a healthy version release cadence and project activity because the last version was released less than 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
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.