From 7003875d6aa62f6be4397389e9f5d2319df795de Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sat, 20 Apr 2019 18:30:31 +0700 Subject: [PATCH 1/4] Declare global prototype in JSDOMEnvironment to fix issue #8347 --- packages/jest-environment-jsdom/src/index.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 8d0c33c1df7c..88a976d267c9 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -15,13 +15,14 @@ import {JSDOM, VirtualConsole} from 'jsdom'; // The `Window` interface does not have an `Error.stackTraceLimit` property, but // `JSDOMEnvironment` assumes it is there. -interface Win extends Window { - Error: { - stackTraceLimit: number; +type Win = Window & + Global.Global & { + Error: { + stackTraceLimit: number; + }; }; -} -function isWin(globals: Win | Global.Global): globals is Win { +function isWin(globals: Win): globals is Win { return (globals as Win).document !== undefined; } @@ -31,8 +32,7 @@ function isWin(globals: Win | Global.Global): globals is Win { class JSDOMEnvironment implements JestEnvironment { dom: JSDOM | null; fakeTimers: FakeTimers | null; - // @ts-ignore - global: Global.Global | Win | null; + global: Win; errorEventListener: ((event: Event & {error: Error}) => void) | null; moduleMocker: ModuleMocker | null; @@ -119,6 +119,7 @@ class JSDOMEnvironment implements JestEnvironment { } } this.errorEventListener = null; + // @ts-ignore this.global = null; this.dom = null; this.fakeTimers = null; From 739e3f322f7107ead761442858941df51bc8277b Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sun, 21 Apr 2019 13:12:38 +0700 Subject: [PATCH 2/4] Change type to check isWin and remove set global to null --- packages/jest-environment-jsdom/src/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 88a976d267c9..ba09a04d5dba 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -22,7 +22,7 @@ type Win = Window & }; }; -function isWin(globals: Win): globals is Win { +function isWin(globals: Win | Global.Global): globals is Win { return (globals as Win).document !== undefined; } @@ -119,8 +119,6 @@ class JSDOMEnvironment implements JestEnvironment { } } this.errorEventListener = null; - // @ts-ignore - this.global = null; this.dom = null; this.fakeTimers = null; return Promise.resolve(); From edac7194d241eb29feb94e8181afc80b42b49313 Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sun, 21 Apr 2019 13:19:17 +0700 Subject: [PATCH 3/4] Add description of PR to the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad6df5adf66c..5ea8ff4bcb5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283)) - `[jest-haste-map]` Fix the `mapper` option which was incorrectly ignored ([#8299](https://github.com/facebook/jest/pull/8299)) - `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335)) +- `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352)) ### Chore & Maintenance From cb09287d30dfc02ec5e66b6c98b1f2b3ab956d00 Mon Sep 17 00:00:00 2001 From: Lam Hieu Date: Sun, 21 Apr 2019 18:16:17 +0700 Subject: [PATCH 4/4] Set global to `null` in teardown process and uncheck is window for global prototype --- packages/jest-environment-jsdom/src/index.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index ba09a04d5dba..5e362f9244f6 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -22,13 +22,6 @@ type Win = Window & }; }; -function isWin(globals: Win | Global.Global): globals is Win { - return (globals as Win).document !== undefined; -} - -// A lot of the globals expected by other APIs are `NodeJS.Global` and not -// `Window`, so we need to cast here and there - class JSDOMEnvironment implements JestEnvironment { dom: JSDOM | null; fakeTimers: FakeTimers | null; @@ -109,16 +102,16 @@ class JSDOMEnvironment implements JestEnvironment { this.fakeTimers.dispose(); } if (this.global) { - if (this.errorEventListener && isWin(this.global)) { + if (this.errorEventListener) { this.global.removeEventListener('error', this.errorEventListener); } // Dispose "document" to prevent "load" event from triggering. Object.defineProperty(this.global, 'document', {value: null}); - if (isWin(this.global)) { - this.global.close(); - } + this.global.close(); } this.errorEventListener = null; + // @ts-ignore + this.global = null; this.dom = null; this.fakeTimers = null; return Promise.resolve();