next.js: Regression in 12.0.8 for jest: TypeError: require.resolveWeak is not a function

Run next info (available from version 12.0.8 and up)

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
Binaries:
  Node: 16.8.0
  npm: 7.21.0
  Yarn: 1.22.10
  pnpm: N/A
Relevant packages:
  next: 12.0.8
  react: 17.0.2
  react-dom: 17.0.2

What version of Next.js are you using?

12.0.8

What version of Node.js are you using?

16.8.0

What browser are you using?

Edge (Chromium)

What operating system are you using?

Windows 11 Enterprise (by the way, why does next info say I am using Windows 10??)

How are you deploying your application?

On Azure App Service as docker container

Describe the Bug

We have a Next.js website where the unit tests are using Jest. When I try to upgrade the website from Next.js 12.0.7 to 12.0.8 the unit tests which test code that uses dynamic code loading start failing.

It looks to me this is related to this previously closed bug, which would make this a regression for Next.js 12.0.8: TypeError: require.resolveWeak is not a function (jest test) #19862

Example output

  ● Test suite failed to run

    TypeError: require.resolveWeak is not a function

    > 29 | const HeaderSearchBoxMobile = dynamic(() => import('../HeaderSearchBox/HeaderSearchBoxMobile/HeaderSearchBoxMobile'));

Expected Behavior

Tests should pass even if the code they test uses dynamic()

To Reproduce

Run a Jest test where the test code tests some code that uses dynamic() to load some other code.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Found the problem, opening a PR with a new test that covers this case.

I tried a totally clean project again with yarn create next-app -e with-babel-jest, added your modifications, and finally reproduced it. 👍

This is released on canary 🎉 Please try it out: npm install next@canary.

https://codesandbox.io/s/next-dynamic-22ejz

if you fork it on codesandbox, you can run npm test in the terminal on the right (click the + icon). On next@12.0.8 (which is installed in this sandbox), the test will fail. If you downgrade to next@12.0.7 (select the version in the dependecies list on the left), the test will pass.

Often, when the codesandbox container hangs; you can restart the next server from the menu on the left: image

@duc-gp Do note we provide a babel setup as well:

@balazsorban44 thanks, ive just checked out the setup you mentioned and added a simple test: https://github.com/vercel/next.js/compare/canary...duc-gp:example-jest-automock-not-working-with-nextjs-babel-setup?expand=1

when you run npm run test DummyA.test.js you can see the error TypeError: require.resolveWeak is not a function

We see the same issue in our code base (closed source, sorry): with next@12.0.8 tests fail when running jest; with next@12.0.7 everything is fine. Strangely, running jest --coverage works fine on both versions.