Skip to content

Commit e5a84d9

Browse files
fix(jest-runtime): prevent global module registry from leaking into isolateModules registry (#10963)
1 parent b0f751e commit e5a84d9

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
3838
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
3939
- `[jest-runtime]` Fix stack overflow and promise deadlock when importing mutual dependant ES module ([#10892](https://github.com/facebook/jest/pull/10892))
40+
- `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963))
4041
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
4142
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
4243
- `[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))

packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,33 @@ describe('resetModules', () => {
218218
});
219219

220220
describe('isolateModules', () => {
221+
it("keeps it's registry isolated from global one", () =>
222+
createRuntime(__filename, {
223+
moduleNameMapper,
224+
}).then(runtime => {
225+
let exports;
226+
exports = runtime.requireModuleOrMock(
227+
runtime.__mockRootPath,
228+
'ModuleWithState',
229+
);
230+
exports.increment();
231+
expect(exports.getState()).toBe(2);
232+
233+
runtime.isolateModules(() => {
234+
exports = runtime.requireModuleOrMock(
235+
runtime.__mockRootPath,
236+
'ModuleWithState',
237+
);
238+
expect(exports.getState()).toBe(1);
239+
});
240+
241+
exports = runtime.requireModuleOrMock(
242+
runtime.__mockRootPath,
243+
'ModuleWithState',
244+
);
245+
expect(exports.getState()).toBe(2);
246+
}));
247+
221248
it('resets all modules after the block', () =>
222249
createRuntime(__filename, {
223250
moduleNameMapper,

packages/jest-runtime/src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,10 @@ export default class Runtime {
610610
if (options?.isInternalModule) {
611611
moduleRegistry = this._internalModuleRegistry;
612612
} else {
613-
if (
614-
this._moduleRegistry.get(modulePath) ||
615-
!this._isolatedModuleRegistry
616-
) {
617-
moduleRegistry = this._moduleRegistry;
618-
} else {
613+
if (this._isolatedModuleRegistry) {
619614
moduleRegistry = this._isolatedModuleRegistry;
615+
} else {
616+
moduleRegistry = this._moduleRegistry;
620617
}
621618
}
622619

0 commit comments

Comments
 (0)