sodatea / vite-jest

First-class Vite integration for Jest
MIT License
401 stars 51 forks source link

Jest is not defined in examples/react-app-ts #20

Open albohlabs opened 2 years ago

albohlabs commented 2 years ago

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.
smcenlly commented 2 years ago

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.

fadi-george commented 2 years ago

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.

martpet commented 2 years ago

This works for me in jest.setup.js

/* global jest */
global.jest = jest;
rquast commented 2 years ago

Adding global.jest = jest to jest.setup.js ends up introducing weird bugs for me. :(

BuntyBru commented 1 year ago

Facing the same :(

morzel85 commented 11 months ago

This worked for me: import { vi } from 'vitest'; and then replace of jest.fn() with vi.fn().