From abf417c4fcf51df75740443b10bf87ba2ef5245f Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sun, 5 May 2019 15:45:48 +0700 Subject: [PATCH 1/3] Clone to new test environment options while create test environments --- packages/jest-environment-jsdom/src/index.ts | 2 +- packages/jest-environment-node/src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 5e362f9244f6..bb6d6152ce79 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -35,7 +35,7 @@ class JSDOMEnvironment implements JestEnvironment { runScripts: 'dangerously', url: config.testURL, virtualConsole: new VirtualConsole().sendTo(options.console || console), - ...config.testEnvironmentOptions, + ...Object.create(config.testEnvironmentOptions), }); const global = (this.global = this.dom.window.document.defaultView as Win); diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index bc82af123b5e..1d66d21d3e0b 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -28,7 +28,7 @@ class NodeEnvironment implements JestEnvironment { this.context = vm.createContext(); const global = (this.global = vm.runInContext( 'this', - Object.assign(this.context, config.testEnvironmentOptions), + Object.assign(this.context, Object.create(config.testEnvironmentOptions)), )); global.global = global; global.clearInterval = clearInterval; From 2b46df46950251ffe79cde255639d98ed4637697 Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sun, 5 May 2019 15:56:12 +0700 Subject: [PATCH 2/3] Update change log for PR #8420 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b949443570c1..a4be7401baf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - `[jest-runtime]` Fix virtual mocks not being unmockable after previously being mocked ([#8396](https://github.com/facebook/jest/pull/8396)) - `[jest-transform]` Replace special characters in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353)) - `[jest-config]` Allow exactly one project ([#7498](https://github.com/facebook/jest/pull/7498)) +- `[jest-environment-jsdom]` `[jest-environment-node]` Fix `testEnvironmentOptions` affected in the test process ([#8420](https://github.com/facebook/jest/pull/8420)) ### Chore & Maintenance From 44b49ae66d6fa995e6ee2246f5e80912fa67ff35 Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sun, 5 May 2019 17:13:19 +0700 Subject: [PATCH 3/3] Fix syntax using to creating empty object --- packages/jest-environment-jsdom/src/index.ts | 21 +++++++++++++------- packages/jest-environment-node/src/index.ts | 5 ++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index bb6d6152ce79..239854201eed 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -30,13 +30,20 @@ class JSDOMEnvironment implements JestEnvironment { moduleMocker: ModuleMocker | null; constructor(config: Config.ProjectConfig, options: EnvironmentContext = {}) { - this.dom = new JSDOM('', { - pretendToBeVisual: true, - runScripts: 'dangerously', - url: config.testURL, - virtualConsole: new VirtualConsole().sendTo(options.console || console), - ...Object.create(config.testEnvironmentOptions), - }); + this.dom = new JSDOM( + '', + Object.assign( + { + pretendToBeVisual: true, + runScripts: 'dangerously', + url: config.testURL, + virtualConsole: new VirtualConsole().sendTo( + options.console || console, + ), + }, + Object.create(config.testEnvironmentOptions || null), + ), + ); const global = (this.global = this.dom.window.document.defaultView as Win); if (!global) { diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 1d66d21d3e0b..6e62214ef25f 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -28,7 +28,10 @@ class NodeEnvironment implements JestEnvironment { this.context = vm.createContext(); const global = (this.global = vm.runInContext( 'this', - Object.assign(this.context, Object.create(config.testEnvironmentOptions)), + Object.assign( + this.context, + Object.create(config.testEnvironmentOptions || null), + ), )); global.global = global; global.clearInterval = clearInterval;