diff --git a/CHANGELOG.md b/CHANGELOG.md index f30c724de98c..ee22740c1367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711)) - `[jest-circus, jest-jasmine2]` fix: don't assume `stack` is always a string ([#10697](https://github.com/facebook/jest/pull/10697)) - `[jest-config]` Fix bug introduced in watch mode by PR [#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692)) +- `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737)) - `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713)) - `[jest-runtime]` Handle file URLs in dynamic imports ([#10744](https://github.com/facebook/jest/pull/10744)) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 89ee1bdb2daa..efd348a2bc48 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -954,7 +954,7 @@ describe('preset', () => { return '/node_modules/react-native-js-preset/jest-preset.js'; } - if (name === 'doesnt-exist') { + if (name.includes('doesnt-exist')) { return null; } diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index e5598f5a29ff..ce1915d33daf 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -133,14 +133,15 @@ const setupPreset = ( ); try { + if (!presetModule) { + throw new Error(`Cannot find module '${presetPath}'`); + } + // Force re-evaluation to support multiple projects try { - if (presetModule) { - delete require.cache[require.resolve(presetModule)]; - } + delete require.cache[require.resolve(presetModule)]; } catch {} - // @ts-expect-error: `presetModule` can be null? preset = require(presetModule); } catch (error) { if (error instanceof SyntaxError || error instanceof TypeError) {