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 diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 8d0c33c1df7c..5e362f9244f6 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -15,24 +15,17 @@ 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 { - 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; - // @ts-ignore - global: Global.Global | Win | null; + global: Win; errorEventListener: ((event: Event & {error: Error}) => void) | null; moduleMocker: ModuleMocker | null; @@ -109,16 +102,15 @@ 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;