Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
albohlabs opened this issue Aug 5, 2021 · 6 comments
Open

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

albohlabs opened this issue Aug 5, 2021 · 6 comments

Comments

@albohlabs
Copy link

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
Copy link

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
Copy link

fadi-george commented Sep 7, 2021

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
Copy link

martpet commented Sep 12, 2021

This works for me in jest.setup.js

/* global jest */
global.jest = jest;

@rquast
Copy link

rquast commented Oct 18, 2021

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

@BuntyBru
Copy link

Facing the same :(

@morzel85
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants