Skip to content

Commit

Permalink
feat: print stack on preset normalize error
Browse files Browse the repository at this point in the history
  • Loading branch information
wtho committed Feb 19, 2019
1 parent 2f3d557 commit 962eb19
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `[expect]`: Improve report when matcher fails, part 7 ([#7866](https://github.com/facebook/jest/pull/7866))
- `[expect]`: Improve report when matcher fails, part 8 ([#7876](https://github.com/facebook/jest/pull/7876))
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))
- `[jest-config]` Print error information on preset normalizing error (pending)

### Fixes

Expand Down
39 changes: 38 additions & 1 deletion packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,10 @@ describe('preset', () => {
return '/node_modules/react-native/jest-preset.json';
}

if (name === 'react-native-js-preset/jest-preset') {
return '/node_modules/react-native-js-preset/jest-preset.js';
}

if (name === 'doesnt-exist') {
return null;
}
Expand All @@ -951,6 +955,15 @@ describe('preset', () => {
}),
{virtual: true},
);
jest.doMock(
'/node_modules/react-native-js-preset/jest-preset.js',
() => ({
moduleNameMapper: {
json: true,
},
}),
{virtual: true},
);
jest.mock(
'/node_modules/with-json-ext/jest-preset.json',
() => ({
Expand Down Expand Up @@ -1021,7 +1034,31 @@ describe('preset', () => {
},
{},
);
}).toThrowError(/Unexpected token }/);
}).toThrowError(
/Unexpected token } in JSON at position 104.*at JSON.parse/s,
);
});

test('throws when preset evaluation throws type error', () => {
jest.doMock(
'/node_modules/react-native-js-preset/jest-preset.js',
() => ({
transform: {}.nonExistingProp.call(),
}),
{virtual: true},
);

expect(() => {
normalize(
{
preset: 'react-native-js-preset',
rootDir: '/root/path/foo',
},
{},
);
}).toThrowError(
/TypeError: Cannot read property 'call' of undefined.*at Object.call/s,
);
});

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

Expand Down

0 comments on commit 962eb19

Please sign in to comment.