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

Not compatible with Jest's resetMocks option #19

Open
josselinbuils opened this issue Sep 23, 2020 · 2 comments
Open

Not compatible with Jest's resetMocks option #19

josselinbuils opened this issue Sep 23, 2020 · 2 comments

Comments

@josselinbuils
Copy link

josselinbuils commented Sep 23, 2020

Hello,

First thank you for this great lib 😄

It seems that the way to use this library described in the documentation is not compatible with the resetMocks option of Jest.

Indeed, next/dynamic is only mocked for the first test and the real one is restored from the second test.

I don't know if it could be managed by the lib directly but it could be at least documented.

Regards,
Josselin BUILS

@josselinbuils josselinbuils changed the title Not compatible with resetMocks option Not compatible with Jest's resetMocks option Sep 23, 2020
@josselinbuils
Copy link
Author

Here is a possible but quite verbose workaround:

  let MyComponent;

  beforeAll(async () => {
    let preloadAll;
    jest.isolateModules(() => {
      preloadAll = require('jest-next-dynamic');
    });
    MyComponent = require('../MyComponent').default;
    await preloadAll();
  });

@josselinbuils
Copy link
Author

josselinbuils commented Sep 23, 2020

I don't understand why it is working but putting this in a file referenced by setupFilesAfterEnv in the Jest config seems also to fix the issue:

jest.isolateModules(() => {
  const preloadAll = require('jest-next-dynamic');

  beforeEach(async () => {
    await preloadAll();
  });
});

The unknown thing to me is why the mock implementation is not reset between tests in these conditions as this setup file seems to be executed only once by test file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant