Skip to content

Commit

Permalink
include error message for preset json (#4766)
Browse files Browse the repository at this point in the history
* include error message for preset json

* add test for invalid preset json

* update CHANGELOG
  • Loading branch information
xjlim authored and cpojer committed Oct 26, 2017
1 parent 0ea4c38 commit 165efcd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* `[jest-resolve]` changes method of determining builtin modules to include missing builtins ([#4740](https://github.com/facebook/jest/pull/4740))
* `[pretty-format]` Prevent error in pretty-format for window in jsdom test env ([#4750](https://github.com/facebook/jest/pull/4750))
* `[jest-resolve]` Preserve module identity for symlinks ([#4761](https://github.com/facebook/jest/pull/4761))
* `[jest-config]` Include error message for `preset` json ([#4766](https://github.com/facebook/jest/pull/4766))

### Features
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro
<yellow></>"
`;
exports[`preset throws when preset is invalid 1`] = `
"<red><bold><bold>● <bold>Validation Error</>:</>
<red></>
<red> Preset <bold>react-native</> is invalid:</>
<red> Unexpected token } in JSON at position 104</>
<red></>
<red> <bold>Configuration Documentation:</></>
<red> https://facebook.github.io/jest/docs/configuration.html</>
<red></>"
`;
exports[`preset throws when preset not found 1`] = `
"<red><bold><bold>● <bold>Validation Error</>:</>
<red></>
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-config/src/__tests__/jest-preset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"moduleNameMapper": {
"b": "b"
},
"modulePathIgnorePatterns": ["b"],
"setupFiles": ["b"],
}
16 changes: 16 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,22 @@ describe('preset', () => {
}).toThrowErrorMatchingSnapshot();
});

test('throws when preset is invalid', () => {
jest.mock('/node_modules/react-native/jest-preset.json', () =>
require.requireActual('./jest-preset.json'),
);

expect(() => {
normalize(
{
preset: 'react-native',
rootDir: '/root/path/foo',
},
{},
);
}).toThrowErrorMatchingSnapshot();
});

test('works with "react-native"', () => {
expect(() => {
normalize(
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ const setupPreset = (
// $FlowFixMe
preset = (require(presetModule): InitialOptions);
} catch (error) {
if (error instanceof SyntaxError) {
throw createConfigError(
` Preset ${chalk.bold(presetPath)} is invalid:\n ${error.message}`,
);
}
throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`);
}

Expand Down

0 comments on commit 165efcd

Please sign in to comment.