From 59f42d86756fcc3c9caf0c8a5cf1b13324941d4e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 20 May 2021 15:30:17 +0200 Subject: [PATCH] fix: do not cache modules that throw during evaluation (#11263) --- CHANGELOG.md | 1 + .../__tests__/runtime_require_module.test.js | 10 +++++++++ packages/jest-runtime/src/index.ts | 21 ++++++++++++------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb8daded58bc..0585c1ec3104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ - `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963)) - `[jest-runtime]` Refactor to prevent race condition when linking and evaluating ES Modules ([#11150](https://github.com/facebook/jest/pull/11150)) - `[jest-runtime]` Throw correct error when attempting to load ESM via `require` ([#11260](https://github.com/facebook/jest/pull/11260)) +- `[jest-runtime]` Do not cache modules that throw during evaluation ([#11263](https://github.com/facebook/jest/pull/11263)) - `[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)) diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js index 453373c895d8..937d1bc14350 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -349,6 +349,16 @@ describe('Runtime requireModule', () => { expect(Module1).toBe(Module2); }); + it('does not cache modules that throw during evaluation', async () => { + const runtime = await createRuntime(__filename); + expect(() => + runtime.requireModule(runtime.__mockRootPath, 'throwing'), + ).toThrowError(); + expect(() => + runtime.requireModule(runtime.__mockRootPath, 'throwing'), + ).toThrowError(); + }); + onNodeVersions('>=12.12.0', () => { it('overrides module.createRequire', async () => { const runtime = await createRuntime(__filename); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index e3de7866eae9..cbe95fd67bde 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -692,14 +692,19 @@ export default class Runtime { }; moduleRegistry.set(modulePath, localModule); - this._loadModule( - localModule, - from, - moduleName, - modulePath, - options, - moduleRegistry, - ); + try { + this._loadModule( + localModule, + from, + moduleName, + modulePath, + options, + moduleRegistry, + ); + } catch (error: unknown) { + moduleRegistry.delete(modulePath); + throw error; + } return localModule.exports; }