Skip to content

Commit 165efcd

Browse files
xjlimcpojer
authored andcommitted
include error message for preset json (#4766)
* include error message for preset json * add test for invalid preset json * update CHANGELOG
1 parent 0ea4c38 commit 165efcd

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* `[jest-resolve]` changes method of determining builtin modules to include missing builtins ([#4740](https://github.com/facebook/jest/pull/4740))
1717
* `[pretty-format]` Prevent error in pretty-format for window in jsdom test env ([#4750](https://github.com/facebook/jest/pull/4750))
1818
* `[jest-resolve]` Preserve module identity for symlinks ([#4761](https://github.com/facebook/jest/pull/4761))
19+
* `[jest-config]` Include error message for `preset` json ([#4766](https://github.com/facebook/jest/pull/4766))
1920

2021
### Features
2122
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))

packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro
1717
<yellow></>"
1818
`;
1919
20+
exports[`preset throws when preset is invalid 1`] = `
21+
"<red><bold><bold>● <bold>Validation Error</>:</>
22+
<red></>
23+
<red> Preset <bold>react-native</> is invalid:</>
24+
<red> Unexpected token } in JSON at position 104</>
25+
<red></>
26+
<red> <bold>Configuration Documentation:</></>
27+
<red> https://facebook.github.io/jest/docs/configuration.html</>
28+
<red></>"
29+
`;
30+
2031
exports[`preset throws when preset not found 1`] = `
2132
"<red><bold><bold>● <bold>Validation Error</>:</>
2233
<red></>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"moduleNameMapper": {
3+
"b": "b"
4+
},
5+
"modulePathIgnorePatterns": ["b"],
6+
"setupFiles": ["b"],
7+
}

packages/jest-config/src/__tests__/normalize.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,22 @@ describe('preset', () => {
877877
}).toThrowErrorMatchingSnapshot();
878878
});
879879

880+
test('throws when preset is invalid', () => {
881+
jest.mock('/node_modules/react-native/jest-preset.json', () =>
882+
require.requireActual('./jest-preset.json'),
883+
);
884+
885+
expect(() => {
886+
normalize(
887+
{
888+
preset: 'react-native',
889+
rootDir: '/root/path/foo',
890+
},
891+
{},
892+
);
893+
}).toThrowErrorMatchingSnapshot();
894+
});
895+
880896
test('works with "react-native"', () => {
881897
expect(() => {
882898
normalize(

packages/jest-config/src/normalize.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ const setupPreset = (
6464
// $FlowFixMe
6565
preset = (require(presetModule): InitialOptions);
6666
} catch (error) {
67+
if (error instanceof SyntaxError) {
68+
throw createConfigError(
69+
` Preset ${chalk.bold(presetPath)} is invalid:\n ${error.message}`,
70+
);
71+
}
6772
throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`);
6873
}
6974

0 commit comments

Comments
 (0)