Skip to content

Commit

Permalink
improve "preset not found" error message (#6863)
Browse files Browse the repository at this point in the history
* add a descriptive error message for case when preset module exist but doesn't have 'jest-preset.js' or 'jest-preset.json' files at the root

* add changelog entry
  • Loading branch information
ranyitz authored and SimenB committed Aug 18, 2018
1 parent 2fe109b commit 2a421eb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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
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

0 comments on commit 2a421eb

Please sign in to comment.