nicolo-ribaudo / jest-light-runner

A Jest runner that runs tests directly in bare Node.js, without virtualizing the environment.
MIT License
231 stars 24 forks source link

@jest/globals alternative #42

Open Jaid opened 2 years ago

Jaid commented 2 years ago

Switching to jest-light-runner with tests that use import {it, expect} from "@jest/globals" currently doesn't work. It's easily fixed by just removing this import and using those magically injected globals, but this way ESLint reports undefined variables and I lose my IDE's type features.

So could you either inject your own globals into from "@jest/globals" imports or expose your own globals, so I can use import {it, expect} from "jest-light-runner"?

Alx101 commented 1 year ago

How does jest-runtime do it today? Maybe it's possible to mock the globals import

I notice if I do not import directly from @jest/globals my jest.spyOn calls get incorrect types

SimenB commented 1 year ago

jest-runtime does it by intercepting the require (or import) call and returning a module with the correct API (what would be the globals for the currently running module). I'm not sure how this module would do it loaders or module mocks. If globals are enabled it's easy enough (just be a facade for them), but if globals are disabled this module needs to interact with the framework (circus) via https://github.com/facebook/jest/blob/41bf2300895a2c00d0525d21260f0a392819871f/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts#L37