Skip to content

Commit e4ec36b

Browse files
committed
feat: print stack on preset normalize error
1 parent 2f3d557 commit e4ec36b

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `[expect]`: Improve report when matcher fails, part 7 ([#7866](https://github.com/facebook/jest/pull/7866))
66
- `[expect]`: Improve report when matcher fails, part 8 ([#7876](https://github.com/facebook/jest/pull/7876))
77
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))
8+
- `[jest-config]` Print error information on preset normalization error ([#7935](https://github.com/facebook/jest/pull/7935))
89

910
### Fixes
1011

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,10 @@ describe('preset', () => {
934934
return '/node_modules/react-native/jest-preset.json';
935935
}
936936

937+
if (name === 'react-native-js-preset/jest-preset') {
938+
return '/node_modules/react-native-js-preset/jest-preset.js';
939+
}
940+
937941
if (name === 'doesnt-exist') {
938942
return null;
939943
}
@@ -951,6 +955,15 @@ describe('preset', () => {
951955
}),
952956
{virtual: true},
953957
);
958+
jest.doMock(
959+
'/node_modules/react-native-js-preset/jest-preset.js',
960+
() => ({
961+
moduleNameMapper: {
962+
json: true,
963+
},
964+
}),
965+
{virtual: true},
966+
);
954967
jest.mock(
955968
'/node_modules/with-json-ext/jest-preset.json',
956969
() => ({
@@ -1021,7 +1034,31 @@ describe('preset', () => {
10211034
},
10221035
{},
10231036
);
1024-
}).toThrowError(/Unexpected token }/);
1037+
}).toThrowError(
1038+
/Unexpected token } in JSON at position 104[\s\S]*at JSON.parse/,
1039+
);
1040+
});
1041+
1042+
test('throws when preset evaluation throws type error', () => {
1043+
jest.doMock(
1044+
'/node_modules/react-native-js-preset/jest-preset.js',
1045+
() => ({
1046+
transform: {}.nonExistingProp.call(),
1047+
}),
1048+
{virtual: true},
1049+
);
1050+
1051+
expect(() => {
1052+
normalize(
1053+
{
1054+
preset: 'react-native-js-preset',
1055+
rootDir: '/root/path/foo',
1056+
},
1057+
{},
1058+
);
1059+
}).toThrowError(
1060+
/TypeError: Cannot read property 'call' of undefined[\s\S]*at Object.call/,
1061+
);
10251062
});
10261063

10271064
test('works with "react-native"', () => {

packages/jest-config/src/normalize.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ const setupPreset = (
9595
// $FlowFixMe
9696
preset = (require(presetModule): InitialOptions);
9797
} catch (error) {
98-
if (error instanceof SyntaxError) {
98+
if (error instanceof SyntaxError || error instanceof TypeError) {
9999
throw createConfigError(
100-
` Preset ${chalk.bold(presetPath)} is invalid:\n ${error.message}`,
100+
` Preset ${chalk.bold(presetPath)} is invalid:\n\n ${
101+
error.message
102+
}\n ${error.stack}`,
101103
);
102104
}
103105

0 commit comments

Comments
 (0)