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

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

Merged
merged 31 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 19 commits
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
5 changes: 4 additions & 1 deletion packages/jest-environment-jsdom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/environment": "^24.1.0",
"@jest/fake-timers": "^24.1.0",
"@jest/types": "^24.1.0",
"jest-mock": "^24.0.0",
"jest-util": "^24.0.0",
"jsdom": "^11.5.1"
SimenB marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"devDependencies": {
"@types/jsdom": "^11.12.0"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @flow
*/

import type {Script} from 'vm';
import type {ProjectConfig} from 'types/Config';
import type {EnvironmentContext} from 'types/Environment';
import type {Global} from 'types/Global';
import type {ModuleMocker} from 'jest-mock';

import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
import {Script} from 'vm';
import {Global, Config} from '@jest/types';
import mock, {ModuleMocker} from 'jest-mock';
import {installCommonGlobals} from 'jest-util';
import mock from 'jest-mock';
import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
import {JestEnvironment, EnvironmentContext} from '@jest/environment';
import {JSDOM, VirtualConsole} from 'jsdom';

class JSDOMEnvironment {
dom: ?Object;
fakeTimers: ?FakeTimers<number>;
global: ?Global;
errorEventListener: ?Function;
moduleMocker: ?ModuleMocker;
class JSDOMEnvironment implements JestEnvironment {
dom: JSDOM | null;
fakeTimers: FakeTimers<number> | null;
global: Global.Global;
errorEventListener: ((event: Event & {error: any}) => void) | null;
moduleMocker: ModuleMocker | null;

constructor(config: ProjectConfig, options?: EnvironmentContext = {}) {
constructor(config: Config.ProjectConfig, options: EnvironmentContext = {}) {
this.dom = new JSDOM('<!DOCTYPE html>', {
pretendToBeVisual: true,
runScripts: 'dangerously',
Expand All @@ -51,13 +47,13 @@ class JSDOMEnvironment {
const originalAddListener = global.addEventListener;
const originalRemoveListener = global.removeEventListener;
let userErrorListenerCount = 0;
global.addEventListener = function(name) {
global.addEventListener = function(name: string) {
if (name === 'error') {
userErrorListenerCount++;
}
return originalAddListener.apply(this, arguments);
};
global.removeEventListener = function(name) {
global.removeEventListener = function(name: string) {
if (name === 'error') {
userErrorListenerCount--;
}
Expand All @@ -79,11 +75,11 @@ class JSDOMEnvironment {
});
}

setup(): Promise<void> {
setup() {
return Promise.resolve();
}

teardown(): Promise<void> {
teardown() {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
Expand All @@ -102,12 +98,16 @@ class JSDOMEnvironment {
return Promise.resolve();
}

runScript(script: Script): ?any {
runScript(script: Script) {
if (this.dom) {
return this.dom.runVMScript(script);
// `runVMScript` returns `void` and JestEnvironment.runScript is expected
// to return `... | null` so we don't return (`void`) early here:
//
// return this.dom.runVMScript(script);
SimenB marked this conversation as resolved.
Show resolved Hide resolved
this.dom.runVMScript(script);
}
return null;
}
}

module.exports = JSDOMEnvironment;
export = JSDOMEnvironment;
14 changes: 14 additions & 0 deletions packages/jest-environment-jsdom/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "build",
"rootDir": "src"
},
"references": [
{"path": "../jest-environment"},
{"path": "../jest-fake-timers"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"}
]
}