diff --git a/CHANGELOG.md b/CHANGELOG.md index 9be830431972..241d74fc29d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser ([#9874](https://github.com/facebook/jest/pull/9874)) - `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801)) - `[jest-runtime]` Populate `require.cache` ([#9841](https://github.com/facebook/jest/pull/9841)) diff --git a/docs/Configuration.md b/docs/Configuration.md index c856ca2ca7c1..d8c404d28a1b 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -881,9 +881,9 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap ### `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/tmpvar/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: @@ -963,7 +963,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/tmpvar/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"}`. ### `testMatch` [array\] diff --git a/e2e/__tests__/__snapshots__/consoleLogOutputWhenRunInBand.test.ts.snap b/e2e/__tests__/__snapshots__/consoleLogOutputWhenRunInBand.test.ts.snap index 571dd419e639..f4728989cc42 100644 --- a/e2e/__tests__/__snapshots__/consoleLogOutputWhenRunInBand.test.ts.snap +++ b/e2e/__tests__/__snapshots__/consoleLogOutputWhenRunInBand.test.ts.snap @@ -20,6 +20,6 @@ exports[`prints console.logs when run with forceExit 3`] = ` console.log Hey - at Object. (__tests__/a-banana.js:1:1) + at Object.log (__tests__/a-banana.js:1:30) `; diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 7f3d73efee7c..239830936939 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`] = ` "setupFilesAfterEnv": [], "skipFilter": false, "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/__tests__/consoleLogOutputWhenRunInBand.test.ts b/e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts index 3e505c906365..ea0dd1d1a117 100644 --- a/e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts +++ b/e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts @@ -34,14 +34,14 @@ test('prints console.logs when run with forceExit', () => { const {rest, summary} = extractSummary(stderr); - if (nodeMajorVersion < 12) { + if (nodeMajorVersion < 10) { expect(stdout).toContain( 'at Object..test (__tests__/a-banana.js:1:1)', ); stdout = stdout.replace( 'at Object..test (__tests__/a-banana.js:1:1)', - 'at Object. (__tests__/a-banana.js:1:1)', + 'at Object.log (__tests__/a-banana.js:1:30)', ); } diff --git a/e2e/__tests__/resolveBrowserField.test.ts b/e2e/__tests__/resolveBrowserField.test.ts index 2c37924e2bc5..aeba32b6353b 100644 --- a/e2e/__tests__/resolveBrowserField.test.ts +++ b/e2e/__tests__/resolveBrowserField.test.ts @@ -35,6 +35,7 @@ test('preserves module identity for symlinks when using browser field resolution `, 'package.json': JSON.stringify({ jest: { + testEnvironment: 'jsdom', testMatch: ['/test-files/test.js'], browser: true, }, diff --git a/examples/angular/jest.config.js b/examples/angular/jest.config.js index 4329aeac9dad..3fbb887ae16d 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: { '^.+\\.[t|j]s$': [ 'babel-jest', 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 cb189da42604..e1fb008f34e8 100644 --- a/jest.config.js +++ b/jest.config.js @@ -34,7 +34,6 @@ module.exports = { '/packages/pretty-format/build/plugins/ConvertAnsi.js', require.resolve('jest-snapshot-serializer-raw'), ], - testEnvironment: './packages/jest-environment-node', testPathIgnorePatterns: [ '/__arbitraries__/', '/node_modules/', 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 a9eb4ea43840..2f50767e2889 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 @@ -180,7 +180,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 874a6d958c75..647f7ae54d87 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -100,8 +100,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 () => { @@ -111,8 +110,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/generate_config_file.ts b/packages/jest-cli/src/init/generate_config_file.ts index 36c19bdf3759..f10f8b68da0e 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -45,9 +45,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 6cdc3cbe8a01..e0f8f2eab9f6 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -53,7 +53,7 @@ const defaultOptions: Config.DefaultOptions = { setupFilesAfterEnv: [], skipFilter: false, snapshotSerializers: [], - testEnvironment: 'jest-environment-jsdom', + testEnvironment: 'jest-environment-node', testEnvironmentOptions: {}, testFailureExitCode: 1, testLocationInResults: false, diff --git a/website/blog/2017-12-18-jest-22.md b/website/blog/2017-12-18-jest-22.md index cb50df774577..e3447479ae77 100644 --- a/website/blog/2017-12-18-jest-22.md +++ b/website/blog/2017-12-18-jest-22.md @@ -11,7 +11,7 @@ Today we are announcing a new major version of Jest which refines almost all par ## Good bye Node 4 & welcome JSDOM 11 -With this release we are dropping support for Node 4, mainly because one of our main dependencies, JSDOM, ended their support. Jest now comes out of the box with JSDOM 11 which features better support for SVGs, `requestAnimationFrame`, `URL` and `URLSearchParams` built in, and [much more](https://github.com/tmpvar/jsdom/blob/master/Changelog.md). +With this release we are dropping support for Node 4, mainly because one of our main dependencies, JSDOM, ended their support. Jest now comes out of the box with JSDOM 11 which features better support for SVGs, `requestAnimationFrame`, `URL` and `URLSearchParams` built in, and [much more](https://github.com/jsdom/jsdom/blob/master/Changelog.md). ## Custom Runners + Easy parallelization with `jest-worker` diff --git a/website/versioned_docs/version-22.x/Configuration.md b/website/versioned_docs/version-22.x/Configuration.md index 76814adff6f7..a8a544632c4d 100644 --- a/website/versioned_docs/version-22.x/Configuration.md +++ b/website/versioned_docs/version-22.x/Configuration.md @@ -651,7 +651,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap Default: `"jsdom"` -The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/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 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. If some tests require another environment, you can add a `@jest-environment` docblock. @@ -714,7 +714,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/tmpvar/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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-23.x/Configuration.md b/website/versioned_docs/version-23.x/Configuration.md index d3abda1eaac5..bcdde77c59b1 100644 --- a/website/versioned_docs/version-23.x/Configuration.md +++ b/website/versioned_docs/version-23.x/Configuration.md @@ -715,7 +715,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap Default: `"jsdom"` -The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/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 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. 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: @@ -780,7 +780,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/tmpvar/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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-24.x/Configuration.md b/website/versioned_docs/version-24.x/Configuration.md index 2e169d285daa..b6a04fc95423 100644 --- a/website/versioned_docs/version-24.x/Configuration.md +++ b/website/versioned_docs/version-24.x/Configuration.md @@ -860,7 +860,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap Default: `"jsdom"` -The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/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 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. 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: @@ -942,7 +942,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/tmpvar/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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-25.1/Configuration.md b/website/versioned_docs/version-25.1/Configuration.md index a2c1ed6c3f8d..f026b29d5002 100644 --- a/website/versioned_docs/version-25.1/Configuration.md +++ b/website/versioned_docs/version-25.1/Configuration.md @@ -871,7 +871,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap Default: `"jsdom"` -The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/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 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. 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: @@ -951,7 +951,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/tmpvar/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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-25.3/Configuration.md b/website/versioned_docs/version-25.3/Configuration.md index d854c2d348c5..80b7a3323606 100644 --- a/website/versioned_docs/version-25.3/Configuration.md +++ b/website/versioned_docs/version-25.3/Configuration.md @@ -884,7 +884,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap Default: `"jsdom"` -The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/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 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. 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: @@ -964,7 +964,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/tmpvar/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"}`. ### `testMatch` [array\]