From d4260ba1dda910936c902ca0cabf121e3f929dfe Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 10 Feb 2022 10:39:55 +0100 Subject: [PATCH 1/2] feat: stop shipping `jsdom` by default --- CHANGELOG.md | 1 + packages/jest-config/package.json | 1 - packages/jest-config/src/ValidConfig.ts | 2 +- packages/jest-config/tsconfig.json | 1 - packages/jest-resolve/src/utils.ts | 28 +++++++++++++------ packages/jest-runner/package.json | 1 - packages/jest-runner/tsconfig.json | 1 - .../src/__tests__/fixtures/jestConfig.ts | 4 +-- yarn.lock | 4 +-- 9 files changed, 24 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 341e2533493d..b6e87eb374aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[expect]` [**BREAKING**] Migrate to ESM ([#12344](https://github.com/facebook/jest/pull/12344)) +- `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354)) - `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355)) - `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924)) - `[jest-environment-jsdom]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340)) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 1515632d8410..10713230db92 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -35,7 +35,6 @@ "glob": "^7.1.1", "graceful-fs": "^4.2.9", "jest-circus": "^27.5.1", - "jest-environment-jsdom": "^27.5.1", "jest-environment-node": "^27.5.1", "jest-get-type": "^27.5.1", "jest-regex-util": "^27.5.1", diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 8de7fb87c1c7..d8d4ff8bcb5f 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -113,7 +113,7 @@ const initialOptions: Config.InitialOptions = { snapshotFormat: PRETTY_FORMAT_DEFAULTS, snapshotResolver: '/snapshotResolver.js', snapshotSerializers: ['my-serializer-module'], - testEnvironment: 'jest-environment-jsdom', + testEnvironment: 'jest-environment-node', testEnvironmentOptions: {userAgent: 'Agent/007'}, testFailureExitCode: 1, testLocationInResults: false, diff --git a/packages/jest-config/tsconfig.json b/packages/jest-config/tsconfig.json index 47dcfb106c61..6ed9554e6476 100644 --- a/packages/jest-config/tsconfig.json +++ b/packages/jest-config/tsconfig.json @@ -10,7 +10,6 @@ // jest-test-sequencer, but that is just `require.resolve`d, so no real use // for their types "references": [ - {"path": "../jest-environment-jsdom"}, {"path": "../jest-environment-node"}, {"path": "../jest-get-type"}, {"path": "../jest-regex-util"}, diff --git a/packages/jest-resolve/src/utils.ts b/packages/jest-resolve/src/utils.ts index 045356edbe2c..fbbdf19598b8 100644 --- a/packages/jest-resolve/src/utils.ts +++ b/packages/jest-resolve/src/utils.ts @@ -101,15 +101,25 @@ export const resolveTestEnvironment = ({ rootDir: Config.Path; testEnvironment: string; requireResolveFunction: (moduleName: string) => string; -}): string => - resolveWithPrefix(undefined, { - filePath, - humanOptionName: 'Test environment', - optionName: 'testEnvironment', - prefix: 'jest-environment-', - requireResolveFunction, - rootDir, - }); +}): string => { + try { + return resolveWithPrefix(undefined, { + filePath, + humanOptionName: 'Test environment', + optionName: 'testEnvironment', + prefix: 'jest-environment-', + requireResolveFunction, + rootDir, + }); + } catch (error: any) { + if (filePath === 'jsdom' || 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.'; + } + + throw error; + } +}; /** * Finds the watch plugins to use: diff --git a/packages/jest-runner/package.json b/packages/jest-runner/package.json index f8307bbdf5e0..119482f17dd0 100644 --- a/packages/jest-runner/package.json +++ b/packages/jest-runner/package.json @@ -27,7 +27,6 @@ "emittery": "^0.8.1", "graceful-fs": "^4.2.9", "jest-docblock": "^27.5.1", - "jest-environment-jsdom": "^27.5.1", "jest-environment-node": "^27.5.1", "jest-haste-map": "^27.5.1", "jest-leak-detector": "^27.5.1", diff --git a/packages/jest-runner/tsconfig.json b/packages/jest-runner/tsconfig.json index 8646589e6cf8..7ce0efdb2782 100644 --- a/packages/jest-runner/tsconfig.json +++ b/packages/jest-runner/tsconfig.json @@ -10,7 +10,6 @@ {"path": "../jest-console"}, {"path": "../jest-docblock"}, {"path": "../jest-environment"}, - {"path": "../jest-environment-jsdom"}, {"path": "../jest-environment-node"}, {"path": "../jest-haste-map"}, {"path": "../jest-leak-detector"}, diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts b/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts index cff071ccb772..eac4e2aa322f 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts @@ -44,7 +44,7 @@ const defaultConfig = { restoreMocks: false, roots: [''], snapshotSerializers: [], - testEnvironment: 'jest-environment-jsdom', + testEnvironment: 'jest-environment-node', testPathIgnorePatterns: [NODE_MODULES_REGEXP], testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$', testResultsProcessor: null, @@ -106,7 +106,7 @@ const validConfig = { setupFilesAfterEnv: ['/testSetupFile.js'], silent: true, snapshotSerializers: ['my-serializer-module'], - testEnvironment: 'jest-environment-jsdom', + testEnvironment: 'jest-environment-node', testNamePattern: 'test signature', testPathIgnorePatterns: [NODE_MODULES_REGEXP], testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$', diff --git a/yarn.lock b/yarn.lock index 0809bc65a47b..9cb3c4a5ae06 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12790,7 +12790,6 @@ __metadata: glob: ^7.1.1 graceful-fs: ^4.2.9 jest-circus: ^27.5.1 - jest-environment-jsdom: ^27.5.1 jest-environment-node: ^27.5.1 jest-get-type: ^27.5.1 jest-regex-util: ^27.5.1 @@ -12860,7 +12859,7 @@ __metadata: languageName: unknown linkType: soft -"jest-environment-jsdom@^27.5.1, jest-environment-jsdom@workspace:packages/jest-environment-jsdom": +"jest-environment-jsdom@workspace:packages/jest-environment-jsdom": version: 0.0.0-use.local resolution: "jest-environment-jsdom@workspace:packages/jest-environment-jsdom" dependencies: @@ -13173,7 +13172,6 @@ __metadata: emittery: ^0.8.1 graceful-fs: ^4.2.9 jest-docblock: ^27.5.1 - jest-environment-jsdom: ^27.5.1 jest-environment-node: ^27.5.1 jest-haste-map: ^27.5.1 jest-jasmine2: ^27.5.1 From b48a162aa99ce11e110f047dedd6d4f330266e83 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 10 Feb 2022 11:08:17 +0100 Subject: [PATCH 2/2] pnp test should depend on jsdom --- scripts/verifyPnP.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/verifyPnP.js b/scripts/verifyPnP.js index f20a94452d05..a3504e6c59c0 100644 --- a/scripts/verifyPnP.js +++ b/scripts/verifyPnP.js @@ -34,6 +34,7 @@ try { { dependencies: { jest: '*', + 'jest-environment-jsdom': '*', }, name: 'test-pnp', }, @@ -42,7 +43,7 @@ try { ), ); fs.writeFileSync( - path.join(cwd, 'test.js'), + path.join(cwd, 'jsdom.test.js'), dedent` /* * @jest-environment jsdom @@ -53,6 +54,14 @@ try { }); `, ); + fs.writeFileSync( + path.join(cwd, 'node.test.js'), + dedent` + test('dummy', () => { + expect(typeof window).toBe('undefined'); + }); + `, + ); execa.sync('yarn', ['link', '--private', '--all', rootDirectory], { cwd, stdio: 'inherit',