Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not cache modules that throw during evaluation #11263

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -688,14 +688,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we do the same for mockRegistry?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point! we should 😀

Copy link
Member Author

@SimenB SimenB May 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already do actually

https://github.com/facebook/jest/blob/57e32e99a7994dfdcca8f777a2961294919551b9/packages/jest-runtime/src/index.ts#L803-L812

or rather, we don't cache until it's loaded, so no need to clear it

throw error;
}

return localModule.exports;
}
Expand Down