From 3df471143e9ef1595cc75b2cabf6f93e553bb6e4 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Wed, 9 Aug 2023 18:26:11 +0800 Subject: [PATCH 01/11] fix: isolate esm async import bug --- packages/jest-runtime/src/index.ts | 38 +++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b34ecdfe3890..03f22c3974f8 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -179,6 +179,7 @@ export default class Runtime { private readonly _moduleMockFactories: Map unknown>; private readonly _moduleMocker: ModuleMocker; private _isolatedModuleRegistry: ModuleRegistry | null; + private _isolatedESMModuleRegistry: ModuleRegistry | null; private _moduleRegistry: ModuleRegistry; private readonly _esmoduleRegistry: Map; private readonly _cjsNamedExports: Map>; @@ -242,6 +243,7 @@ export default class Runtime { this._moduleMocker = this._environment.moduleMocker; this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; + this._isolatedESMModuleRegistry = null; this._moduleRegistry = new Map(); this._esmoduleRegistry = new Map(); this._cjsNamedExports = new Map(); @@ -416,12 +418,15 @@ export default class Runtime { query = '', ): Promise { const cacheKey = modulePath + query; + const registry = this._isolatedESMModuleRegistry + ? this._isolatedESMModuleRegistry + : this._esmoduleRegistry; if (this._fileTransformsMutex.has(cacheKey)) { await this._fileTransformsMutex.get(cacheKey); } - if (!this._esmoduleRegistry.has(cacheKey)) { + if (!registry.has(cacheKey)) { invariant( typeof this._environment.getVmContext === 'function', 'ES Modules are only supported if your test environment has the `getVmContext` function', @@ -454,7 +459,7 @@ export default class Runtime { context, ); - this._esmoduleRegistry.set(cacheKey, wasm); + registry.set(cacheKey, wasm); transformResolve(); return wasm; @@ -462,7 +467,7 @@ export default class Runtime { if (this._resolver.isCoreModule(modulePath)) { const core = this._importCoreModule(modulePath, context); - this._esmoduleRegistry.set(cacheKey, core); + registry.set(cacheKey, core); transformResolve(); @@ -526,11 +531,11 @@ export default class Runtime { } invariant( - !this._esmoduleRegistry.has(cacheKey), + !registry.has(cacheKey), `Module cache already has entry ${cacheKey}. This is a bug in Jest, please report it!`, ); - this._esmoduleRegistry.set(cacheKey, module); + registry.set(cacheKey, module); transformResolve(); } catch (error) { @@ -539,7 +544,7 @@ export default class Runtime { } } - const module = this._esmoduleRegistry.get(cacheKey); + const module = registry.get(cacheKey); invariant( module, @@ -563,14 +568,18 @@ export default class Runtime { return; } + const registry = this._isolatedESMModuleRegistry + ? this._isolatedESMModuleRegistry + : this._esmoduleRegistry; + if (specifier === '@jest/globals') { - const fromCache = this._esmoduleRegistry.get('@jest/globals'); + const fromCache = registry.get('@jest/globals'); if (fromCache) { return fromCache; } const globals = this.getGlobalsForEsm(referencingIdentifier, context); - this._esmoduleRegistry.set('@jest/globals', globals); + registry.set('@jest/globals', globals); return globals; } @@ -586,7 +595,7 @@ export default class Runtime { return this.importMock(referencingIdentifier, specifier, context); } - const fromCache = this._esmoduleRegistry.get(specifier); + const fromCache = registry.get(specifier); if (fromCache) { return fromCache; @@ -662,7 +671,7 @@ export default class Runtime { } } - this._esmoduleRegistry.set(specifier, module); + registry.set(specifier, module); return module; } @@ -1164,21 +1173,28 @@ export default class Runtime { } async isolateModulesAsync(fn: () => Promise): Promise { - if (this._isolatedModuleRegistry || this._isolatedMockRegistry) { + if ( + this._isolatedModuleRegistry || + this._isolatedMockRegistry || + this._isolatedESMModuleRegistry + ) { throw new Error( 'isolateModulesAsync cannot be nested inside another isolateModulesAsync or isolateModules.', ); } this._isolatedModuleRegistry = new Map(); this._isolatedMockRegistry = new Map(); + this._isolatedESMModuleRegistry = new Map(); try { await fn(); } finally { // might be cleared within the callback this._isolatedModuleRegistry?.clear(); this._isolatedMockRegistry?.clear(); + this._isolatedESMModuleRegistry?.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; + this._isolatedESMModuleRegistry = null; } } From e6ba77b9a4b295f9c1cafc69d500f6280dc7c3d5 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 10 Aug 2023 00:29:57 +0800 Subject: [PATCH 02/11] fix: add test plan --- .../__snapshots__/asyncESMImport.test.ts.snap | 9 ++++++++ e2e/__tests__/asyncESMImport.test.ts | 20 +++++++++++++++++ e2e/async-esm-import/__tests__/test.js | 22 +++++++++++++++++++ e2e/async-esm-import/main.js | 15 +++++++++++++ e2e/async-esm-import/package.json | 7 ++++++ 5 files changed, 73 insertions(+) create mode 100644 e2e/__tests__/__snapshots__/asyncESMImport.test.ts.snap create mode 100644 e2e/__tests__/asyncESMImport.test.ts create mode 100644 e2e/async-esm-import/__tests__/test.js create mode 100644 e2e/async-esm-import/main.js create mode 100644 e2e/async-esm-import/package.json diff --git a/e2e/__tests__/__snapshots__/asyncESMImport.test.ts.snap b/e2e/__tests__/__snapshots__/asyncESMImport.test.ts.snap new file mode 100644 index 000000000000..54d8b55b71c6 --- /dev/null +++ b/e2e/__tests__/__snapshots__/asyncESMImport.test.ts.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`runs test with async ESM import 1`] = ` +"Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites." +`; diff --git a/e2e/__tests__/asyncESMImport.test.ts b/e2e/__tests__/asyncESMImport.test.ts new file mode 100644 index 000000000000..6825788852d1 --- /dev/null +++ b/e2e/__tests__/asyncESMImport.test.ts @@ -0,0 +1,20 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {extractSummary} from '../Utils'; +import runJest from '../runJest'; + +test('runs test with async ESM import', () => { + const {exitCode, stderr} = runJest('async-esm-import', [], { + nodeOptions: '--experimental-vm-modules --no-warnings', + }); + + const {summary} = extractSummary(stderr); + + expect(summary).toMatchSnapshot(); + expect(exitCode).toBe(0); +}); diff --git a/e2e/async-esm-import/__tests__/test.js b/e2e/async-esm-import/__tests__/test.js new file mode 100644 index 000000000000..70b9a91b908a --- /dev/null +++ b/e2e/async-esm-import/__tests__/test.js @@ -0,0 +1,22 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +import {jest} from '@jest/globals'; + +test('should have a fresh module state in each isolateModulesAsync context', async () => { + await jest.isolateModulesAsync(async () => { + const {getState, incState} = await import('../main.js'); + expect(getState()).toBe(0); + incState(); + expect(getState()).toBe(1); + }); + await jest.isolateModulesAsync(async () => { + const {getState, incState} = await import('../main.js'); + expect(getState()).toBe(0); + incState(); + expect(getState()).toBe(1); + }); +}); diff --git a/e2e/async-esm-import/main.js b/e2e/async-esm-import/main.js new file mode 100644 index 000000000000..fd9d95f6fab5 --- /dev/null +++ b/e2e/async-esm-import/main.js @@ -0,0 +1,15 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +let myState = 0; + +export function incState() { + myState += 1; +} + +export function getState() { + return myState; +} diff --git a/e2e/async-esm-import/package.json b/e2e/async-esm-import/package.json new file mode 100644 index 000000000000..1a3caa4daa6c --- /dev/null +++ b/e2e/async-esm-import/package.json @@ -0,0 +1,7 @@ +{ + "name": "async-esm-import", + "type": "module", + "jest": { + "testEnvironment": "node" + } +} From a53ffa5da7bc1ff481a17329e94d72ed1ed94f20 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 10 Aug 2023 00:38:41 +0800 Subject: [PATCH 03/11] chore: update pkg.json && readme --- CHANGELOG.md | 2 ++ e2e/async-esm-import/package.json | 1 - e2e/resolve-async/package.json | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 953ca6b204ca..8d103beed525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-runtime]` Fix dynamic esm import module bug when loaded module through `jest.isolateModulesAsync` ([14397](https://github.com/jestjs/jest/pull/14397)) + ### Chore & Maintenance ### Performance diff --git a/e2e/async-esm-import/package.json b/e2e/async-esm-import/package.json index 1a3caa4daa6c..4cb1bd3732ee 100644 --- a/e2e/async-esm-import/package.json +++ b/e2e/async-esm-import/package.json @@ -1,6 +1,5 @@ { "name": "async-esm-import", - "type": "module", "jest": { "testEnvironment": "node" } diff --git a/e2e/resolve-async/package.json b/e2e/resolve-async/package.json index ea366d4b6bde..4fb4b6922088 100644 --- a/e2e/resolve-async/package.json +++ b/e2e/resolve-async/package.json @@ -1,5 +1,4 @@ { - "type": "module", "jest": { "resolver": "/resolver.cjs", "transform": {} From 54ff86e0cda477f0ced0080120cf4a82dfc0d6c0 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 10 Aug 2023 10:43:47 +0800 Subject: [PATCH 04/11] fix: code review --- e2e/async-esm-import/__tests__/{test.js => main.mjs} | 4 ++-- e2e/async-esm-import/{main.js => main.mjs} | 0 e2e/async-esm-import/package.json | 9 ++++++++- e2e/resolve-async/package.json | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) rename e2e/async-esm-import/__tests__/{test.js => main.mjs} (82%) rename e2e/async-esm-import/{main.js => main.mjs} (100%) diff --git a/e2e/async-esm-import/__tests__/test.js b/e2e/async-esm-import/__tests__/main.mjs similarity index 82% rename from e2e/async-esm-import/__tests__/test.js rename to e2e/async-esm-import/__tests__/main.mjs index 70b9a91b908a..9a0974c0a726 100644 --- a/e2e/async-esm-import/__tests__/test.js +++ b/e2e/async-esm-import/__tests__/main.mjs @@ -8,13 +8,13 @@ import {jest} from '@jest/globals'; test('should have a fresh module state in each isolateModulesAsync context', async () => { await jest.isolateModulesAsync(async () => { - const {getState, incState} = await import('../main.js'); + const {getState, incState} = await import('../main.mjs'); expect(getState()).toBe(0); incState(); expect(getState()).toBe(1); }); await jest.isolateModulesAsync(async () => { - const {getState, incState} = await import('../main.js'); + const {getState, incState} = await import('../main.mjs'); expect(getState()).toBe(0); incState(); expect(getState()).toBe(1); diff --git a/e2e/async-esm-import/main.js b/e2e/async-esm-import/main.mjs similarity index 100% rename from e2e/async-esm-import/main.js rename to e2e/async-esm-import/main.mjs diff --git a/e2e/async-esm-import/package.json b/e2e/async-esm-import/package.json index 4cb1bd3732ee..8704a3b7ac42 100644 --- a/e2e/async-esm-import/package.json +++ b/e2e/async-esm-import/package.json @@ -1,6 +1,13 @@ { "name": "async-esm-import", "jest": { - "testEnvironment": "node" + "testEnvironment": "node", + "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js?|ts?|mjs?|mts?)$", + "moduleFileExtensions": [ + "js", + "ts", + "mjs", + "mts" + ] } } diff --git a/e2e/resolve-async/package.json b/e2e/resolve-async/package.json index 4fb4b6922088..ea366d4b6bde 100644 --- a/e2e/resolve-async/package.json +++ b/e2e/resolve-async/package.json @@ -1,4 +1,5 @@ { + "type": "module", "jest": { "resolver": "/resolver.cjs", "transform": {} From 08f9bb8de71685504f09679fcaef7376a637adcc Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 10 Aug 2023 13:12:29 +0800 Subject: [PATCH 05/11] fix: e2e test update --- e2e/async-esm-import/__tests__/{main.mjs => main.js} | 4 ++-- e2e/async-esm-import/{main.mjs => main.js} | 0 e2e/async-esm-import/package.json | 10 +++------- 3 files changed, 5 insertions(+), 9 deletions(-) rename e2e/async-esm-import/__tests__/{main.mjs => main.js} (82%) rename e2e/async-esm-import/{main.mjs => main.js} (100%) diff --git a/e2e/async-esm-import/__tests__/main.mjs b/e2e/async-esm-import/__tests__/main.js similarity index 82% rename from e2e/async-esm-import/__tests__/main.mjs rename to e2e/async-esm-import/__tests__/main.js index 9a0974c0a726..70b9a91b908a 100644 --- a/e2e/async-esm-import/__tests__/main.mjs +++ b/e2e/async-esm-import/__tests__/main.js @@ -8,13 +8,13 @@ import {jest} from '@jest/globals'; test('should have a fresh module state in each isolateModulesAsync context', async () => { await jest.isolateModulesAsync(async () => { - const {getState, incState} = await import('../main.mjs'); + const {getState, incState} = await import('../main.js'); expect(getState()).toBe(0); incState(); expect(getState()).toBe(1); }); await jest.isolateModulesAsync(async () => { - const {getState, incState} = await import('../main.mjs'); + const {getState, incState} = await import('../main.js'); expect(getState()).toBe(0); incState(); expect(getState()).toBe(1); diff --git a/e2e/async-esm-import/main.mjs b/e2e/async-esm-import/main.js similarity index 100% rename from e2e/async-esm-import/main.mjs rename to e2e/async-esm-import/main.js diff --git a/e2e/async-esm-import/package.json b/e2e/async-esm-import/package.json index 8704a3b7ac42..65c783ce83e6 100644 --- a/e2e/async-esm-import/package.json +++ b/e2e/async-esm-import/package.json @@ -1,13 +1,9 @@ { "name": "async-esm-import", + "type": "module", "jest": { + "transform": {}, "testEnvironment": "node", - "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js?|ts?|mjs?|mts?)$", - "moduleFileExtensions": [ - "js", - "ts", - "mjs", - "mts" - ] + "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js?|ts?|mjs?|mts?)$" } } From 3710da6e56ce61f1fcaa5cb43c1a9ad953761c79 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 10 Aug 2023 15:01:11 +0800 Subject: [PATCH 06/11] chore: remote config --- e2e/async-esm-import/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/e2e/async-esm-import/package.json b/e2e/async-esm-import/package.json index 65c783ce83e6..bb69b33c5159 100644 --- a/e2e/async-esm-import/package.json +++ b/e2e/async-esm-import/package.json @@ -3,7 +3,6 @@ "type": "module", "jest": { "transform": {}, - "testEnvironment": "node", - "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js?|ts?|mjs?|mts?)$" + "testEnvironment": "node" } } From 60f9f59255cba05fc09129e17ef2fc42c1facbe5 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 10 Aug 2023 16:18:19 +0800 Subject: [PATCH 07/11] fix: cr problem --- packages/jest-runtime/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 03f22c3974f8..769379e5d410 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1201,8 +1201,10 @@ export default class Runtime { resetModules(): void { this._isolatedModuleRegistry?.clear(); this._isolatedMockRegistry?.clear(); + this._isolatedESMModuleRegistry?.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; + this._isolatedESMModuleRegistry = null; this._mockRegistry.clear(); this._moduleRegistry.clear(); this._esmoduleRegistry.clear(); From 3a9d0a6d57b5f14a2ce417d249ec70d41ff653cf Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sat, 12 Aug 2023 12:52:33 +0800 Subject: [PATCH 08/11] chore: refactor file --- CHANGELOG.md | 2 +- ...cESMImport.test.ts.snap => isolateModulesAsync.test.ts.snap} | 0 .../{asyncESMImport.test.ts => isolateModulesAsync.test.ts} | 0 .../__tests__/main.js | 0 e2e/{async-esm-import => isolate-modules-async}/main.js | 0 e2e/{async-esm-import => isolate-modules-async}/package.json | 0 6 files changed, 1 insertion(+), 1 deletion(-) rename e2e/__tests__/__snapshots__/{asyncESMImport.test.ts.snap => isolateModulesAsync.test.ts.snap} (100%) rename e2e/__tests__/{asyncESMImport.test.ts => isolateModulesAsync.test.ts} (100%) rename e2e/{async-esm-import => isolate-modules-async}/__tests__/main.js (100%) rename e2e/{async-esm-import => isolate-modules-async}/main.js (100%) rename e2e/{async-esm-import => isolate-modules-async}/package.json (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d103beed525..551aa62d9f44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[jest-runtime]` Fix dynamic esm import module bug when loaded module through `jest.isolateModulesAsync` ([14397](https://github.com/jestjs/jest/pull/14397)) +- `[jest-runtime]` Fix dynamic esm import module bug when loaded module through `jest.isolateModulesAsync` ([14397](https://github.com/jestjs/jest/pull/14397)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/asyncESMImport.test.ts.snap b/e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap similarity index 100% rename from e2e/__tests__/__snapshots__/asyncESMImport.test.ts.snap rename to e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap diff --git a/e2e/__tests__/asyncESMImport.test.ts b/e2e/__tests__/isolateModulesAsync.test.ts similarity index 100% rename from e2e/__tests__/asyncESMImport.test.ts rename to e2e/__tests__/isolateModulesAsync.test.ts diff --git a/e2e/async-esm-import/__tests__/main.js b/e2e/isolate-modules-async/__tests__/main.js similarity index 100% rename from e2e/async-esm-import/__tests__/main.js rename to e2e/isolate-modules-async/__tests__/main.js diff --git a/e2e/async-esm-import/main.js b/e2e/isolate-modules-async/main.js similarity index 100% rename from e2e/async-esm-import/main.js rename to e2e/isolate-modules-async/main.js diff --git a/e2e/async-esm-import/package.json b/e2e/isolate-modules-async/package.json similarity index 100% rename from e2e/async-esm-import/package.json rename to e2e/isolate-modules-async/package.json From e12f6abe1b2ed3b5bd0ba00d00eb9a38e101d35a Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sat, 12 Aug 2023 14:41:22 +0800 Subject: [PATCH 09/11] fix: e2e test --- e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap | 2 +- e2e/__tests__/isolateModulesAsync.test.ts | 4 ++-- e2e/isolate-modules-async/package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap b/e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap index 54d8b55b71c6..c1577869b112 100644 --- a/e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap +++ b/e2e/__tests__/__snapshots__/isolateModulesAsync.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`runs test with async ESM import 1`] = ` +exports[`runs test with isolate modules async import 1`] = ` "Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total diff --git a/e2e/__tests__/isolateModulesAsync.test.ts b/e2e/__tests__/isolateModulesAsync.test.ts index 6825788852d1..6eb8e570225d 100644 --- a/e2e/__tests__/isolateModulesAsync.test.ts +++ b/e2e/__tests__/isolateModulesAsync.test.ts @@ -8,8 +8,8 @@ import {extractSummary} from '../Utils'; import runJest from '../runJest'; -test('runs test with async ESM import', () => { - const {exitCode, stderr} = runJest('async-esm-import', [], { +test('runs test with isolate modules async import', () => { + const {exitCode, stderr} = runJest('isolate-modules-async', [], { nodeOptions: '--experimental-vm-modules --no-warnings', }); diff --git a/e2e/isolate-modules-async/package.json b/e2e/isolate-modules-async/package.json index bb69b33c5159..8ae42c533cda 100644 --- a/e2e/isolate-modules-async/package.json +++ b/e2e/isolate-modules-async/package.json @@ -1,5 +1,5 @@ { - "name": "async-esm-import", + "name": "isolate-modules-async", "type": "module", "jest": { "transform": {}, From 3bbf1ba21a4adeb1860846c605c15edcd0b17551 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sat, 12 Aug 2023 14:44:24 +0800 Subject: [PATCH 10/11] fix: remove property --- packages/jest-runtime/src/index.ts | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 769379e5d410..d54ed63fb325 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -179,7 +179,6 @@ export default class Runtime { private readonly _moduleMockFactories: Map unknown>; private readonly _moduleMocker: ModuleMocker; private _isolatedModuleRegistry: ModuleRegistry | null; - private _isolatedESMModuleRegistry: ModuleRegistry | null; private _moduleRegistry: ModuleRegistry; private readonly _esmoduleRegistry: Map; private readonly _cjsNamedExports: Map>; @@ -243,7 +242,6 @@ export default class Runtime { this._moduleMocker = this._environment.moduleMocker; this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; - this._isolatedESMModuleRegistry = null; this._moduleRegistry = new Map(); this._esmoduleRegistry = new Map(); this._cjsNamedExports = new Map(); @@ -418,8 +416,8 @@ export default class Runtime { query = '', ): Promise { const cacheKey = modulePath + query; - const registry = this._isolatedESMModuleRegistry - ? this._isolatedESMModuleRegistry + const registry = this._isolatedModuleRegistry + ? this._isolatedModuleRegistry : this._esmoduleRegistry; if (this._fileTransformsMutex.has(cacheKey)) { @@ -568,8 +566,8 @@ export default class Runtime { return; } - const registry = this._isolatedESMModuleRegistry - ? this._isolatedESMModuleRegistry + const registry = this._isolatedModuleRegistry + ? this._isolatedModuleRegistry : this._esmoduleRegistry; if (specifier === '@jest/globals') { @@ -1173,38 +1171,29 @@ export default class Runtime { } async isolateModulesAsync(fn: () => Promise): Promise { - if ( - this._isolatedModuleRegistry || - this._isolatedMockRegistry || - this._isolatedESMModuleRegistry - ) { + if (this._isolatedModuleRegistry || this._isolatedMockRegistry) { throw new Error( 'isolateModulesAsync cannot be nested inside another isolateModulesAsync or isolateModules.', ); } this._isolatedModuleRegistry = new Map(); this._isolatedMockRegistry = new Map(); - this._isolatedESMModuleRegistry = new Map(); try { await fn(); } finally { // might be cleared within the callback this._isolatedModuleRegistry?.clear(); this._isolatedMockRegistry?.clear(); - this._isolatedESMModuleRegistry?.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; - this._isolatedESMModuleRegistry = null; } } resetModules(): void { this._isolatedModuleRegistry?.clear(); this._isolatedMockRegistry?.clear(); - this._isolatedESMModuleRegistry?.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; - this._isolatedESMModuleRegistry = null; this._mockRegistry.clear(); this._moduleRegistry.clear(); this._esmoduleRegistry.clear(); From 6f7d6892a991e37e22ba86379d76d70b55d76ec5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Aug 2023 14:07:25 +0200 Subject: [PATCH 11/11] move changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06a5d52f0793..a036a10eaa01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,6 @@ ### Fixes -- `[jest-runtime]` Fix dynamic esm import module bug when loaded module through `jest.isolateModulesAsync` ([14397](https://github.com/jestjs/jest/pull/14397)) - `[expect, @jest/expect-utils]` `ObjectContaining` support `sumbol` as key ([#14414](https://github.com/jestjs/jest/pull/14414)) - `[expect]` Remove `@types/node` from dependencies ([#14385](https://github.com/jestjs/jest/pull/14385)) - `[jest-core]` Use workers in watch mode by default to avoid crashes ([#14059](https://github.com/facebook/jest/pull/14059) & [#14085](https://github.com/facebook/jest/pull/14085)). @@ -12,6 +11,7 @@ - `[jest-mock]` Revert [#13692](https://github.com/jestjs/jest/pull/13692) as it was a breaking change ([#14429](https://github.com/jestjs/jest/pull/14429)) - `[jest-mock]` Revert [#13866](https://github.com/jestjs/jest/pull/13866) as it was a breaking change ([#14429](https://github.com/jestjs/jest/pull/14429)) - `[jest-mock]` Revert [#13867](https://github.com/jestjs/jest/pull/13867) as it was a breaking change ([#14429](https://github.com/jestjs/jest/pull/14429)) +- `[jest-runtime]` Fix dynamic esm import module bug when loaded module through `jest.isolateModulesAsync` ([14397](https://github.com/jestjs/jest/pull/14397)) ### Chore & Maintenance