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

Regression in 12.0.8 for jest: TypeError: require.resolveWeak is not a function #33476

Closed
zmarty opened this issue Jan 19, 2022 · 19 comments · Fixed by #33908
Closed

Regression in 12.0.8 for jest: TypeError: require.resolveWeak is not a function #33476

zmarty opened this issue Jan 19, 2022 · 19 comments · Fixed by #33908
Assignees
Labels
Testing Related to testing with Next.js. Webpack Related to Webpack with Next.js.

Comments

@zmarty
Copy link

zmarty commented Jan 19, 2022

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.

@zmarty zmarty added the bug Issue was opened via the bug report template. label Jan 19, 2022
@balazsorban44
Copy link
Member

Based on your reproduction, I could not reproduce this issue. Could you create one that I can have a look at?

On next info: OS version is reported by the native os.version() Node.js API, so it's not much we can do about that. Probably related to the Node.js version you are using, maybe you have to update Node.js.

@balazsorban44 balazsorban44 added please add a complete reproduction The issue lacks information for further investigation Testing Related to testing with Next.js. and removed bug Issue was opened via the bug report template. labels Jan 19, 2022
@Parnswir
Copy link
Contributor

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.

@balazsorban44
Copy link
Member

Hi, @Parnswir, thanks! Please create a small public reproduction then 🙏 I cannot see the issue, so it might be related to your project specifically. Try reducing the number of pages/components/code until a point it can be shared and the issue still reproduces.

Here is a good resource on this topic.

@balazsorban44 balazsorban44 added the Webpack Related to Webpack with Next.js. label Jan 20, 2022
@Parnswir
Copy link
Contributor

Parnswir commented Jan 20, 2022

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

@balazsorban44
Copy link
Member

Hmm. Thanks for the CodeSandbox, it is helpful! I recognized you did not follow the test setup from https://nextjs.org/docs/testing

Applying those seems to solve the problem. Could you try it?

@zmarty are you using a custom/Babel setup for your test environment? Could you share it?

@duc-gp
Copy link

duc-gp commented Jan 20, 2022

Hmm. Thanks for the CodeSandbox, it is helpful! I recognized you did not follow the test setup from https://nextjs.org/docs/testing

Applying those seems to solve the problem. Could you try it?

@zmarty are you using a custom/Babel setup for your test environment? Could you share it?

What exactly are you referring to or what exactly did you changed in the codesandbox to make the test run? Can you provide a link?

@zmarty
Copy link
Author

zmarty commented Jan 20, 2022

@zmarty are you using a custom/Babel setup for your test environment? Could you share it?

So far we have tried to repro this on an empty Next.js instance and we can't.

We stopped using a custom babel setup for the website entirely a few versions ago.

I do see we use some custom webpack config in next.config.js, so we'll be looking at that as well. Inside the unit tests folder I only see that we set up some mocks.

@duc-gp
Copy link

duc-gp commented Jan 21, 2022

We also tried on an the https://github.com/vercel/next.js/tree/canary/examples/with-jest example and there it works.

Unfortunately we still need babel, otherwise we cannot use jest's automock functionality. I've forked and provided an example here:
https://github.com/vercel/next.js/compare/canary...duc-gp:example-jest-automock-not-working?expand=1

  • run npm run test example.test.js to start the test
  • when you outcomment the transform in jest.config.js + delete the .babelrc so babel is not used, the test does not work anymore

@balazsorban44
Copy link
Member

balazsorban44 commented Jan 21, 2022

@duc-gp
Copy link

duc-gp commented Jan 21, 2022

@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

@balazsorban44
Copy link
Member

Interesting, I could not reproduce still:

image

Could you add an actual repository link so I can verify this?

@duc-gp
Copy link

duc-gp commented Jan 24, 2022

Interesting, I could not reproduce still:

image

Could you add an actual repository link so I can verify this?

what exactly did you do to try to reproduce this? from what setup is your screenshot from?
the link to the repository is: https://github.com/duc-gp/next.js/tree/example-jest-automock-not-working-with-nextjs-babel-setup

  • switch to the branch example-jest-automock-not-working-with-nextjs-babel-setup !
  • go into examples/with-jest-babel
  • npm i
  • npm run test DummyA.test.js

edit: I just tried the same steps on another machine and could reproduce this as well
image

node versions I've tried:

  • v14.16.0
  • v16.13.0

@balazsorban44 balazsorban44 added kind: bug and removed please add a complete reproduction The issue lacks information for further investigation labels Jan 25, 2022
@balazsorban44
Copy link
Member

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

@PsyGik
Copy link

PsyGik commented Jan 28, 2022

Thank you for reporting this. I was upgrading a project to Next.js 12 and the CI just barfed all over with the error TypeError: require.resolveWeak is not a function.

On another note, the Upgrade Guide does not specify the changes that need to be done to setup Jest with the Rust compiler when upgrading to Next 12. It'd be nice to have the docs updated please.

@timneutkens timneutkens self-assigned this Jan 31, 2022
@timneutkens
Copy link
Member

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

timneutkens added a commit to timneutkens/next.js that referenced this issue Feb 2, 2022
Fixes vercel#33476

I've added the test for this but it passes either way, even when the change to loadable.js is not made.
@kodiakhq kodiakhq bot closed this as completed in #33908 Feb 8, 2022
kodiakhq bot pushed a commit that referenced this issue Feb 8, 2022
Fixes #33476

I've added the test for this but it passes either way, even when the change to loadable.js is not made. On the reproduction I was able to reproduce it consistently and it's fixed with this change. 



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@timneutkens
Copy link
Member

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

@ryparker
Copy link
Contributor

ryparker commented Feb 16, 2022

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

Works for me 👍🏻 Thanks!

natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
Fixes vercel#33476

I've added the test for this but it passes either way, even when the change to loadable.js is not made. On the reproduction I was able to reproduce it consistently and it's fixed with this change. 



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@timneutkens
Copy link
Member

Great 👍 This is out on stable in 12.1

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Testing Related to testing with Next.js. Webpack Related to Webpack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants