-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Jest tests failing when testing components with dynamic import in Next 10.2.0 #24566
Comments
Possibly related to #24281 ? |
Ok added a reproducible example: Failing example: https://github.com/ballpit/dynamic-10.2-bug/tree/master/components To run, just clone and run npm run test. Jest is configured to use the following babel config: {
"presets": ["next/babel"],
"plugins": []
} |
Suffering from same issue. jest-next-dynamic also stopped working with 10.2 update. Workarounds other than downgrading Next.js to 10.1.3 would be appreciated. |
We have just updated our project and stated to experience the same issue! Indeed, it seems related to #24281 We are also using jest-next-dynamic which has an issue open too but it seems the issue is directly here. It would be nice if we could provide a better API for testing and have the behaviour of jest-next-dynamic directly from nextjs. Let me know if we could help in any way. |
Experiencing this issue too. |
Having the same errors from the same line in |
Nearly the same issue when using
|
Looks like a fix is being developed in #24751 Thanks you maintainers! |
I'm experiencing this same issue just after update Next.js version to 10.2 and webpack from 4 to 5. Component:
Jest error:
|
Hi, for now I'm doing this: ####### jest.setup.js jest.isolateModules(() => {
const preloadAll = require('jest-next-dynamic');
beforeAll(async () => {
await preloadAll();
});
}); And always using ssr false: const ResponsiveImages = dynamic(import('components/ResponsiveImage/ResponsiveImages'), { ssr: false }); Maybe it could help you @bcomnes |
Cool thank you for the workaround @omariosouto! I will try that. |
I tried it out, however this did not solve the problem for me, same error. I had to add the following line to my jest config file to enable a setup file: setupFilesAfterEnv: ['./jest.setup.js'] And even added the additional babel plugin they recommend: "plugins": ["babel-plugin-dynamic-import-node"] |
unfortunately it did not solve the problem for me too :( still waiting for a solution |
it seems the |
is it possible to overwrite this plugin version? at least until a proper solution is provided I really don’t want to downgrade next version haha |
@correttojs can you share your configs, please? |
|
Great! This workaround worked for me. For a project not using typescript, but using styled-jsx, this is what my config looks like: // .babelrc.js
module.exports = (api) => {
const isTest = api.env('test')
api.cache(true)
// remove this part when https://github.com/vercel/next.js/issues/24566 is closed
if (isTest) {
return {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['styled-jsx/babel'],
env: {
test: {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]]
}
}
}
}
return {
presets: ['next/babel']
}
} This appears to be a valid work around until #24751 is landed. |
I can confirm that this workaround works well. The only think I had to adjust was to extend the
|
Not sure if completely related but in [email protected] I get this (it worked fine in [email protected])
my .babelrc.js is just
|
Yup, thats the exact error if that component has a dynamic import in it. |
Just to follow up because people have asked me about this, after #24751 was landed, I was able to revert back to a simple .babelrc
and everything worked fine. |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
10.2.0
What version of Node.js are you using?
14
What browser are you using?
N/A (but JSDOM in the test runner)
What operating system are you using?
macos linux
How are you deploying your application?
Pre deploy failure
Describe the Bug
I'm still putting together a minimal reproduction case (will post shortly), but(added) My jest tests that test rendering of components that make use ofnext/dynamic
began failing on next 10.2.0.The error:
The simplified component in question:
This is the code that causes the test to fail with the above error:
The test that fails:
When those were removed, the tests passed again on 10.2.0. Leaving them in and pinning next to 10.1.3, things worked again.
Expected Behavior
That I can test react components in jest that utilize
next/dynamic
.To Reproduce
Reproducible gist incoming. I need to extract out a simplified example. Hang tight.Failing example: https://github.com/ballpit/dynamic-10.2-bug/tree/master/components
Passing workaround: https://github.com/ballpit/dynamic-10.2-bug/compare/workaround
To run, just clone and run npm run test. Jest is configured to use the following babel config:
The text was updated successfully, but these errors were encountered: