Skip to content

Commit

Permalink
fix(jest-runtime): prevent global module registry from leaking into i…
Browse files Browse the repository at this point in the history
…solated registry

BREAKING CHANGE:
require statements in isolateModules will always return a fresh instance of imported module
  • Loading branch information
toomuchdesign committed Dec 21, 2020
1 parent b0f751e commit 8e9fc15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 8e9fc15

Please sign in to comment.