From 0aa70abd885a93fcd2759afd80a0652320093347 Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Fri, 6 Sep 2019 22:36:15 -0700 Subject: [PATCH] [jest-config] Changed "Preset ... not found" validation error (#8924) --- .../src/__tests__/normalize.test.js | 23 +++++++++++++ packages/jest-config/src/normalize.ts | 32 ++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 9ccb14ddb43a..2f3cb51d6557 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1021,6 +1021,29 @@ describe('preset', () => { }).toThrowErrorMatchingSnapshot(); }); + test('throws when a dependency is missing in the preset', () => { + jest.doMock( + '/node_modules/react-native-js-preset/jest-preset.js', + () => { + require('library-that-is-not-installed'); + return { + transform: {}, + }; + }, + {virtual: true}, + ); + + expect(() => { + normalize( + { + preset: 'react-native-js-preset', + rootDir: '/root/path/foo', + }, + {}, + ); + }).toThrowError(/Cannot find module 'library-that-is-not-installed'/); + }); + test('throws when preset is invalid', () => { jest.doMock('/node_modules/react-native/jest-preset.json', () => jest.requireActual('./jest-preset.json'), diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index ed8940cc3d57..45ec83eb7e6e 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -97,19 +97,35 @@ const setupPreset = ( ); } - const preset = Resolver.findNodeModule(presetPath, { - basedir: options.rootDir, - }); + if (error.message.includes('Cannot find module')) { + if (error.message.includes(presetPath)) { + const preset = Resolver.findNodeModule(presetPath, { + basedir: options.rootDir, + }); - if (preset) { + if (preset) { + throw createConfigError( + ` Module ${chalk.bold( + presetPath, + )} should have "jest-preset.js" or "jest-preset.json" file at the root.`, + ); + } + throw createConfigError( + ` Preset ${chalk.bold(presetPath)} not found.`, + ); + } throw createConfigError( - ` Module ${chalk.bold( - presetPath, - )} should have "jest-preset.js" or "jest-preset.json" file at the root.`, + ` Missing dependency in ${chalk.bold(presetPath)}:\n\n ${ + error.message + }\n ${error.stack}`, ); } - throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`); + throw createConfigError( + ` An unknown error occurred in ${chalk.bold(presetPath)}:\n\n ${ + error.message + }\n ${error.stack}`, + ); } if (options.setupFiles) {