From 362c6f8bf539e805365bec460875acf89c0e218f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 26 Apr 2022 11:01:03 +0200 Subject: [PATCH] fix: correctly throw an error if jest-environment-jsdom is missing --- CHANGELOG.md | 1 + packages/jest-resolve/src/utils.ts | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d0b68cddb7..bce19e24acb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[jest-circus]` Improve `test.concurrent` ([#12748](https://github.com/facebook/jest/pull/12748)) +- `[jest-resolve]` Correctly throw an error if jsdom env is used, but not installed ([#12749](https://github.com/facebook/jest/pull/12749)) ### Chore & Maintenance diff --git a/packages/jest-resolve/src/utils.ts b/packages/jest-resolve/src/utils.ts index e695343003a3..7a55cdd2c1a4 100644 --- a/packages/jest-resolve/src/utils.ts +++ b/packages/jest-resolve/src/utils.ts @@ -98,6 +98,11 @@ export const resolveTestEnvironment = ({ testEnvironment: string; requireResolveFunction: (moduleName: string) => string; }): string => { + // we don't want to resolve the actual `jsdom` env if `jest-environment-jsdom` is not installed + if (filePath === 'jsdom') { + filePath = 'jest-environment-jsdom'; + } + try { return resolveWithPrefix(undefined, { filePath, @@ -108,7 +113,7 @@ export const resolveTestEnvironment = ({ rootDir, }); } catch (error: any) { - if (filePath === 'jsdom' || filePath === 'jest-environment-jsdom') { + if (filePath === 'jest-environment-jsdom') { error.message += '\n\nAs of Jest 28 "jest-environment-jsdom" is no longer shipped by default, make sure to install it separately.'; }