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(jest-config): throw correct error for missing preset modules #10737

Merged
merged 5 commits into from Oct 31, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))
- `[jest-circus, jest-jasmine2]` fix: don't assume `stack` is always a string ([#10697](https://github.com/facebook/jest/pull/10697))
- `[jest-config]` Fix bug introduced in watch mode by PR [#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692))
- `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737))
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))
- `[jest-runtime]` Handle file URLs in dynamic imports ([#10744](https://github.com/facebook/jest/pull/10744))

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/__tests__/normalize.test.js
Expand Up @@ -954,7 +954,7 @@ describe('preset', () => {
return '/node_modules/react-native-js-preset/jest-preset.js';
}

if (name === 'doesnt-exist') {
if (name.includes('doesnt-exist')) {
return null;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -133,14 +133,15 @@ const setupPreset = (
);

try {
if (!presetModule) {
throw new Error(`Cannot find module '${presetPath}'`);
}

// Force re-evaluation to support multiple projects
try {
if (presetModule) {
delete require.cache[require.resolve(presetModule)];
}
delete require.cache[require.resolve(presetModule)];
} catch {}

// @ts-expect-error: `presetModule` can be null?
Copy link
Member

Choose a reason for hiding this comment

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

good stuff! 👍

preset = require(presetModule);
} catch (error) {
if (error instanceof SyntaxError || error instanceof TypeError) {
Expand Down