Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare global prototype in JSDOMEnvironment to fix issue #8347 #8352

Merged
merged 4 commits into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
jeysal marked this conversation as resolved.
Show resolved Hide resolved
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;
lh0x00 marked this conversation as resolved.
Show resolved Hide resolved
this.dom = null;
lh0x00 marked this conversation as resolved.
Show resolved Hide resolved
this.fakeTimers = null;
Expand Down