next.js: dynamic import not working in test environment
Bug report
Describe the bug
I have upgraded next.js from version 6 to 7, and running tests with jest now exits with the following:
TypeError: require.resolveWeak is not a function
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Clone https://github.com/giovannigiordano/next.js/tree/master/examples/with-jest
- Install dependencies
- Run
yarn test - See error
Expected behavior
I expect testing dynamic import as would work as for version 6
Screenshots
https://i.imgur.com/i3aw7Eg.png
System information
- OS: macOS Mojave 10.14
- Version of Next.js: 7.0.1
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 9
- Comments: 41 (21 by maintainers)
heya, we’ve recently published a package that also mocks
next/dynamic, but actually lets you render the resulting dynamic components: https://github.com/FormidableLabs/jest-next-dynamicThe idea is:
next/dynamic, but wrap it and capture the Loadable component’spreloadfunction.preloadinitializers to a queue.preloadAllfunction that will flush the queue.From there, you just need to call
beforeAll(preloadAll)and it will be as if the dynamic components were all already available – meaning they actually render, appear in snapshots instead of “loading…”, etc. This is exactly how you’d do it withreact-loadable’s built-inLoadable.preloadAllmethod.Similar to the approach above, by mocking the
next/dynamicmodule and flushing a queue of every dynamic component, you don’t have to track down and export every single one in order to get at itspreloadmethod. 😃Hope that helps someone!
(* Since the default Jest environment simulates a DOM environment, even if you get a handle on Loadable’s built-in
preloadAllmethod, it won’t actually work due to this line which skips enqueuing the preload initializers – since in a Jest DOM environment,windowwill be defined.)FWIW, here’s the fix we’ve implemented inside
jest.setup.json my team. The advantage is that it’s set up globally for all our tests.The behavior should be exactly the same as before as the dynamic weren’t ever processed (even inside snapshots if you have some)
Hi @Timer, I’m not sure this issue should be closed. I’m still facing this issue (same error as in the first post). I’ve made a minimal reproducible version here: https://github.com/ellsclytn/next-with-jest
A clone, install all deps, and
yarn test/npm run testwill show it. This particular version has all dependencies updated to their latest at the time of writing.This was fixed recently in Next.js 8.1 or 9, I cannot recall exactly.
this is how i hacked it,
Since the release 9.1.2, I have this error in all of my test js with a dynamic component I have this error:
TypeError: LoadableComponent.preload is not a function.Did you have this problem ?
also got the same error in next 9.0.
I use typescript and added
"babel-plugin-dynamic-import-node",to the plugins intest-env.my babelrc looks like this:
EDIT: using https://github.com/FormidableLabs/jest-next-dynamic solved the issue for me
@HeroProtagonist actually I have mocked
next/dynamicand addedbabel-plugin-transform-dynamic-import.Hi, Is there any update regarding this problem? Using jest-next-dynamic is a bit hacky and it does not work fine with shallow render in test.
@jhoffmcd I kept tinkering with it, took an approach of having separate tsconfig from this repo: https://github.com/deptno/next.js-typescript-starter-kit.
Forked/revised sandbox (https://codesandbox.io/s/949zpj8yw) seems to be working. I don’t know if this is the right/best approach but… it works?
Oh and as far as advising the Next.js team on a potential built-in solution for this:
NODE_ENVis set totest– maybe the customimportBabel plugin is another thing to switch out there? Currently theimportplugin pretty much depends on webpack, but it’s rare to use webpack for tests.react-loadableimplementation, which enqueuespreloadinitializers even on the client (notypeof windowcheck).preloadAllas an export ofnext/dynamic. Right now it’s possible to callpreloadon individualdynamic()results, butpreloadAllisn’t exposed as an API anywhere (unless you reach intodistfolders to findloadable.js).@Emiliano-Bucci Feel free to open a new issue with a reproduction that we can take a look at 🙏
same issue here and the dynamic imports returns this
@damusnet Can you see test it with the
.babelrcin this codesandbox 🙏it looks like this:
This used to work for me without any change to the code or
.babelrc, butnext@9.0.1breaks my tests again. (see https://github.com/zeit/next.js/issues/7872).makes them pass again but seems unfortunate as it means that tests and code don’t use the same config anymore. And using
babel-plugin-dynamic-import-nodein all environments seems to disable the dynamic loading altogether (i.e. everything works but no chunks are loaded).@lfades I’m having the same issue as originally posted but can’t seem to fix it by adding jest-next-dynamic, babel-plugin-dynamic-import-node and using the .babelrc setup you suggested.
I upgraded to 8.1.0 without any difference, still getting the original
TypeError: require.resolveWeak is not a functionerror. Any ideas on how I can continue to debug this?Here’s a working sandbox: https://codesandbox.io/s/nextdynamictestingissue-siuvx using a fork from @jhoffmcd sandbox.
The only change I did was to
.babelrc:Using
next@8.1.0, it also works in the latestcanary. Please let me know if it’s working.indeed sorry, here there is my solution: