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

improve "preset not found" error message #6863

Merged
merged 2 commits into from
Aug 18, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `[jest-cli]` Fix incorrect `testEnvironmentOptions` warning ([#6852](https://github.com/facebook/jest/pull/6852))
- `[jest-each`] Prevent done callback being supplied to describe ([#6843](https://github.com/facebook/jest/pull/6843))
- `[jest-config`] Better error message for a case when a preset module was found, but no `jest-preset.js` or `jest-preset.json` at the root ([#6863](https://github.com/facebook/jest/pull/6863))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro
<yellow></>"
`;

exports[`preset throws when module was found but no "jest-preset.js" or "jest-preset.json" files 1`] = `
"<red><bold><bold>● <bold>Validation Error</>:</>
<red></>
<red> Module <bold>exist-but-no-jest-preset</> should have \\"jest-preset.js\\" or \\"jest-preset.json\\" file at the root.</>
<red></>
<red> <bold>Configuration Documentation:</></>
<red> https://jestjs.io/docs/configuration.html</>
<red></>"
`;

exports[`preset throws when preset not found 1`] = `
"<red><bold><bold>● <bold>Validation Error</>:</>
<red></>
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,11 @@ describe('preset', () => {
if (name === 'react-native/jest-preset') {
return '/node_modules/react-native/jest-preset.json';
}

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

return '/node_modules/' + name;
});
jest.doMock(
Expand Down Expand Up @@ -915,6 +920,15 @@ describe('preset', () => {
}),
{virtual: true},
);
jest.mock(
'/node_modules/exist-but-no-jest-preset/index.js',
() => ({
moduleNameMapper: {
js: true,
},
}),
{virtual: true},
);
});

afterEach(() => {
Expand All @@ -933,6 +947,18 @@ describe('preset', () => {
}).toThrowErrorMatchingSnapshot();
});

test('throws when module was found but no "jest-preset.js" or "jest-preset.json" files', () => {
expect(() => {
normalize(
{
preset: 'exist-but-no-jest-preset',
rootDir: '/root/path/foo',
},
{},
);
}).toThrowErrorMatchingSnapshot();
});

test('throws when preset is invalid', () => {
jest.doMock('/node_modules/react-native/jest-preset.json', () =>
require.requireActual('./jest-preset.json'),
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ const setupPreset = (
` Preset ${chalk.bold(presetPath)} is invalid:\n ${error.message}`,
);
}

const preset = Resolver.findNodeModule(presetPath, {
basedir: options.rootDir,
});

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.`);
}

Expand Down