Giter Club home page Giter Club logo

jest-light-runner's Introduction

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
  • Inline and external snapshots
  • Jest cli options: --testNamePattern/-t, --maxWorkers
  • Jest config options: setupFiles, snapshotSerializers, maxWorkers

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.

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

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 in two alternative ways:

  1. Run Jest by explicitly running Node.js:
    node --loader ts-node/esm ./node_modules/.bin/jest
    
  2. Use 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
    

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.

jest-light-runner's People

Contributors

nicolo-ribaudo avatar simenb avatar fisker avatar piranna avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.