Mocking issue when using Jest with the Rust compiler (`next/jest`)
See original GitHub issueWhat version of Next.js are you using?
12.0.7
What version of Node.js are you using?
17.2.0
What browser are you using?
Chrome
What operating system are you using?
macOS 12.0.1
How are you deploying your application?
Vercel
Describe the Bug
I’m trying to test my Next.js 12 API Routes using Jest with the Rust compiler thanks to next/jest
(which is amazing).
It looks like I can’t use any mocking feature, for example a basic jest.mock()
.
When trying to customize a mock in a test, I would get an error similar to:
TypeError: mockFn.mockReturnValue is not a function
When trying to use jest.mock()
with an explicit factory as second parameter, the factory is not even called which may indicates nothing is actually mocked.
Even if @swc/jest
is not used, I wonder if this configuration problem described in a comment could be the main issue I’m hitting here as it looks like the Next.js base SWC options do not include these settings which are used by getJestSWCOptions()
.
As a temporary workaround, I’m declaring my jest.mock()
in another file that I have to import in my tests as suggested in the same issue which works but a bit of a pain to maintain.
Expected Behavior
Being able to use jest.mock()
in a test file.
To Reproduce
Use jest.mock()
in a test file with Jest and the Rust compiler thanks to next/jest
and try to customize the mock using mockFn.mockReturnValue()
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:22
- Comments:17 (4 by maintainers)
Top GitHub Comments
Opened a PR that fixes this issue: https://github.com/vercel/next.js/pull/33731
Can confirm the issue.
Started a fresh NextJS the other day and used Jest with Rust as the docs say. Tried to use manual mocks in my tests yesterday but it doesn’t pick up the mock-files.
Simple work-around for now: opt-out of rust and opt-in to the Babel version like the docs say. No changes in my tests and the mocks are picked up 🚀