Skip to content

Commit

Permalink
[jest-config] Changed "Preset ... not found" validation error (#8924)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon authored and SimenB committed Sep 7, 2019
1 parent 116303b commit 0df9981
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- `[babel-plugin-jest-hoist]` Add `BigInt` to `WHITELISTED_IDENTIFIERS` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[babel-preset-jest]` Add `@babel/plugin-syntax-bigint` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[expect]` Add `BigInt` support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924))
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
Expand Down
23 changes: 23 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,29 @@ describe('preset', () => {
}).toThrowErrorMatchingSnapshot();
});

test('throws when a dependency is missing in the preset', () => {
jest.doMock(
'/node_modules/react-native-js-preset/jest-preset.js',
() => {
require('library-that-is-not-installed');
return {
transform: {},
};
},
{virtual: true},
);

expect(() => {
normalize(
{
preset: 'react-native-js-preset',
rootDir: '/root/path/foo',
},
{},
);
}).toThrowError(/Cannot find module 'library-that-is-not-installed'/);
});

test('throws when preset is invalid', () => {
jest.doMock('/node_modules/react-native/jest-preset.json', () =>
jest.requireActual('./jest-preset.json'),
Expand Down
32 changes: 24 additions & 8 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,35 @@ const setupPreset = (
);
}

const preset = Resolver.findNodeModule(presetPath, {
basedir: options.rootDir,
});
if (error.message.includes('Cannot find module')) {
if (error.message.includes(presetPath)) {
const preset = Resolver.findNodeModule(presetPath, {
basedir: options.rootDir,
});

if (preset) {
if (preset) {
throw createConfigError(
` Module ${chalk.bold(
presetPath,
)} should have "jest-preset.js" or "jest-preset.json" file at the root.`,
);
}
throw createConfigError(
` Preset ${chalk.bold(presetPath)} not found.`,
);
}
throw createConfigError(
` Module ${chalk.bold(
presetPath,
)} should have "jest-preset.js" or "jest-preset.json" file at the root.`,
` Missing dependency in ${chalk.bold(presetPath)}:\n\n ${
error.message
}\n ${error.stack}`,
);
}

throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`);
throw createConfigError(
` An unknown error occurred in ${chalk.bold(presetPath)}:\n\n ${
error.message
}\n ${error.stack}`,
);
}

if (options.setupFiles) {
Expand Down

0 comments on commit 0df9981

Please sign in to comment.