From 0c52b10c642c52e965e34e96b3572e863f863e80 Mon Sep 17 00:00:00 2001 From: David Stensland Date: Thu, 29 Oct 2020 20:12:09 -0700 Subject: [PATCH 1/3] fix(jest-config): throw correct error for missing preset modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When loading a preset module that cannot be resolved (e.g. is not npm installed), the following error is shown to users: ● Validation Error: Preset foobar is invalid: The "id" argument must be of type string. Received null Instead, the following error should be shown ● Validation Error: Preset foobar not found. This PR changes the error message by handling when Resolver.findNodeModule returns null, indicating that the module cannot be successfully imported. --- CHANGELOG.md | 1 + packages/jest-config/src/__tests__/normalize.test.js | 2 +- packages/jest-config/src/normalize.ts | 9 +++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc4c3c1f8bde..80834a912af0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711)) +- `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737)) ### Chore & Maintenance diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 89ee1bdb2daa..4287519acc5d 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 (/doesnt-exist/.test(name)) { return null; } diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 5d60d45c2053..8e23db998984 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) { From ca8c88b663e12ffb0c338820ab2d89bcf5053ece Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 31 Oct 2020 00:57:30 +0100 Subject: [PATCH 2/3] Update packages/jest-config/src/__tests__/normalize.test.js --- packages/jest-config/src/__tests__/normalize.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 4287519acc5d..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 (/doesnt-exist/.test(name)) { + if (name.includes('doesnt-exist')) { return null; } From 2012e7edd3b57843ceeea50065b779f216057ed4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 31 Oct 2020 00:58:32 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb40e0c20c80..ee22740c1367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,10 @@ ### Fixes - `[babel-plugin-jest-hoist]` Preserve order of hoisted mock nodes within containing block ([#10536](https://github.com/facebook/jest/pull/10536)) -- `[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)) - `[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))