question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking issue when using Jest with the Rust compiler (`next/jest`)

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Reactions:22
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
timneutkenscommented, Jan 27, 2022

Opened a PR that fixes this issue: https://github.com/vercel/next.js/pull/33731

7reactions
bastibuckcommented, Jan 19, 2022

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 🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js 12, Jest & Testing Library (Rust compiler setup) error
I have resolved this issue. Here is what had happened: For reasons that I do not understand, when I installed the Jest &...
Read more >
Testing Next.js apps with Jest - LogRocket Blog
Jest makes testing large Next.js apps a breeze with automated tests ... Jest configuration inbuilt as well, powered by the Rust compiler (a ......
Read more >
Testing | Next.js
Under the hood, next/jest is automatically configuring Jest for you, including: Setting up transform using SWC; Auto mocking stylesheets ( .css , .module.css...
Read more >
Jest Preview supports Next.js
First-class support for Next.js Rust-based compiler for Jest​. We want Jest Preview to have the best DX for all frontend developers. Besides, ...
Read more >
ECMAScript Modules - Jest
ESM mocking is supported through jest.unstable_mockModule . As the name suggests, this API is still work in progress, please follow this issue ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found