Jest
Installation
npm i -D jest
More at Getting Started (jestjs.io).
Usage
jest <test-file|regex> [option]
# Use with npm scripts
npm <npm-script> -- [option]
// package.json e.g.:
{
"scripts": {
"test" : "jest [option]"
}
}
Options:
--bail <boolean|number>
(abort after the first fail or any given amount of fails)--coverage <boolean>
--config <config-file>
(jest.config.\<extension\>
by default)-f, --onlyFailures
(runs only previously failed tests)--verbose <boolean>
(false
by default ortrue
if there is a single test file)
More at Jest CLI Options - Reference (jestjs.io).
Configuration
Files named jest.config.<js|ts|mjs|cjs|json>
will be discovered automatically.
// jest.config.ts e.g.:
import type { Config } from 'jest';
const config: Config = {
bail: true,
coverage: true,
verbose: true,
// ...
};
export default config;
More at Configuration Jest - Reference (jestjs.io).
Using ESMs
1- Setup config file:
// jest.config.mjs e.g.:
export default {
testEnvironment: "jest-environment-node",
transform: {}, // Avoid transforming `import` statements.
};
2- Setup package file:
// package.json e.g.:
{
"scripts": {
"test": "NODE_OPTIONS='--experimental-vm-modules' jest"
},
"type": "module"
}
3- Setup test files:
// main.test.js e.g.:
import { jest } from '@jest/globals';
More at ECMAScript Modules (jestjs.io).