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

include error message for preset json #4766

Merged
merged 3 commits into from
Oct 26, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather flip it.

if (error.code === 'MODULE_NOT_FOUND') {
  throw createConfigError(`  Preset ${chalk.bold(presetPath)} not found.`);
}

throw createConfigError(
  `  Unable to load preset ${chalk.bold(presetPath)}:\n  ${error.message}`,
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB I am getting a different error for that (AssertionError for the actual require), the error code will only be shown in test (MODULE_NOT_FOUND from jest-resolve).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting. Thanks for checking!

throw createConfigError(
` Preset ${chalk.bold(presetPath)} is invalid:\n ${error.message}`,
);
}
throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`);
}

Expand Down