From 982159f92c17b2216227e17a3212fa91c7f5a132 Mon Sep 17 00:00:00 2001 From: Andrea Carraro Date: Mon, 21 Dec 2020 11:44:19 +0100 Subject: [PATCH 1/2] fix(jest-runtime): prevent global module registry from leaking into isolated registry BREAKING CHANGE: require statements in isolateModules will always return a fresh instance of imported module --- CHANGELOG.md | 1 + .../runtime_require_module_or_mock.test.js | 27 +++++++++++++++++++ packages/jest-runtime/src/index.ts | 9 +++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d075aed3528..812d210a3a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ ### Fixes +- `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963)) - `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903)) - `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708)) - `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929)) diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js index 5eb2c31e89e0..73eb78ffae82 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js @@ -218,6 +218,33 @@ describe('resetModules', () => { }); describe('isolateModules', () => { + it("keeps it's registry isolated from global one", () => + createRuntime(__filename, { + moduleNameMapper, + }).then(runtime => { + let exports; + exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + exports.increment(); + expect(exports.getState()).toBe(2); + + runtime.isolateModules(() => { + exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + expect(exports.getState()).toBe(1); + }); + + exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + expect(exports.getState()).toBe(2); + })); + it('resets all modules after the block', () => createRuntime(__filename, { moduleNameMapper, diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 0b60667ccad8..a3f63c6f853f 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -610,13 +610,10 @@ export default class Runtime { if (options?.isInternalModule) { moduleRegistry = this._internalModuleRegistry; } else { - if ( - this._moduleRegistry.get(modulePath) || - !this._isolatedModuleRegistry - ) { - moduleRegistry = this._moduleRegistry; - } else { + if (this._isolatedModuleRegistry) { moduleRegistry = this._isolatedModuleRegistry; + } else { + moduleRegistry = this._moduleRegistry; } } From 428b040939da964b97b6a1ebbbabf9deeac0a40e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Dec 2020 13:09:14 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 812d210a3a17..57c0f6692aa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,6 @@ ### Fixes -- `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963)) - `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903)) - `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708)) - `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929)) @@ -38,6 +37,7 @@ - `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644)) - `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853)) - `[jest-runtime]` Fix stack overflow and promise deadlock when importing mutual dependant ES module ([#10892](https://github.com/facebook/jest/pull/10892)) +- `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963)) - `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749)) - `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753)) - `[jest-transform]` [**BREAKING**] Refactor API of transformers to pass an options bag rather than separate `config` and other options ([#10834](https://github.com/facebook/jest/pull/10834))