Skip to content

Commit

Permalink
fix(jest-config): throw correct error for missing preset modules
Browse files Browse the repository at this point in the history
When loading a preset module that cannot be resolved (e.g. is not npm
installed), the following error is shown to users:

    ● Validation Error:

      Preset foobar is invalid:

      The "id" argument must be of type string. Received null

Instead, the following error should be shown

    ● Validation Error:

      Preset foobar not found.

This PR changes the error message by handling when
Resolver.findNodeModule returns null, indicating that the module cannot
be successfully imported.
  • Loading branch information
terite committed Oct 30, 2020
1 parent 0eee946 commit 0c52b10
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
### Fixes

- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))
- `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737))

### Chore & Maintenance

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 (/doesnt-exist/.test(name)) {
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?
preset = require(presetModule);
} catch (error) {
if (error instanceof SyntaxError || error instanceof TypeError) {
Expand Down

0 comments on commit 0c52b10

Please sign in to comment.