🍃 Leaf

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:

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).