-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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 import not working in test environment #5416
Comments
Sounds like you have to transpile them yourself in the test environment using the babel plugin. |
Hi! I can take a look at his 👀 |
@HeroProtagonist actually I have mocked |
I'm using
Could you try the latest version ? |
@revskill10 I hope you have tried my fork because even if I use 7.0.2-canary.7 it doesn't work. |
@HeroProtagonist did you get a chance to have a look? I'm having the same issue and would like to get some support here, as it seems to me that the changes to dynamic imports in Next.js@7 are not compatible with the jest env. And no instructions given, nor there's an example/integration in the Next.js repo itself The issue is reproducible in the repo @giovannigiordano provided. |
hi @giovannigiordano can you elaborate a bit more about how you mocked the dynamic/import ? I can't figure it out yet :( babelrc
test
file
|
@Lidofernandez I've followed your same flow. |
@giovannigiordano the problem I'm trying to test my dynamic component with jest and snapshot. It should render the dynamic component onClick but it nothing happens:
|
@Lidofernandez if you mock the dynamic import substituting the implementation with an empty function, the module will return undefined so it won't show in the snapshot |
indeed sorry, here there is my solution:
|
FWIW, here's the fix we've implemented inside // Manually mock next/dynamic as the next.js (7.0.2) babel plugin will compile to Webpack
// lazy imports (require.resolveWeak) who're conflicting with the Node module system.
jest.mock('next/dynamic', () => () => {
const DynamicComponent = () => null;
DynamicComponent.displayName = 'LoadableComponent';
DynamicComponent.preload = jest.fn();
return DynamicComponent;
}); The behavior should be exactly the same as before as the dynamic weren't ever processed (even inside snapshots if you have some) |
heya, we've recently published a package that also mocks The idea is:
From there, you just need to call Similar to the approach above, by mocking the Hope that helps someone! (* Since the default Jest environment simulates a DOM environment, even if you get a handle on Loadable's built-in |
Oh and as far as advising the Next.js team on a potential built-in solution for this:
|
this is how i hacked it,
|
Here is my custom solution using Jest node modules mocking: /* ./__mocks__/next/dynamic.js */
import React from 'react';
const mockReact = React;
let componentName = 'DynamicComponent';
export const __setComponentName = data => {
componentName = data;
};
const DynamicComponent = () => ({ children, ...rest }) =>
mockReact.createElement(componentName, rest, children);
export default DynamicComponent; Then in your test: import React from 'react';
import TestRenderer from 'react-test-renderer';
import MyComponent from '..';
describe('<MyComponent />', () => {
require('next/dynamic').__setComponentName('NameOfTheDynamicComponentForTheSnapshot');
it('should work as usual', () => {
const component = TestRenderer.create(<MyComponent />);
expect(component.toJSON()).toMatchSnapshot();
});
}); No dependency or babel plugin needed. It will render the snapshot as if you had called |
I wonder if this is solved in canary with the import to node transform, cc @lfades |
Just wanted to give a quick link to a CodeSandbox repro: https://codesandbox.io/s/4j3lw8lj20 If you fork this and open a new terminal and run |
@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? |
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. |
Changing the code to pass the tests seems not a good idea. But anyway it worked! |
Here's a working sandbox: https://codesandbox.io/s/nextdynamictestingissue-siuvx using a fork from @jhoffmcd sandbox. The only change I did was to {
"presets": [
"next/babel",
"@zeit/next-typescript/babel"
],
"env": {
"test": {
"plugins": [
"babel-plugin-dynamic-import-node"
]
}
}
} Using |
@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 |
@axelinternet I did a change to |
This used to work for me without any change to the code or {
"presets": ["next/babel"],
"env": {
"test": {
"plugins": ["babel-plugin-dynamic-import-node"]
}
}
} makes them pass again but seems unfortunate as it means that tests and code don't use the same config anymore. And using |
@damusnet Can you see test it with the it looks like this: {
"presets": [
"next/babel"
],
"env": {
"test": {
"presets": [
[
"next/babel",
{
"transform-runtime": {
"corejs": false
}
}
]
],
"plugins": [
"babel-plugin-dynamic-import-node"
],
}
}
} |
also got the same error in next 9.0. I use typescript and added my babelrc looks like this:
EDIT: using https://github.com/FormidableLabs/jest-next-dynamic solved the issue for me |
I have met this error too and try above solutions except using jest-next-dynamic. |
This was fixed recently in Next.js 8.1 or 9, I cannot recall exactly. |
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 |
@Timer, could you please provide a working example of this, since I am using edit: I found the answer on stack overflow |
I'm not sure this is really a solution. The jest-next-dynamic lib is working for me but I feel this is something that should be working out of the box for Next (and the team appears to think so too). |
Sure, it would be great to have them provide a working example for us. |
Since the release 9.1.2, I have this error in all of my test js with a dynamic component I have this error: Did you have this problem ? |
Are there any news on this issue? After upgrading to next js 10 i start to get again |
@Emiliano-Bucci Feel free to open a new issue with a reproduction that we can take a look at 🙏 |
@SBoudrias This worked for me even in 2021. Anyone who actually needs to test the result of a dynamic import will probably need a different solution though. |
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. |
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:
yarn test
Expected behavior
I expect testing dynamic import as would work as for version 6
Screenshots
https://i.imgur.com/i3aw7Eg.png
System information
The text was updated successfully, but these errors were encountered: