diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d21a5ffb299..e3376f7ee7dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499)) - `[jest-resolve]` Fix module identity preservation with symlinks and browser field resolution ([#9511](https://github.com/facebook/jest/pull/9511)) - `[jest-resolve]` Do not confuse directories with files ([#8912](https://github.com/facebook/jest/pull/8912)) +- `[jest-runtime]` Reset `isolateModules` if it fails ([#9541](https://github.com/facebook/jest/pull/9541)) - `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451)) - `[jest-snapshot]` Properly indent new snapshots in the presences of existing ones ([#9523](https://github.com/facebook/jest/pull/9523)) - `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460)) 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 a92f4a31769b..5eb2c31e89e0 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 @@ -240,6 +240,21 @@ describe('isolateModules', () => { expect(exports.getState()).toBe(1); })); + it('resets module after failing', () => + createRuntime(__filename, { + moduleNameMapper, + }).then(runtime => { + expect(() => + runtime.isolateModules(() => { + throw new Error('Error from isolated module'); + }), + ).toThrowError('Error from isolated module'); + + runtime.isolateModules(() => { + expect(true).toBe(true); + }); + })); + it('cannot nest isolateModules blocks', () => createRuntime(__filename, { moduleNameMapper, diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index cb74daf324af..16f5f3774a5e 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -539,9 +539,12 @@ class Runtime { } this._isolatedModuleRegistry = new Map(); this._isolatedMockRegistry = new Map(); - fn(); - this._isolatedModuleRegistry = null; - this._isolatedMockRegistry = null; + try { + fn(); + } finally { + this._isolatedModuleRegistry = null; + this._isolatedMockRegistry = null; + } } resetModules() {