Skip to content

chore: migrate jest-environment-jsdom to TypeScript #8003

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

Merged
merged 31 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c857e4c
Rename files
Feb 25, 2019
0cfe10b
Migrate jest-environment-node
Feb 25, 2019
57a3567
Minor
Feb 25, 2019
9b279a6
Merge branch 'master' into jest-environment
Feb 26, 2019
2c3afe4
Use new fake timers package
Feb 26, 2019
ae3c45c
Implement JestEnvironment
Feb 26, 2019
bd4e070
before migration
Feb 26, 2019
71048f6
Update CHANGELOG.md
Feb 26, 2019
cc4eacb
add to tsconfigs
SimenB Feb 26, 2019
b3effe8
Merge remote-tracking branch 'upstream/master'
Feb 27, 2019
9cf040b
Merge branch 'master' into jest-environment
Feb 27, 2019
a40ea8d
Revert "before migration"
Feb 27, 2019
b215884
Merge branch 'master' into jest-environment
Feb 27, 2019
0a9e4c4
Rename files
Feb 27, 2019
4e8e638
Migrate to TS (WIP)
Feb 27, 2019
3c4486f
Update package.json
Feb 27, 2019
d50560b
Revert "Update package.json"
Feb 27, 2019
3968bc4
Update package.json
Feb 27, 2019
6105073
runScript should always return `null` (never `void`)
Feb 27, 2019
5ffc083
Migrate to TS (WIP)
Feb 27, 2019
f45cc88
Migrate mocks
Feb 27, 2019
1f9081a
Change runScript return type to unknown
Feb 27, 2019
f1ed385
Migrate jest-environment-jsdom
Feb 27, 2019
e162e48
Comments
Feb 27, 2019
eebb6f8
Update changelog and jest-config tsconfig.json
Feb 27, 2019
a743367
Merge remote-tracking branch 'upstream/master'
Feb 28, 2019
3beb007
Merge branch 'master' into jest-environment
Feb 28, 2019
5fb6bfd
Update return type of runScript()
Feb 28, 2019
58fc28b
Update global assignment of Window
Feb 28, 2019
c7c84f7
Merge branch 'master' into jest-environment
SimenB Feb 28, 2019
06539b1
tweaks to reduce diff
SimenB Feb 28, 2019
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
Prev Previous commit
Next Next commit
Migrate to TS (WIP)
  • Loading branch information
Mikael Lirbank committed Feb 27, 2019
commit 5ffc083fd76331d32cda2fe41e847924914b0cef
43 changes: 33 additions & 10 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
import {JestEnvironment, EnvironmentContext} from '@jest/environment';
import {JSDOM, VirtualConsole} from 'jsdom';

interface Win extends Window {
Error: {
stackTraceLimit: number;
};
}

function isWin(globals: Win | Global.Global): globals is Win {
return (globals as Win).document !== undefined;
}

class JSDOMEnvironment implements JestEnvironment {
dom: JSDOM | null;
fakeTimers: FakeTimers<number> | null;
global: Global.Global;
errorEventListener: ((event: Event & {error: any}) => void) | null;
// @ts-ignore
global: Global.Global | Win | null;
errorEventListener: ((event: Event & {error: unknown}) => void) | null;
moduleMocker: ModuleMocker | null;

constructor(config: Config.ProjectConfig, options: EnvironmentContext = {}) {
Expand All @@ -28,7 +39,16 @@ class JSDOMEnvironment implements JestEnvironment {
virtualConsole: new VirtualConsole().sendTo(options.console || console),
...config.testEnvironmentOptions,
});
const global = (this.global = this.dom.window.document.defaultView);
const global = (this.global = this.dom.window.document
.defaultView as Win | null);

if (!global || !this.global) {
this.fakeTimers = null;
this.errorEventListener = null;
this.moduleMocker = null;
return;
}

// Node's error-message stack size is limited at 10, but it's pretty useful
// to see more than that when a test fails.
this.global.Error.stackTraceLimit = 100;
Expand Down Expand Up @@ -84,12 +104,14 @@ class JSDOMEnvironment implements JestEnvironment {
this.fakeTimers.dispose();
}
if (this.global) {
if (this.errorEventListener) {
if (this.errorEventListener && isWin(this.global)) {
this.global.removeEventListener('error', this.errorEventListener);
}
// Dispose "document" to prevent "load" event from triggering.
Object.defineProperty(this.global, 'document', {value: null});
this.global.close();
if (isWin(this.global)) {
this.global.close();
}
}
this.errorEventListener = null;
this.global = null;
Expand All @@ -100,11 +122,12 @@ class JSDOMEnvironment implements JestEnvironment {

runScript(script: Script) {
if (this.dom) {
// `runVMScript` returns `void` and JestEnvironment.runScript is expected
// to return `... | null` so we don't return (`void`) early here:
//
// return this.dom.runVMScript(script);
this.dom.runVMScript(script);
// Explicitly returning `unknown` since `runVMScript` currently returns
// `void`, which is wrong

// WORK IN PROGRESS:
// return this.dom.runVMScript(script) as unknown;
return this.dom.runVMScript(script) as any;
}
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export declare class JestEnvironment {
global: Global.Global;
fakeTimers: FakeTimers<unknown> | null;
moduleMocker: ModuleMocker | null;
// runScript(
// script: Script,
// ): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null | unknown;
runScript(
script: Script,
): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null;
Expand Down