Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: print error msg & stack on preset normalization error #7935

Merged
merged 1 commit into from Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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 normalization error ([#7935](https://github.com/facebook/jest/pull/7935))

### Fixes

Expand Down
37 changes: 36 additions & 1 deletion packages/jest-config/src/__tests__/normalize.test.js
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,29 @@ describe('preset', () => {
},
{},
);
}).toThrowError(/Unexpected token }/);
}).toThrowError(/Unexpected token } in JSON at position 104[\s\S]* at /);
});

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[\s\S]* at /,
);
});

test('works with "react-native"', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-config/src/normalize.js
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