Skip to content

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

Closed

Description

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions