From 24b421ee2a6948a5862d59e5a2a8786989554ff5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 20:13:36 +0200 Subject: [PATCH] chore: default to node test env rather than browser --- CHANGELOG.md | 1 + docs/Configuration.md | 6 +++--- e2e/__tests__/__snapshots__/showConfig.test.ts.snap | 2 +- e2e/__tests__/config.test.ts | 1 + examples/angular/jest.config.js | 1 + examples/jquery/package.json | 3 +++ examples/react-testing-library/package.json | 3 +++ examples/react/package.json | 3 +++ examples/typescript/package.json | 3 +++ jest.config.js | 1 - .../src/init/__tests__/__snapshots__/init.test.js.snap | 2 +- packages/jest-cli/src/init/__tests__/init.test.js | 6 ++---- packages/jest-cli/src/init/generate_config_file.ts | 4 ++-- packages/jest-config/src/Defaults.ts | 2 +- website/versioned_docs/version-22.x/Configuration.md | 2 +- website/versioned_docs/version-23.x/Configuration.md | 2 +- website/versioned_docs/version-24.x/Configuration.md | 2 +- website/versioned_docs/version-25.x/Configuration.md | 2 +- website/versioned_docs/version-26.0/Configuration.md | 2 +- 19 files changed, 30 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57bbc7d8a17a..1d91195d8d6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-config]` Support config files exporting (`async`) `function`s ([#10001](https://github.com/facebook/jest/pull/10001)) +- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser ([#9874](https://github.com/facebook/jest/pull/9874)) - `[jest-cli, jest-core]` Add `--selectProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612)) ### Fixes diff --git a/docs/Configuration.md b/docs/Configuration.md index 0e774f4f21fa..9390403f5413 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -891,9 +891,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: @@ -973,7 +973,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"}`. ### `testMatch` [array\] diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 9c644c2b47e6..e2ec3eae2066 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -49,7 +49,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/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 18ad91a82b6d..905d9fc8b41d 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: [ '/__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 5e4733fabd9d..9e9d4e2e34fe 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 @@ -177,7 +177,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 579ab04f24fc..6b683d2e6068 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 f10ec309c835..993bf5ac9dfe 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -51,7 +51,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/versioned_docs/version-22.x/Configuration.md b/website/versioned_docs/version-22.x/Configuration.md index edf1f78de0e9..2e0b4943bd9a 100644 --- a/website/versioned_docs/version-22.x/Configuration.md +++ b/website/versioned_docs/version-22.x/Configuration.md @@ -716,7 +716,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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-23.x/Configuration.md b/website/versioned_docs/version-23.x/Configuration.md index f7037c456657..e585789cacdd 100644 --- a/website/versioned_docs/version-23.x/Configuration.md +++ b/website/versioned_docs/version-23.x/Configuration.md @@ -782,7 +782,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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-24.x/Configuration.md b/website/versioned_docs/version-24.x/Configuration.md index a66b0e4f2a2e..5134e15fe1ff 100644 --- a/website/versioned_docs/version-24.x/Configuration.md +++ b/website/versioned_docs/version-24.x/Configuration.md @@ -944,7 +944,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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-25.x/Configuration.md b/website/versioned_docs/version-25.x/Configuration.md index 18d04e621043..79269c3e3127 100644 --- a/website/versioned_docs/version-25.x/Configuration.md +++ b/website/versioned_docs/version-25.x/Configuration.md @@ -966,7 +966,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"}`. ### `testMatch` [array\] diff --git a/website/versioned_docs/version-26.0/Configuration.md b/website/versioned_docs/version-26.0/Configuration.md index 46bc1f3517c5..167ff0f5eb92 100644 --- a/website/versioned_docs/version-26.0/Configuration.md +++ b/website/versioned_docs/version-26.0/Configuration.md @@ -966,7 +966,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"}`. ### `testMatch` [array\]