vite-jest
vite-jest copied to clipboard
Jest is not defined in examples/react-app-ts
When i try to run examples/react-app-ts
and reference the global jest
in the tests an exception is thrown:
$ vite-jest
(node:54191) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FAIL src/App.test.tsx
● Test suite failed to run
ReferenceError: jest is not defined
4 | import App from './App';
5 |
> 6 | const fn = jest.fn()
| ^
7 |
8 | test('renders learn react link', () => {
9 | render(<App />);
at src/App.test.tsx:6:12
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:387:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 4.05 s
Ran all test suites.
<somePath>/vite-jest/examples/react-app-ts/node_modules/execa/lib/error.js:60
error = new Error(message);
^
Error: Command failed with exit code 1: node --experimental-vm-modules <somePath>/vite-jest/examples/react-app-ts/node_modules/jest/bin/jest.js
at makeError (<somePath>vite-jest/examples/react-app-ts/node_modules/execa/lib/error.js:60:11)
at Function.module.exports.sync (<somePath>/vite-jest/examples/react-app-ts/node_modules/execa/index.js:194:17)
at file://<somePath>/vite-jest/examples/react-app-ts/node_modules/vite-jest/bin/vite-jest.js:11:7
at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
at async Loader.import (internal/modules/esm/loader.js:177:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5) {
shortMessage: 'Command failed with exit code 1: node --experimental-vm-modules<somePath>/vite-jest/examples/react-app-ts/node_modules/jest/bin/jest.js',
command: 'node --experimental-vm-modules <somePath>/vite-jest/examples/react-app-ts/node_modules/jest/bin/jest.js',
escapedCommand: 'node --experimental-vm-modules "<somePath>/vite-jest/examples/react-app-ts/node_modules/jest/bin/jest.js"',
exitCode: 1,
signal: undefined,
signalDescription: undefined,
stdout: undefined,
stderr: undefined,
failed: true,
timedOut: false,
isCanceled: false,
killed: false
}
error Command failed with exit code 1.
I had a similar problem. I was using npm install
. I deleted package-lock.json
, node_modules
and reinstalled with yarn
and it worked for me.
I have a similar issue, can't do jest.mock. Doing global.jest = jest
in setupTests makes it available but jest.mock won't actually mock the methods.
This works for me in jest.setup.js
/* global jest */
global.jest = jest;
Adding global.jest = jest to jest.setup.js ends up introducing weird bugs for me. :(