From 02be885726f4a33beb4723057eaaf6eb6e57ef74 Mon Sep 17 00:00:00 2001 From: Joe Lim Date: Thu, 26 Oct 2017 03:22:45 -0700 Subject: [PATCH 1/3] include error message for preset json --- packages/jest-config/src/normalize.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index fc09f87e5cee..f12e7d20e9c1 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -64,6 +64,11 @@ const setupPreset = ( // $FlowFixMe preset = (require(presetModule): InitialOptions); } catch (error) { + if (error instanceof SyntaxError) { + throw createConfigError( + ` Preset ${chalk.bold(presetPath)} is invalid:\n ${error.message}`, + ); + } throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`); } From 4d2fcf6ea671aedefa1d9f8bfd57f8380f096adb Mon Sep 17 00:00:00 2001 From: Joe Lim Date: Thu, 26 Oct 2017 06:00:38 -0700 Subject: [PATCH 2/3] add test for invalid preset json --- .../__snapshots__/normalize.test.js.snap | 11 +++++++++++ .../jest-config/src/__tests__/jest-preset.json | 7 +++++++ .../jest-config/src/__tests__/normalize.test.js | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 packages/jest-config/src/__tests__/jest-preset.json diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap index 73a07001f27f..9a96aa40291e 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap @@ -17,6 +17,17 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro " `; +exports[`preset throws when preset is invalid 1`] = ` +"Validation Error: + + Preset react-native is invalid: + Unexpected token } in JSON at position 104 + + Configuration Documentation: + https://facebook.github.io/jest/docs/configuration.html +" +`; + exports[`preset throws when preset not found 1`] = ` "Validation Error: diff --git a/packages/jest-config/src/__tests__/jest-preset.json b/packages/jest-config/src/__tests__/jest-preset.json new file mode 100644 index 000000000000..bfa7919fac94 --- /dev/null +++ b/packages/jest-config/src/__tests__/jest-preset.json @@ -0,0 +1,7 @@ +{ + "moduleNameMapper": { + "b": "b" + }, + "modulePathIgnorePatterns": ["b"], + "setupFiles": ["b"], +} diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index c654d4b03f87..48181c615873 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -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( From 2178235aaabb961283a3aa78e6a385513269f4d2 Mon Sep 17 00:00:00 2001 From: Joe Lim Date: Thu, 26 Oct 2017 06:10:00 -0700 Subject: [PATCH 3/3] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33808e0697bd..904813910622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))