From c8939e56d3e8f6cc024d3be6e6b66825cd376e93 Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Sat, 2 Dec 2017 11:16:54 -0500 Subject: [PATCH 1/8] Allow testEnvironmentOptions to be sent to jsdom options or node context. --- CHANGELOG.md | 2 ++ docs/Configuration.md | 11 +++++++++++ packages/jest-config/src/defaults.js | 1 + packages/jest-config/src/index.js | 1 + packages/jest-config/src/normalize.js | 1 + packages/jest-config/src/valid_config.js | 1 + packages/jest-environment-jsdom/src/index.js | 16 +++++++++++----- packages/jest-environment-node/src/index.js | 5 ++++- test_utils.js | 1 + types/Config.js | 3 +++ 10 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 968f63cea419..731d747ae3b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,8 @@ ### Features +* `[jest-config]` Add `testEnvironmentOptions` to apply to jsdom options or node context. + ([#?](https://github.com/facebook/jest/pull/?)) * `[jest-jasmine2]` Update Timeout error message to `jest.timeout` and display current timeout value ([#4990](https://github.com/facebook/jest/pull/4990)) * `[jest-runner]` Enable experimental detection of leaked contexts diff --git a/docs/Configuration.md b/docs/Configuration.md index f40ba90e1e60..38d7906c1746 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -726,6 +726,17 @@ class CustomEnvironment extends NodeEnvironment { } ``` +### `testEnvironmentOptions` [Object] + +##### available in Jest **2?.0.0+** + +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"}`. + ### `testMatch` [array] ##### available in Jest **19.0.0+** diff --git a/packages/jest-config/src/defaults.js b/packages/jest-config/src/defaults.js index 4fe4ac704d7f..404a75b90445 100644 --- a/packages/jest-config/src/defaults.js +++ b/packages/jest-config/src/defaults.js @@ -56,6 +56,7 @@ export default ({ runner: 'jest-runner', snapshotSerializers: [], testEnvironment: 'jest-environment-jsdom', + testEnvironmentOptions: {}, testFailureExitCode: 1, testLocationInResults: false, testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'], diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 4305850c62bb..8ae2513bb5ce 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -147,6 +147,7 @@ const getConfigs = ( skipNodeResolution: options.skipNodeResolution, snapshotSerializers: options.snapshotSerializers, testEnvironment: options.testEnvironment, + testEnvironmentOptions: options.testEnvironmentOptions, testLocationInResults: options.testLocationInResults, testMatch: options.testMatch, testPathIgnorePatterns: options.testPathIgnorePatterns, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 472c983f4ac2..0cfd0b98f69f 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -481,6 +481,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'silent': case 'skipNodeResolution': case 'testEnvironment': + case 'testEnvironmentOptions': case 'testFailureExitCode': case 'testLocationInResults': case 'testNamePattern': diff --git a/packages/jest-config/src/valid_config.js b/packages/jest-config/src/valid_config.js index c1c5fa9a9707..859e855e4fe7 100644 --- a/packages/jest-config/src/valid_config.js +++ b/packages/jest-config/src/valid_config.js @@ -77,6 +77,7 @@ export default ({ skipNodeResolution: false, snapshotSerializers: ['my-serializer-module'], testEnvironment: 'jest-environment-jsdom', + testEnvironmentOptions: {}, testFailureExitCode: 1, testLocationInResults: false, testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'], diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 33293fe28aec..4d6bada02876 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -23,11 +23,17 @@ class JSDOMEnvironment { moduleMocker: ?ModuleMocker; constructor(config: ProjectConfig) { - this.dom = new JSDOM('', { - pretendToBeVisual: true, - runScripts: 'dangerously', - url: config.testURL, - }); + this.dom = new JSDOM( + '', + Object.assign( + { + pretendToBeVisual: true, + runScripts: 'dangerously', + url: config.testURL, + }, + config.testEnvironmentOptions, + ), + ); const global = (this.global = this.dom.window.document.defaultView); // Node's error-message stack size is limited at 10, but it's pretty useful // to see more than that when a test fails. diff --git a/packages/jest-environment-node/src/index.js b/packages/jest-environment-node/src/index.js index 89147f3e031c..168afd72ee7d 100644 --- a/packages/jest-environment-node/src/index.js +++ b/packages/jest-environment-node/src/index.js @@ -30,7 +30,10 @@ class NodeEnvironment { constructor(config: ProjectConfig) { this.context = vm.createContext(); - const global = (this.global = vm.runInContext('this', this.context)); + const global = (this.global = vm.runInContext( + 'this', + Object.assign(this.context, config.testEnvironmentOptions), + )); global.global = global; global.clearInterval = clearInterval; global.clearTimeout = clearTimeout; diff --git a/test_utils.js b/test_utils.js index 3fa55abc6271..7abff3c79f6b 100644 --- a/test_utils.js +++ b/test_utils.js @@ -89,6 +89,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { skipNodeResolution: false, snapshotSerializers: [], testEnvironment: 'node', + testEnvironmentOptions: {}, testLocationInResults: false, testMatch: [], testPathIgnorePatterns: [], diff --git a/types/Config.js b/types/Config.js index 9dfa137b7ce4..376445516b7a 100644 --- a/types/Config.js +++ b/types/Config.js @@ -48,6 +48,7 @@ export type DefaultOptions = {| runTestsByPath: boolean, snapshotSerializers: Array, testEnvironment: string, + testEnvironmentOptions: Object, testFailureExitCode: string | number, testLocationInResults: boolean, testMatch: Array, @@ -122,6 +123,7 @@ export type InitialOptions = { skipNodeResolution?: boolean, snapshotSerializers?: Array, testEnvironment?: string, + testEnvironmentOptions?: Object, testFailureExitCode?: string | number, testLocationInResults?: boolean, testMatch?: Array, @@ -224,6 +226,7 @@ export type ProjectConfig = {| skipNodeResolution: boolean, snapshotSerializers: Array, testEnvironment: string, + testEnvironmentOptions: Object, testMatch: Array, testLocationInResults: boolean, testPathIgnorePatterns: Array, From 243e8ef8d2b247e4f21992b8c22839838c6eb915 Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Sat, 2 Dec 2017 12:43:39 -0500 Subject: [PATCH 2/8] Update pull-request#5003. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 731d747ae3b4..3c81e92a7448 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,7 @@ ### Features * `[jest-config]` Add `testEnvironmentOptions` to apply to jsdom options or node context. - ([#?](https://github.com/facebook/jest/pull/?)) + ([#5003](https://github.com/facebook/jest/pull/5003)) * `[jest-jasmine2]` Update Timeout error message to `jest.timeout` and display current timeout value ([#4990](https://github.com/facebook/jest/pull/4990)) * `[jest-runner]` Enable experimental detection of leaked contexts From 2be0fb624cc770e9e0b76acd50eab7548ab5ae53 Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Sat, 2 Dec 2017 12:55:51 -0500 Subject: [PATCH 3/8] Fix integration_test show_config snapshot. --- .../__tests__/__snapshots__/show_config.test.js.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/integration_tests/__tests__/__snapshots__/show_config.test.js.snap b/integration_tests/__tests__/__snapshots__/show_config.test.js.snap index 5fa329deb625..bc52f635f121 100644 --- a/integration_tests/__tests__/__snapshots__/show_config.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/show_config.test.js.snap @@ -39,6 +39,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` \\"setupFiles\\": [], \\"snapshotSerializers\\": [], \\"testEnvironment\\": \\"jest-environment-jsdom\\", + \\"testEnvironmentOptions\\": {}, \\"testLocationInResults\\": false, \\"testMatch\\": [ \\"**/__tests__/**/*.js?(x)\\", From b881851b254de509ae90bcdcfbee2a8d73d55bd6 Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Sun, 3 Dec 2017 13:00:05 -0500 Subject: [PATCH 4/8] available in Jest **22 --- docs/Configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 38d7906c1746..894e920f8c6a 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -728,7 +728,7 @@ class CustomEnvironment extends NodeEnvironment { ### `testEnvironmentOptions` [Object] -##### available in Jest **2?.0.0+** +##### available in Jest **22.0.0+** Default: `{}` From a0eca53423f1fa66668a22d2d9f4b13f84651867 Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Tue, 5 Dec 2017 23:08:51 -0500 Subject: [PATCH 5/8] Add testEnvironmentOptions to integration_tests. --- integration_tests/__tests__/config.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/integration_tests/__tests__/config.test.js b/integration_tests/__tests__/config.test.js index 12a29e6edba9..c4c825d7511f 100644 --- a/integration_tests/__tests__/config.test.js +++ b/integration_tests/__tests__/config.test.js @@ -59,3 +59,18 @@ test('config from argv is respected with sane config JSON', () => { expect(stdout).toMatch('"watchman": false'); }); + +test('works with jsdom testEnvironmentOptions config JSON', () => { + const result = runJest('environmentOptions', [ + '--config=' + + JSON.stringify({ + testEnvironmentOptions: { + userAgent: "Agent/007" + } + }), + ]); + const stderr = result.stderr.toString(); + + expect(result.status).toBe(0); + expect(stderr).toMatch('found userAgent Agent/007'); +}); From 5860d18f063d466dc303e2e32f2c7af22f8eae03 Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Wed, 6 Dec 2017 07:38:11 -0500 Subject: [PATCH 6/8] Fix lint errors and environmentOptions. --- integration_tests/__tests__/config.test.js | 2 +- .../__tests__/environmentOptions.test.js | 11 +++++++++++ integration_tests/environmentOptions/package.json | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 integration_tests/environmentOptions/__tests__/environmentOptions.test.js create mode 100644 integration_tests/environmentOptions/package.json diff --git a/integration_tests/__tests__/config.test.js b/integration_tests/__tests__/config.test.js index c4c825d7511f..9bebdb87d3a9 100644 --- a/integration_tests/__tests__/config.test.js +++ b/integration_tests/__tests__/config.test.js @@ -65,7 +65,7 @@ test('works with jsdom testEnvironmentOptions config JSON', () => { '--config=' + JSON.stringify({ testEnvironmentOptions: { - userAgent: "Agent/007" + userAgent: "Agent/007", } }), ]); diff --git a/integration_tests/environmentOptions/__tests__/environmentOptions.test.js b/integration_tests/environmentOptions/__tests__/environmentOptions.test.js new file mode 100644 index 000000000000..dde93e6093a9 --- /dev/null +++ b/integration_tests/environmentOptions/__tests__/environmentOptions.test.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +test('found userAgent Agent/007', () => { + expect(navigator.userAgent).toBe("Agent/007"); +}); diff --git a/integration_tests/environmentOptions/package.json b/integration_tests/environmentOptions/package.json new file mode 100644 index 000000000000..0ded940b7cb7 --- /dev/null +++ b/integration_tests/environmentOptions/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "jsdom" + } +} From bcd787122fa6f30a3ea02a14233ed29593211b7d Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Wed, 6 Dec 2017 07:45:26 -0500 Subject: [PATCH 7/8] Fix more lint errors. --- integration_tests/__tests__/config.test.js | 2 +- .../environmentOptions/__tests__/environmentOptions.test.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/integration_tests/__tests__/config.test.js b/integration_tests/__tests__/config.test.js index 9bebdb87d3a9..e7fc3098b23b 100644 --- a/integration_tests/__tests__/config.test.js +++ b/integration_tests/__tests__/config.test.js @@ -66,7 +66,7 @@ test('works with jsdom testEnvironmentOptions config JSON', () => { JSON.stringify({ testEnvironmentOptions: { userAgent: "Agent/007", - } + }, }), ]); const stderr = result.stderr.toString(); diff --git a/integration_tests/environmentOptions/__tests__/environmentOptions.test.js b/integration_tests/environmentOptions/__tests__/environmentOptions.test.js index dde93e6093a9..4940942ceded 100644 --- a/integration_tests/environmentOptions/__tests__/environmentOptions.test.js +++ b/integration_tests/environmentOptions/__tests__/environmentOptions.test.js @@ -5,7 +5,8 @@ * LICENSE file in the root directory of this source tree. */ 'use strict'; +/*global window */ test('found userAgent Agent/007', () => { - expect(navigator.userAgent).toBe("Agent/007"); + expect(window.navigator.userAgent).toBe("Agent/007"); }); From 4a393e20ec644fbbff078465f5015d97f4acbf1f Mon Sep 17 00:00:00 2001 From: Keith Mashinter Date: Wed, 6 Dec 2017 07:53:19 -0500 Subject: [PATCH 8/8] Fix lint quoting, burned since all my other projects want double-quotes. --- integration_tests/__tests__/config.test.js | 2 +- .../environmentOptions/__tests__/environmentOptions.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integration_tests/__tests__/config.test.js b/integration_tests/__tests__/config.test.js index e7fc3098b23b..a0a8fa7ac700 100644 --- a/integration_tests/__tests__/config.test.js +++ b/integration_tests/__tests__/config.test.js @@ -65,7 +65,7 @@ test('works with jsdom testEnvironmentOptions config JSON', () => { '--config=' + JSON.stringify({ testEnvironmentOptions: { - userAgent: "Agent/007", + userAgent: 'Agent/007', }, }), ]); diff --git a/integration_tests/environmentOptions/__tests__/environmentOptions.test.js b/integration_tests/environmentOptions/__tests__/environmentOptions.test.js index 4940942ceded..504f1c945b52 100644 --- a/integration_tests/environmentOptions/__tests__/environmentOptions.test.js +++ b/integration_tests/environmentOptions/__tests__/environmentOptions.test.js @@ -8,5 +8,5 @@ /*global window */ test('found userAgent Agent/007', () => { - expect(window.navigator.userAgent).toBe("Agent/007"); + expect(window.navigator.userAgent).toBe('Agent/007'); });