Skip to content

Commit

Permalink
fix: do not cache modules that throw during evaluation (#11263)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 20, 2021
1 parent 57e32e9 commit 59f42d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_require_module.test.js
Expand Up @@ -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);
Expand Down
21 changes: 13 additions & 8 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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;
}
Expand Down

0 comments on commit 59f42d8

Please sign in to comment.