diff --git a/CHANGELOG.md b/CHANGELOG.md index 574f3c5ac7e7..c59e2dfe29b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `[babel-plugin-jest-hoist]` Add `BigInt` to `WHITELISTED_IDENTIFIERS` ([#8382](https://github.com/facebook/jest/pull/8382)) - `[babel-preset-jest]` Add `@babel/plugin-syntax-bigint` ([#8382](https://github.com/facebook/jest/pull/8382)) - `[expect]` Add `BigInt` support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382)) +- `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924)) - `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689)) - `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841)) - `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873)) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 3620be8a1603..9f085294e276 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1041,6 +1041,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 01e3999aff1d..fea4d8da24f1 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -108,19 +108,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) {