Skip to content

Commit dbeb5da

Browse files
authored
fix: Reset isolateModules after failing (#9541)
1 parent 4f62e93 commit dbeb5da

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499))
1212
- `[jest-resolve]` Fix module identity preservation with symlinks and browser field resolution ([#9511](https://github.com/facebook/jest/pull/9511))
1313
- `[jest-resolve]` Do not confuse directories with files ([#8912](https://github.com/facebook/jest/pull/8912))
14+
- `[jest-runtime]` Reset `isolateModules` if it fails ([#9541](https://github.com/facebook/jest/pull/9541))
1415
- `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451))
1516
- `[jest-snapshot]` Properly indent new snapshots in the presences of existing ones ([#9523](https://github.com/facebook/jest/pull/9523))
1617
- `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460))

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ describe('isolateModules', () => {
240240
expect(exports.getState()).toBe(1);
241241
}));
242242

243+
it('resets module after failing', () =>
244+
createRuntime(__filename, {
245+
moduleNameMapper,
246+
}).then(runtime => {
247+
expect(() =>
248+
runtime.isolateModules(() => {
249+
throw new Error('Error from isolated module');
250+
}),
251+
).toThrowError('Error from isolated module');
252+
253+
runtime.isolateModules(() => {
254+
expect(true).toBe(true);
255+
});
256+
}));
257+
243258
it('cannot nest isolateModules blocks', () =>
244259
createRuntime(__filename, {
245260
moduleNameMapper,

packages/jest-runtime/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,12 @@ class Runtime {
539539
}
540540
this._isolatedModuleRegistry = new Map();
541541
this._isolatedMockRegistry = new Map();
542-
fn();
543-
this._isolatedModuleRegistry = null;
544-
this._isolatedMockRegistry = null;
542+
try {
543+
fn();
544+
} finally {
545+
this._isolatedModuleRegistry = null;
546+
this._isolatedMockRegistry = null;
547+
}
545548
}
546549

547550
resetModules() {

0 commit comments

Comments
 (0)