diff --git a/CHANGELOG.md b/CHANGELOG.md index a42439c24887..c604cb5535ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874)) + ### Fixes ### Chore & Maintenance diff --git a/docs/Configuration.md b/docs/Configuration.md index c34bebedfcdf..585910460707 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -989,9 +989,9 @@ More about serializers API can be found [here](https://github.com/facebook/jest/ ### `testEnvironment` [string] -Default: `"jsdom"` +Default: `"node"` -The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/jsdom/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead. +The test environment that will be used for testing. The default environment in Jest is a Node.js environment. If you are building a web app, you can use a browser-like environment through [`jsdom`](https://github.com/jsdom/jsdom) instead. By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file: @@ -1071,7 +1071,7 @@ beforeAll(() => { Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. ### `testFailureExitCode` [number] diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 7fc1128c1c9e..9e24197f7ab0 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -51,7 +51,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` "skipFilter": false, "slowTestThreshold": 5, "snapshotSerializers": [], - "testEnvironment": "<>/jest-environment-jsdom/build/index.js", + "testEnvironment": "<>/jest-environment-node/build/index.js", "testEnvironmentOptions": {}, "testLocationInResults": false, "testMatch": [ diff --git a/e2e/__tests__/config.test.ts b/e2e/__tests__/config.test.ts index 6d0d0cc57e32..111d6e73a1a4 100644 --- a/e2e/__tests__/config.test.ts +++ b/e2e/__tests__/config.test.ts @@ -59,6 +59,7 @@ test('works with jsdom testEnvironmentOptions config JSON', () => { const result = runJest('environmentOptions', [ '--config=' + JSON.stringify({ + testEnvironment: 'jsdom', testEnvironmentOptions: { url: 'https://jestjs.io', }, diff --git a/e2e/dom-diffing/package.json b/e2e/dom-diffing/package.json index 586d4ca6b75c..0ded940b7cb7 100644 --- a/e2e/dom-diffing/package.json +++ b/e2e/dom-diffing/package.json @@ -1,3 +1,5 @@ { - "jest": {} + "jest": { + "testEnvironment": "jsdom" + } } diff --git a/examples/angular/jest.config.js b/examples/angular/jest.config.js index e6cbd08cca2f..06e6c25e3075 100644 --- a/examples/angular/jest.config.js +++ b/examples/angular/jest.config.js @@ -1,6 +1,7 @@ module.exports = { moduleFileExtensions: ['ts', 'html', 'js', 'json'], setupFilesAfterEnv: ['/setupJest.js'], + testEnvironment: 'jsdom', transform: { '\\.[tj]s$': ['babel-jest', {configFile: require.resolve('./.babelrc')}], }, diff --git a/examples/jquery/package.json b/examples/jquery/package.json index ed417ac49138..87a6963ee511 100644 --- a/examples/jquery/package.json +++ b/examples/jquery/package.json @@ -13,5 +13,8 @@ }, "scripts": { "test": "jest" + }, + "jest": { + "testEnvironment": "jsdom" } } diff --git a/examples/react-testing-library/package.json b/examples/react-testing-library/package.json index 6f5ca6db0820..642e702792b4 100644 --- a/examples/react-testing-library/package.json +++ b/examples/react-testing-library/package.json @@ -17,5 +17,8 @@ }, "scripts": { "test": "jest" + }, + "jest": { + "testEnvironment": "jsdom" } } diff --git a/examples/react/package.json b/examples/react/package.json index c3752e4dbc0f..97af358bacb4 100644 --- a/examples/react/package.json +++ b/examples/react/package.json @@ -16,5 +16,8 @@ }, "scripts": { "test": "jest" + }, + "jest": { + "testEnvironment": "jsdom" } } diff --git a/examples/typescript/package.json b/examples/typescript/package.json index f5e65eaad005..8a2c828a89de 100644 --- a/examples/typescript/package.json +++ b/examples/typescript/package.json @@ -18,5 +18,8 @@ }, "scripts": { "test": "jest" + }, + "jest": { + "testEnvironment": "jsdom" } } diff --git a/jest.config.js b/jest.config.js index dcad9d20336a..d6d551dd506d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -33,7 +33,6 @@ module.exports = { '/packages/pretty-format/build/plugins/ConvertAnsi.js', require.resolve('jest-snapshot-serializer-raw'), ], - testEnvironment: './packages/jest-environment-node', testPathIgnorePatterns: [ '/test-types/', '/__arbitraries__/', diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap index e90ed19aae3e..9656efe20bf3 100644 --- a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap @@ -255,7 +255,7 @@ module.exports = { // snapshotSerializers: [], // The test environment that will be used for testing - // testEnvironment: \\"jest-environment-jsdom\\", + // testEnvironment: \\"jest-environment-node\\", // Options that will be passed to the testEnvironment // testEnvironmentOptions: {}, diff --git a/packages/jest-cli/src/init/__tests__/init.test.js b/packages/jest-cli/src/init/__tests__/init.test.js index 7e0ba3beb9c2..40a3af3dc18f 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -124,8 +124,7 @@ describe('init', () => { const writtenJestConfig = fs.writeFileSync.mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig); - // should modify when the default environment will be changed to "node" - expect(evaluatedConfig).toEqual({}); + expect(evaluatedConfig).toEqual({testEnvironment: 'jsdom'}); }); it('should create configuration for {environment: "node"}', async () => { @@ -135,8 +134,7 @@ describe('init', () => { const writtenJestConfig = fs.writeFileSync.mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig); - // should modify when the default environment will be changed to "node" - expect(evaluatedConfig).toEqual({testEnvironment: 'node'}); + expect(evaluatedConfig).toEqual({}); }); it('should create package.json with configured test command when {scripts: true}', async () => { diff --git a/packages/jest-cli/src/init/generateConfigFile.ts b/packages/jest-cli/src/init/generateConfigFile.ts index a7fdff6e277a..27ca1023b2b1 100644 --- a/packages/jest-cli/src/init/generateConfigFile.ts +++ b/packages/jest-cli/src/init/generateConfigFile.ts @@ -57,9 +57,9 @@ const generateConfigFile = ( }); } - if (environment === 'node') { + if (environment === 'jsdom') { Object.assign(overrides, { - testEnvironment: 'node', + testEnvironment: 'jsdom', }); } diff --git a/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index 3ed8152dbd82..0c8ef6bbf4db 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -54,7 +54,7 @@ const defaultOptions: Config.DefaultOptions = { skipFilter: false, slowTestThreshold: 5, snapshotSerializers: [], - testEnvironment: 'jest-environment-jsdom', + testEnvironment: 'jest-environment-node', testEnvironmentOptions: {}, testFailureExitCode: 1, testLocationInResults: false, diff --git a/website/versioned_docs/version-22.x/Configuration.md b/website/versioned_docs/version-22.x/Configuration.md index 5573979baabc..ab474ab74fe9 100644 --- a/website/versioned_docs/version-22.x/Configuration.md +++ b/website/versioned_docs/version-22.x/Configuration.md @@ -800,7 +800,7 @@ _Note: Jest comes with JSDOM@11 by default. Due to JSDOM 12 and newer dropping s Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. ### `testFailureExitCode` [number] diff --git a/website/versioned_docs/version-23.x/Configuration.md b/website/versioned_docs/version-23.x/Configuration.md index 9009bf609e95..6741c6c7f5ff 100644 --- a/website/versioned_docs/version-23.x/Configuration.md +++ b/website/versioned_docs/version-23.x/Configuration.md @@ -826,7 +826,7 @@ _Note: Jest comes with JSDOM@11 by default. Due to JSDOM 12 and newer dropping s Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. ### `testFailureExitCode` [number] diff --git a/website/versioned_docs/version-24.x/Configuration.md b/website/versioned_docs/version-24.x/Configuration.md index 791b8bdbcf96..b97929c4203c 100644 --- a/website/versioned_docs/version-24.x/Configuration.md +++ b/website/versioned_docs/version-24.x/Configuration.md @@ -967,7 +967,7 @@ _Note: Jest comes with JSDOM@11 by default. Due to JSDOM 12 and newer dropping s Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. ### `testFailureExitCode` [number] diff --git a/website/versioned_docs/version-25.x/Configuration.md b/website/versioned_docs/version-25.x/Configuration.md index 3979c208ce4d..92bb10dc1b6d 100644 --- a/website/versioned_docs/version-25.x/Configuration.md +++ b/website/versioned_docs/version-25.x/Configuration.md @@ -989,7 +989,7 @@ beforeAll(() => { Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. ### `testFailureExitCode` [number]