Skip to content

Commit

Permalink
Declare global prototype in JSDOMEnvironment to fix issue #8347 (#8352)
Browse files Browse the repository at this point in the history
* Declare global prototype in JSDOMEnvironment to fix issue #8347

* Change type to check isWin and remove set global to null

* Add description of PR to the changelog

* Set global to `null` in teardown process and uncheck is window for global prototype
  • Loading branch information
lh0x00 authored and SimenB committed Apr 21, 2019
1 parent 31e06e8 commit 72c920e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 9 additions & 17 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> | null;
// @ts-ignore
global: Global.Global | Win | null;
global: Win;
errorEventListener: ((event: Event & {error: Error}) => void) | null;
moduleMocker: ModuleMocker | null;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 72c920e

Please sign in to comment.