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 a374634
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 a374634

Please sign in to comment.