vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.79k stars 26.95k forks source link

Mocks not working with `@jest/globals` and `next/jest` #43888

Open amannn opened 1 year ago

amannn commented 1 year ago

Verify canary release

Provide environment information

~/Projects/amannn/temp/jest-globals-bug main
❯ yarn next info
yarn run v1.22.18
$ /Users/jan/Projects/amannn/temp/jest-globals-bug/node_modules/.bin/next info
The module 'react' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install react'
The module 'react-dom' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install react-dom'

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
    Binaries:
      Node: 16.16.0
      npm: 8.11.0
      Yarn: 1.22.18
      pnpm: 7.1.7
    Relevant packages:
      next: 13.0.7-canary.3
      eslint-config-next: N/A
      react: N/A
      react-dom: N/A

✨  Done in 3.00s.

Which area(s) of Next.js are affected? (leave empty if unsure)

Jest (next/jest)

Link to the code that reproduces this issue

https://github.com/amannn/jest-globals-bug

To Reproduce

yarn install && yarn test

Describe the Bug

Jest mocks are not working when using next/jest and @jest/globals. They are ignored.

Possible workarounds:

Expected Behavior

Mocks should be applied.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

SamRoehrich commented 1 year ago

will this work for you? https://github.com/amannn/jest-globals-bug/pull/1

amannn commented 1 year ago

Hi @SamRoehrich and thanks for participating! In your PR you've deactivated next/jest. I also noticed that this is a workaround but it kind of defeats the purpose, because I'd like to use next/jest for the features it provides.

SamRoehrich commented 1 year ago

Oh, I did not think that I deactivated it. I was using these docs as reference on how to initialize it. https://github.com/vercel/next.js/blob/canary/examples/with-jest/jest.config.js

willisplummer commented 1 year ago

I'm running into this bug too. @amannn were you able to find a solution?

amannn commented 1 year ago

Nope, but haven't checked in a while!

izorg commented 1 year ago

Hi. Please read this topic https://github.com/swc-project/swc/discussions/5151. As I understand there are several ways to fix it:

  1. Do not use @jest/globals imports
  2. Switch to babel instead of swc
  3. Use jest_workaround and module.type commonjs
zzh8829 commented 5 days ago

For anyone facing this issue and confused.

Do not use @jest/globals

means:

use @types/jest as described as the alternative method in https://jestjs.io/docs/getting-started#type-definitions @jest/globals does not work for some reason that might be related to swc, @types/jest works without issues.