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

Overview

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 wasy 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, beoreEach, afterEach
  • Jest function mocks: jest.fn, jest.spyOn
  • Inline and external snapshots
  • --testNamePattern/-t, to only run some specific tests

Unsupported Jest features

  • import/require mocks. You can use a custom mocking library such as esmock or proxyquire.
  • 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",
};

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 repository, but there are no 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

Comments
  • Typescript support

    Typescript support

    Feature request: would be awesome if it supported typescript compilation (no type-checking needed).

    Maybe something simple like esbuild transformer can be added for *.ts files, that'll cover 99% of the common needs.

    Awesome runner, by the way!

    opened by kirillgroshkov 15
  • Support `expect.addSnapshotSerializer()`

    Support `expect.addSnapshotSerializer()`

    Fixes #28

    ~I haven't test~, but seems this is how it works. https://github.com/facebook/jest/blob/bb0956589f9dbddcc13db0f42b2d4d9ff642b2cd/packages/jest-expect/src/index.ts#L35

    Tested, it works.

    opened by fisker 7
  • Respect `--maxWorkers`

    Respect `--maxWorkers`

    As suggested by Simen, it should be enough to read this.#config.maxWorkers in https://github.com/nicolo-ribaudo/jest-light-runner/blob/9fb6563376cea904bf6653e848c21c15d0a060d6/src/index.js#L10

    opened by nicolo-ribaudo 6
  • `expect.addSnapshotSerializer()` not working as expected

    `expect.addSnapshotSerializer()` not working as expected

    Seems the "serializer" is added globally, maybe because we are not running test files isolatedly?

    I haven't dug into the problem, I'll post what I found.

    opened by fisker 5
  • Add support for `jest.clearAllMocks` and `jest.resetAllMocks`

    Add support for `jest.clearAllMocks` and `jest.resetAllMocks`

    This PR aims to add support for jest.clearAllMocks and jest.resetAllMocks. Fixes #18

    • [x] Show proper error message instead of the existing error log TypeError: Cannot read properties of undefined (reading 'length'). Now shows TypeError: jest.clearAllMocks is not a function. Fixed in 14336c3d42e23705d77778e7a198ba12121a8905
    • [x] Support jest.clearAllMocks
    • [x] Support jest.resetAllMocks
    • [x] Tested

    Need a small help with last 2 tasks

    Thanks for hinting me where to change. I tried this, and I'm facing a small issue.

    I could have done like this in the global.setup.js:

    // excerpt
    globalThis.jest = {
      fn: mock.fn,
      spyOn: mock.spyOn,
      clearAllMocks: mock.JestMock.clearAllMocks.bind(mock.JestMock),
      // ...
    };
    

    But the jest-mock package seem not exporting the JestMock instance. So I can't do mock.JestMock.<thing>. https://github.com/facebook/jest/blob/d2f3dc694d72c0208f47fc2e2ca101e26a9a6d68/packages/jest-mock/src/index.ts#L1239-L1243

    Could someone help?

    opened by vajahath 4
  • Error locations are already converted from URLs to paths by Jest

    Error locations are already converted from URLs to paths by Jest

    Fixes https://github.com/babel/babel/pull/14867#issuecomment-1221608605

    @fisker Do you use inline snapshots in Prettier, and if yes did you ever had problems with the -u Jest options not working?

    opened by nicolo-ribaudo 3
  • Support

    Support "setupFilesAfterEnv"

    Custom matcher libraries like jest-extended suggest to load the extension by specifying setupFilesAfterEnv in the Jest configuration. This doesn't work with jest-light-runner, it is simply ignored so I have to load jest-extended in every test file manually. Maybe it would be possible to support the setupFilesAfterEnv configuration?

    opened by kayahr 3
  • incompatibility: wrong nested beforeEach execution order

    incompatibility: wrong nested beforeEach execution order

    The following test passes with jest but not jest-light-runner. The beforeAll behaviour appears to be correct, though.

    describe('beforeEach execution order', () => {
      const calls = []
      beforeEach(() => {
        calls.push('outer')
      })
    
      describe('nested', () => {
        beforeEach(() => {
          calls.push('inner')
        })
    
        it('expect outer beforeEach before inner', () => {
          expect(calls).toEqual(['outer', 'inner'])
        })
      })
    })
    
    bug 
    opened by mpareja 2
  • Support `snapshotFormat`

    Support `snapshotFormat`

    Prettier added this option in https://github.com/prettier/prettier/pull/12612 by @SimenB

    (Maybe Babel can also use it?)

    I've tested locally, it works.

    //cc @SimenB

    opened by fisker 2
  • Fix `addSnapshotSerializer`

    Fix `addSnapshotSerializer`

    When calling expect.addSnapshotSerializer in one test file, the serializer should not be used globally.

    Solution verified in https://github.com/prettier/prettier/pull/12859

    Fixes #35

    opened by fisker 1
  • Run `beforeEach`/`afterEach` in the correct order

    Run `beforeEach`/`afterEach` in the correct order

    This PR fixes issue #43, it selectively runs the "before" and the "after" hooks separately, before and after the test itself is executed. We've had this running for a while in a medium sized codebase, and it's worked well for us so far!

    opened by iainjreid 1
  • Jest aliases `xit` / `xdescribe` not implemented

    Jest aliases `xit` / `xdescribe` not implemented

    Jest has globally available aliases for test.skip (xit) and describe.skip (xdescribe).. With jest-light-runner, using these causes the test to fail with:

        ReferenceError: xit is not defined
    

    As an easy workaround, I used the non-aliased test.skip and describe.skip. I'm open to providing a PR for this issue

    opened by swcm-mnestler 0
  • Document shared modules between tests

    Document shared modules between tests

    Hi, thanks for this awesome package; it has sped up my tests 10x, so I'm very happy with it!!

    When I was first considering adopting jest-light-runner, though, one of the things that I most wanted to understand was exactly what type of test isolation I'd be giving up in exchange for the speed boost. I wasn't able to find much documentation on exactly how jest's test isolation works, so I mostly relied on the jest-light-runner README. After playing around with jest and this runner, though, I think this note in the readme is a bit incomplete:

    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.

    In particular, from my experiments (please correct me if I'm wrong), it seems like Jest not only evaluates each test file it its own node VM, but also gives each of those VMs a distinct ESM cache (to use when linking modules into the VM), so that mutable ESM exports are also isolated (and re-evaluated) between test files. If that is correct, then it seems important to note that in the README, because --frozen-intrinsics obviously won't flag/prevent mutating module exports.

    (As an aside, it might also be worth documenting that --frozen-intrinsics doesn't work with ts-node, I discovered, because the TS compiler sets some properties on the global Error object.)

    Anyway, like I said, I think this library is awesome! I'm just opening this issue to double check that my understanding is correct, and, if so, note the confusing bits that I ran into when adopting this library (which did break a few of my tests at first), so that hopefully they can be clarified for future users.

    opened by ethanresnick 0
  • docs(readme): use double quotes for Windows compat

    docs(readme): use double quotes for Windows compat

    According to the readme of cross-env:

    Pay special attention to the triple backslash (\\\) before the double quotes (") and the absence of single quotes ('). Both of these conditions have to be met in order to work both on Windows and UNIX.

    I’ve experienced this on a Window machine before and can confirm that ' doesn’t work within cmd.exe, while " does. The former breaks command execution with an error.

    As a result, attention is needed when adding a test script to package.json, as the double quote needs to be escaped in JSON:

    {
      "scripts": {
        "test": "cross-env NODE_OPTIONS=\"--loader ts-node/esm\" jest"
      },
    }
    
    opened by kripod 0
  • Add JEST_WORKER_ID to runner environment

    Add JEST_WORKER_ID to runner environment

    This environment variable is often used to detect whether app code runs inside Jest, it's good to have it for compatibilty (and to allow me to skip having to deal with module mocks).

    Edit: I just found out about NODE_ENV=test which already works with this runner, so I'll be using that. You decide whether you want this 😉.

    opened by silverwind 0
  • Unclear how to integrate in typescript projects

    Unclear how to integrate in typescript projects

    I'm trying to use jest-light-runner on a testing project, and can't really understand the process I'm supposed to be following, if there is support for such a setup.

    What I'm currently doing is adding the runner: "jest-light-runner" option to my jest config (which by itself will output errors for Unknown file extension ".ts").

    I understand this is due to on-the-fly compilation not being supported, as mentioned by the docs. It is mentioned that I should use something like ts-node/esm, which I set up with the flag --loader ts-node/esm as part of NODE_OPTIONS.

    Doing so is getting me random errors (from pino, our logger, not sure why). Is there anything else I should do? Does typescript support actually rely on this experimental node option, or is there anything else I'm missing?

    opened by kace91 1
Releases(v0.4.1)
  • v0.4.1(Oct 28, 2022)

    Bug Fixes

    • Fix snapshot test by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/60
    • Use @jest/expect in worker-runner by @JLHwung in https://github.com/nicolo-ribaudo/jest-light-runner/pull/61

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.4.0...v0.4.1

    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(Aug 27, 2022)

    Breaking changes

    • Update Jest to v29 by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/56
    • Drop Jest <27.5 support by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/57

    Bug Fixes

    • Error locations are already converted from URLs to paths by Jest by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/55

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.3.0...v0.4.0

    Source code(tar.gz)
    Source code(zip)
  • v0.3.0(Aug 8, 2022)

    New features

    • Add testPath to test state by @alecmev in https://github.com/nicolo-ribaudo/jest-light-runner/pull/51

    Bug fixes

    • fix: filter tests by @liuxingbaoyu in https://github.com/nicolo-ribaudo/jest-light-runner/pull/45
    • Fix addSnapshotSerializer by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/47

    New Contributors

    • @liuxingbaoyu made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/45
    • @alecmev made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/50

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.2.2...v0.3.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.2(Jul 1, 2022)

    New Features

    • Expose jestMock.restoreAllMocks from global setup by @janmeier in https://github.com/nicolo-ribaudo/jest-light-runner/pull/41

    Bug Fixes

    • Run beforeEach/afterEach in the correct order by @iainjreid in https://github.com/nicolo-ribaudo/jest-light-runner/pull/44
    • Reload imported modules in --watch mode by @cjroebuck in https://github.com/nicolo-ribaudo/jest-light-runner/pull/38

    New Contributors

    • @janmeier made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/41
    • @cjroebuck made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/38

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.2.1...v0.2.2

    Source code(tar.gz)
    Source code(zip)
  • v0.2.1(Apr 30, 2022)

    New features

    • Support expect.addSnapshotSerializer() by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/29
    • Support snapshotResolver option by @iainjreid in https://github.com/nicolo-ribaudo/jest-light-runner/pull/33

    New Contributors

    • @iainjreid made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/33

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.2.0...v0.2.1

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Apr 26, 2022)

    Breaking changes

    • setupFiles now runs once per global context (i.e. once per worker), rather than once per test file (https://github.com/nicolo-ribaudo/jest-light-runner/pull/21)

    New features

    • Support snapshotFormat by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/22
    • Add setupFilesAfterEnv by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/21
    • Update to Jest 28 by @JLHwung in https://github.com/nicolo-ribaudo/jest-light-runner/pull/24

    Bug fixes

    • Support Node.js v12 by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/23
    • Run setupFiles before the env setup by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/21

    Other

    • fix: link to repository in package.json by @SimenB in https://github.com/nicolo-ribaudo/jest-light-runner/pull/25
    • Make sure to test workers on CI by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/26

    New Contributors

    • @JLHwung made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/24

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.1.3...v0.2.0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.3(Apr 24, 2022)

  • v0.1.2(Apr 24, 2022)

    New features

    • Add support for jest.clearAllMocks and jest.resetAllMocks by @vajahath in https://github.com/nicolo-ribaudo/jest-light-runner/pull/19
    • Implement --runInBand support by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/20

    New Contributors

    • @SimenB made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/11
    • @vajahath made their first contribution in https://github.com/nicolo-ribaudo/jest-light-runner/pull/19

    Full Changelog: https://github.com/nicolo-ribaudo/jest-light-runner/compare/v0.1.1...v0.1.2

    Source code(tar.gz)
    Source code(zip)
  • v0.1.1(Apr 6, 2022)

    New features

    • Add support for setupFiles and snapshotSerializers by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/4
    • Respect --maxWorkers by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/10

    Internal changes

    • Add E2E test for Prettier by @fisker in https://github.com/nicolo-ribaudo/jest-light-runner/pull/8
    • Add Babel e2e test by @nicolo-ribaudo in https://github.com/nicolo-ribaudo/jest-light-runner/pull/9

    Thanks @piranna and @fisker for your first PRs!

    Source code(tar.gz)
    Source code(zip)
Owner
Nicolò Ribaudo
Math student and open source maintainer
Nicolò Ribaudo
Runs various integration tests for the composable picasso parachain.

Picasso Integration Tester Picasso Integration Tester is a collection of different implementation tests for the Picasso Polkadot Parachain. Installati

Dominik Roth 0 Jan 11, 2022
🥾 A simple way to do testing AWS Services and Jest or Serverless and Jest

jest-localstack-preset Install Install via yarn or npm $ yarn add https://github.com/thadeu/jest-localstack-preset.git or $ npm i https://github.com/t

thadeu 5 Oct 1, 2022
Example Serverless DynamoDB integration tests using Jest, TypeScript and the AWS CDK

serverless dynamodb integration tests ?? Example Serverless DynamoDB integration tests using Jest, TypeScript and the AWS CDK Introduction How to inte

Lee Gilmore 8 Nov 4, 2022
Debug your Jest tests. Effortlessly.🛠🖼

Jest Preview Debug your Jest tests. Effortlessly. ?? ?? Try Jest Preview Online. No downloads needed! Why jest-preview When writing tests using Jest,

Hung Viet Nguyen 2.1k Jan 4, 2023
⚙️ REST api with NodeJS(KoaJS) MongoDB and Jest Tests.

RESTRIS Functional Backend implementation of REST api with NodeJS(KoaJS) MongoDB and Jest Tests. Tools TypeScript Node KoaJS MongoDB Mongoose Jest Sup

Beatriz Oliveira 8 Dec 2, 2022
SAP Community Code Challenge: This repository contains an empty OpenUI5 application and end-to-end tests written with wdi5. Take part in the challenge and develop an app that passes the tests.

SAP Community Code Challenge - UI5 The change log describes notable changes in this package. Description This repository is the starting point for the

SAP Samples 8 Oct 24, 2022
A bare-bones example Shopify app build with remix.run

Remix Shopify App A bare-bones Shopify app build with Remix Not supported by or affiliated with Shopify Create .env Add API_KEY to .env Add API_SECRET

Willson Smith 27 Jan 5, 2023
💀 A bare-minimum solution to solve CORS problem via proxy API

?? CORS Hijacker A bare-minimum solution to solve CORS problem via proxy API Base URL https://cors-hijacker.vercel.app/ Get Get Basic HTML Endpoint: $

Irfan Maulana 35 Nov 4, 2022
mirrord lets you easily mirror traffic from your production environment to your development environment.

mirrord lets you easily mirror traffic from your Kubernetes cluster to your development environment. It comes as both Visual Studio Code extension and a CLI tool.

MetalBear 2.1k Dec 24, 2022
JSON Visio is data visualization tool for your json data which seamlessly illustrates your data on graphs without having to restructure anything, paste directly or import file.

JSON Visio is data visualization tool for your json data which seamlessly illustrates your data on graphs without having to restructure anything, paste directly or import file.

Aykut Saraç 20.6k Jan 4, 2023
🏆 2nd Runner Up 🏆 at Vividthata A Blend of ideas By NIT Hamirpur

Index Problem Statement Solution Proposed How it Works Challenges we faced Running Locally Demonstration Glimpse Problem Statement It is often the cas

null 3 Nov 22, 2022
A new zero-config test runner for the true minimalists

Why User-friendly - zero-config, no API to learn, simple conventions Extremely lighweight - only 40 lines of code and no dependencies Super fast - wit

null 680 Dec 20, 2022
A low-feature, dependency-free and performant test runner inspired by Rust and Deno

minitest A low-feature, dependency-free and performant test runner inspired by Rust and Deno Simplicity: Use the mt test runner with the test function

Sondre Aasemoen 4 Nov 12, 2022
A leetcode workspace template with test case runner for JavaScript/TypeScript programmers.

leetcode-typescript-workspace English | 简体中文 A vscode workspace template with test case runner script for JavaScript/TypeScript programmers using exte

null 10 Dec 13, 2022
A simple tap test runner that can be used by any javascript interpreter.

just-tap A simple tap test runner that can be used in any client/server javascript app. Installation npm install --save-dev just-tap Usage import cre

Mark Wylde 58 Nov 7, 2022
A simple Node.js code to get unlimited instagram public pictures by every user without api, without credentials.

Instagram Without APIs Instagram Scraping in August 2022, no credentials required This is a Node.js library, are you looking for the same in PHP? go t

Francesco Orsi 28 Dec 29, 2022
This document introduces an early implementation of the Node-RED runtime that runs on resource-constrained microcontrollers (MCUs).

Node-RED MCU Edition Copyright 2022, Moddable Tech, Inc. All rights reserved. Peter Hoddie Updated June 25, 2022 Introduction This document introduces

Peter Hoddie 53 Jan 3, 2023
Quickly bootstrap your next TypeScript REST API project. Node 16+, auto OpenAPI, Prettier+ESLint, Jest

REST API template with autogenerated OpenAPI Quickly bootstrap your next TypeScript REST API project with the most up to date template. Included a sam

null 6 Oct 1, 2022