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

[jest-config] Changed "Preset ... not found" validation error #8924

Merged
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 @@ -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
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
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