-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Dynamic imports testing failure #3345
Comments
Not sure if related, but the
|
https://github.com/PrudviGali/nextjs-dynamic-import-test-fail repo is a clone of |
I've the same issue in my own project using this config.
|
Here's my comment on this: #3405 (comment) |
Thanks for your response, but the issue is not with TC39 dynamic import proposal works pretty well with Jest testing. Please refer to the repo. But Nextjs implementation of dynamic imports fails in jest because of above two methods repo. Using jsDOM does not help much here. I would request you to re-open this issue and provide a fix if #3405 is not an appropriate way to fix this issue. |
@PrudviGali what we've in an implementation of the TC39 dynamic proposal with some minor implementation changes to support SSR. You are trying to do something which is meant to work in the client. (With Jest testing here) |
@arunoda completely agree with you. Using webpack specific methods We should have a common way to check for non-webpack environments. Using I hope #3414 solves this purpose and detects all non-webpack environments to load fallbacks for |
Let's talk on the other PR. |
Hey, sorry for the necro post, but I'm using Next 5.0.0 and it looks like this issue is still happening. I'm trying to run a jest snapshot test over a page with a dynamically loaded component and running into the I followed the thread and found that the fix mentioned above was removed as part of the new universal webpack change, which could have affected the fix. Is this still supposed to work? |
@arunoda seconded the comment above. Just installed 5.0.0 to test out if that would fix the issue and discovered it does not. |
@timneutkens is this supposed to work still with the universal webpack setup? |
handle-imports (e093441#diff-f6a1a602536347313a67de6bccddafd4L19) introduces back the issue with jest. I tried adding back the check for require.resolveWeak. It allows jest (or node env not using webpack) to run the files that are using dynamic imports. Any reason it was removed @timneutkens? |
Added back the check for resolve.weakResolve just in case this change was not intentional. An alternative to this, just for jest, might be something like https://github.com/developit/modify-babel-preset and a jest preprocesor configuration where we use babel-core and a modified configuration of the next preset to exclude the handle import. That might solve this issue for jest without requiring changes in nextjs, but then every other framework that tests components that rely on dynamic loading in next will need to do something similar. |
Another solution I found which is a bit of a hack is to check for For example: loadGloballyDeferredComponents = () => {
if (typeof require.resolveWeak === 'undefined') {
return;
}
import('path/to/my/component')
.then((componentModule) => {
this.setState({
MyComponent: componentModule.default,
});
})
.catch((err) => {
console.error(err);
});
} I'm not too pleased by this because I have to update my code to allow tests to run, and I have to manually include the component that gets set into the state as |
Final update on my end. Was able to configure jest to parse all nextjs files that used {
"env": {
"test": {
"presets": [
["env", { "modules": "commonjs" }],
"react"
],
"plugins": [
"jest-hoist",
"inline-dotenv",
"transform-inline-environment-variables",
"transform-object-rest-spread",
"dynamic-import-node",
"transform-regenerator"
]
}
}
} |
…4208) Just in case vercel#3345 was a regression and nextjs should still support non webpack node envs for testing.
Expected Behavior
Next.js dynamic imports should work with jest test suites.
Current Behavior
It is failing when we run the test with the following error.
TypeError: require.resolveWeak is not a function
Steps to Reproduce (for bugs)
Your Environment
The text was updated successfully, but these errors were encountered: