• Stars
    star
    226
  • Rank 176,514 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Jest runner that runs tests directly in bare Node.js, without virtualizing the environment.

jest-light-runner

A Jest runner that runs tests directly in bare Node.js, without virtualizing the environment.

Comparison with the default Jest runner

This approach is way faster than the default Jest runner (it more than doubled the speed of Babel's tests suite) and has complete support for the Node.js ESM implementation. However, it doesn't provide support for most of Jest's advanced features.

The lists below are not comprehensive: feel free to start a discussion regarding any other missing Jest feature!

Supported Jest features

  • Jest globals: expect, test, it, describe, beforeAll, afterAll, beforeEach, afterEach
  • Jest function mocks: jest.fn, jest.spyOn, jest.clearAllMocks, jest.resetAllMocks
  • Jest timer mocks: jest.useFakeTimers, jest.useRealTimers, jest.setSystemTime, jest.advanceTimersByTime
  • Inline and external snapshots
  • Jest cli options: --testNamePattern/-t, --maxWorkers, --runInBand
  • Jest config options: setupFiles, setupFilesAfterEnv, snapshotSerializers, maxWorkers, snapshotFormat, snapshotResolver

Unsupported Jest features

  • import/require mocks. You can use a custom mocking library such as esmock or proxyquire.
  • On-the-fly compilation (for example, with Babel or TypeScript). You can use a Node.js module loader, such as ts-node/esm.
  • Tests isolation. Jest runs every test file in its own global environment, meaning that modification to built-ins done in one test file don't affect other test files. This is not supported, but you can use the Node.js option --frozen-intrinsics to prevent such modifications.
  • import.meta.jest. You can use the jest global instead.

Partially supported features

  • process.chdir. This runner uses Node.js workers, that don't support process.chdir(). It provides a simple polyfill so that process.chdir() calls still affect the process.cwd() result, but they won't affect all the other Node.js API (such as fs.* or path.resolve).
  • Coverage reporting. This runner wires coverage data generated by babel-plugin-istanbul (Jest's default coverage provider). However, you have to manually run that plugin:
    • If you are already using Babel to compile your project and you are running Jest on the compiled files, you can add that plugin to your Babel configuration when compiling for the tests
    • If you are not using Babel yet, you can add a babel.config.json file to your project with these contents:
      {
        "plugins": ["babel-plugin-istanbul"]
      }
      and you can run Babel in Jest using a Node.js ESM loader such as babel-register-esm.

Usage

After installing jest and jest-light-runner, add it to your Jest config.

In package.json:

{
  "jest": {
    "runner": "jest-light-runner"
  }
}

or in jest.config.js:

module.exports = {
  runner: "jest-light-runner",
};

Using custom Node.js ESM loaders

You can specify custom ESM loaders using Node.js's --loader option. Jest's CLI doesn't allow providing Node.js-specific options, but you can do it by using the NODE_OPTIONS environment variable:

NODE_OPTIONS="--loader ts-node/esm" jest

Or, if you are using cross-env to be able to provide environment variables on multiple OSes:

cross-env NODE_OPTIONS="--loader ts-node/esm" jest

Don't run Node.js directly:

node --loader ts-node/esm ./node_modules/.bin/jest

This will result in ERR_UNKNOWN_FILE_EXTENSION, due to the loader argument not being passed to the sub-processes. This is a known limitation, and ts-node documentation recommends using NODE_OPTIONS.

Stability

This project follows semver, and it's currently in the 0.x release line.

It is used to run tests in the babel/babel and prettier/prettier repositories, but there are no internal tests for the runner itself. I would gladly accept a pull requests adding a test infrastructure!

Donations

If you use this package and it has helped with your tests, please consider sponsoring me on GitHub! You can also donate to Jest on their OpenCollective page.

More Repositories

1

sei.gay

Make people gay
CSS
32
star
2

yarn-plugin-babel-release-tool

TypeScript
31
star
3

yarn-plugin-conditions

This Yarn plugin adds support for the `condition:` protocol. It can be used for conditionally selecting a dependency version depending on a specific flag.
TypeScript
24
star
4

legacy-decorators-migration-utility

πŸ” A command line utility to upgrade your JavaScript files from the legacy decorators proposal to the new one.
JavaScript
21
star
5

conf-holyjs-moscow-2020

Material for HolyJS 2020 Moscow
TypeScript
11
star
6

brackets-svg-font

An Adobe Brackets extension to view SVG fonts
JavaScript
7
star
7

babel-preset-current-node-syntax

A Babel preset that enables parsing of proposals supported by the current Node.js version.
JavaScript
5
star
8

brackets-zip

JavaScript
5
star
9

for-own.macro

A babel-macro that makes for-in only visit own properties
JavaScript
5
star
10

vue-extract-loader

A Webpack loader which extracts a single block from Vue single-file components
JavaScript
5
star
11

yarn-plugin-jsr

A Yarn plugin to improve the developer experience when working with JSR.
TypeScript
5
star
12

vetur-custom-blocks-demo

TypeScript
4
star
13

import-attributes-ecosystem-support

Tracking support of import attributes across major tools in the ecosystem
JavaScript
4
star
14

brackets-jslint-errors

A Brackets extension
JavaScript
3
star
15

romajs-todo-app

JavaScript
2
star
16

scoped-css-imports

JavaScript
2
star
17

babel-synchronized

An experimental package to synchronusly run Babel when using ECMAScript modules for config or plugins, by moving Babel to a separate thread.
JavaScript
2
star
18

decimal-literal

A template tag to parse and evaluate an expression consisting of Decimal numbers and basic arithmetic operators
TypeScript
2
star
19

chokidar-2

A wrapper around chokidar@2 to be able to specify both @2 and @3 as dependencies
Shell
1
star
20

gi-to-git

Rewrite `gi txxx` and `gi xxx` commands to `git xxx`
TypeScript
1
star
21

ts-experiment-cts-mts-behavior

JavaScript
1
star
22

jest-bug-require-throws

JavaScript
1
star
23

import-defer-polyfill

JavaScript
1
star
24

conf-holyjs-moscow-2019

JavaScript
1
star
25

validate-cjs-module-lexer

Test that `cjs-module-lexer` correctly detects Babel's output.
JavaScript
1
star
26

es-module-evaluation

es-module-evaluation
JavaScript
1
star
27

babel-review-extension

A Chrome extension that makes it easier to review Babel PRs.
JavaScript
1
star
28

babel-bug-show-config-behavior

JavaScript
1
star
29

node-segfault-jest-dynamic-import

JavaScript
1
star
30

vscode-mf2

TypeScript
1
star
31

babel-native-esm-demo

JavaScript
1
star
32

test-gh-action-publish

JavaScript
1
star