From fbfe79bf70c96b6b7efea5635c2ce333a5eba302 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 | 2 ++ 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 +- 18 files changed, 30 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf53aac5ced..b5766621c6c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser ([#9874](https://github.com/facebook/jest/pull/9874)) + ### Fixes ### Chore & Maintenance diff --git a/docs/Configuration.md b/docs/Configuration.md index 0a43c9b9412f..05a014fa78ed 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -930,9 +930,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: @@ -1012,7 +1012,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 77708ecfc22e..115f53794dac 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -50,7 +50,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/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 9f45edb32b42..e1f82b91e632 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 70e36b545cad..23a54bf7b0e3 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 @@ -183,7 +183,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 0824c70f43a1..b9510fbdbb14 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -122,8 +122,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 () => { @@ -133,8 +132,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 c968e2985e19..481ec95e5ee6 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -51,9 +51,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 8f496952ea0e..a097e1f37bb1 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -53,7 +53,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 094f97023c9e..85fe8302b0c2 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 cfab16861d4b..2017f7d7d5cf 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 0da1783ab9f7..7af40937fd90 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 b7ab1d921361..31e7195d881c 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\]