From a8e997a55142efcee5b9a26ac82dd3caf4493b5a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 10 Feb 2022 11:48:13 +0100 Subject: [PATCH] feat: stop shipping `jsdom` by default (#12354) --- 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 +-- scripts/verifyPnP.js | 11 +++++++- yarn.lock | 4 +-- 10 files changed, 34 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfbb919435ce..5606f64e920b 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**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290)) - `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924)) 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/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', diff --git a/yarn.lock b/yarn.lock index 2845a61a3515..75c87c712bbb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12819,7 +12819,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 @@ -12889,7 +12888,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: @@ -13202,7 +13201,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