Skip to content

Commit 59f42d8

Browse files
authored
fix: do not cache modules that throw during evaluation (#11263)
1 parent 57e32e9 commit 59f42d8

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
- `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963))
9494
- `[jest-runtime]` Refactor to prevent race condition when linking and evaluating ES Modules ([#11150](https://github.com/facebook/jest/pull/11150))
9595
- `[jest-runtime]` Throw correct error when attempting to load ESM via `require` ([#11260](https://github.com/facebook/jest/pull/11260))
96+
- `[jest-runtime]` Do not cache modules that throw during evaluation ([#11263](https://github.com/facebook/jest/pull/11263))
9697
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
9798
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
9899
- `[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.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ describe('Runtime requireModule', () => {
349349
expect(Module1).toBe(Module2);
350350
});
351351

352+
it('does not cache modules that throw during evaluation', async () => {
353+
const runtime = await createRuntime(__filename);
354+
expect(() =>
355+
runtime.requireModule(runtime.__mockRootPath, 'throwing'),
356+
).toThrowError();
357+
expect(() =>
358+
runtime.requireModule(runtime.__mockRootPath, 'throwing'),
359+
).toThrowError();
360+
});
361+
352362
onNodeVersions('>=12.12.0', () => {
353363
it('overrides module.createRequire', async () => {
354364
const runtime = await createRuntime(__filename);

packages/jest-runtime/src/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,14 +692,19 @@ export default class Runtime {
692692
};
693693
moduleRegistry.set(modulePath, localModule);
694694

695-
this._loadModule(
696-
localModule,
697-
from,
698-
moduleName,
699-
modulePath,
700-
options,
701-
moduleRegistry,
702-
);
695+
try {
696+
this._loadModule(
697+
localModule,
698+
from,
699+
moduleName,
700+
modulePath,
701+
options,
702+
moduleRegistry,
703+
);
704+
} catch (error: unknown) {
705+
moduleRegistry.delete(modulePath);
706+
throw error;
707+
}
703708

704709
return localModule.exports;
705710
}

0 commit comments

Comments
 (0)