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

[Bug]: jest.isolateModulesAsync does not work with ESM modules #14387

Closed
patrickdawson opened this issue Aug 4, 2023 · 6 comments · Fixed by #14397
Closed

[Bug]: jest.isolateModulesAsync does not work with ESM modules #14387

patrickdawson opened this issue Aug 4, 2023 · 6 comments · Fixed by #14397

Comments

@patrickdawson
Copy link

Version

29.6.2

Steps to reproduce

Steps to reproduce:

  1. Clone https://github.com/patrickdawson/jestIsolateModulesAsyncBug.git
  2. npm install
  3. npm run test
    Test shows the error

Expected behavior

In this testcase

 it('should have a fresh module state in each isolateModulesAsync context', async () => {
    await jest.isolateModulesAsync(async () => {
      const { getState, incState } = await import('../src/main.js');
      expect(getState()).toBe(0);
      incState();
      expect(getState()).toBe(1);
    });
    await jest.isolateModulesAsync(async () => {
      const { getState, incState } = await import('../src/main.js');
      expect(getState()).toBe(0);
      incState();
      expect(getState()).toBe(1);
    });
  });

I expect according to the docs https://jestjs.io/docs/jest-object#jestisolatemodulesasyncfn that each isolateModulesAsync context has its own 'fresh" module so all module variables should be in the intial state. The test shows that this is not the case

Actual behavior

The state of the previous isolateModulesAsync context affects the second scope.

Additional context

No response

Environment

System:
  OS: Windows 11 22H2 (Build 22621.1992)
  CPU: 12th Gen Intel(R) Core(TM) i9-12900K, 3200 MHz, 16 Core, 24 logical cores
Binaries:
  Node: 18.17.2
  npm: 9.6.7
@mrazauskas
Copy link
Contributor

It works with CJS, but fails with ESM modules. There is no need to have "type": "module" in package.json, enough to await import() an .mjs file. I guess this is because _isolatedEsmoduleRegistry is not yet implemented:

private _isolatedModuleRegistry: ModuleRegistry | null;
private _moduleRegistry: ModuleRegistry;
private readonly _esmoduleRegistry: Map<string, VMModule>;

@mrazauskas
Copy link
Contributor

@patrickdawson Could you add a title to this issue, please? Something, like: "jest.isolateModulesAsync does not work with ESM modules".

@patrickdawson patrickdawson changed the title [Bug]: [Bug]: jest.isolateModulesAsync does not work with ESM modules Aug 4, 2023
@patrickdawson
Copy link
Author

Thanks for the quick follow up. I updated the title and the code example in the repository to verify that cjs indeed works. If this is not an easy fix maybe add a hint to the docs so it becomes clear that only cjs is supported at the moment.

@eryue0220
Copy link
Contributor

@mrazauskas Can I pick up this?

@mrazauskas
Copy link
Contributor

Sure. Would be nice to fix this.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants