From c857e4c0059062bd8830a5483b0142d3736b4e99 Mon Sep 17 00:00:00 2001 From: Mikael Lirbank Date: Mon, 25 Feb 2019 09:38:35 -0800 Subject: [PATCH 1/7] Rename files --- .../{node_environment.test.js => node_environment.test.ts} | 0 packages/jest-environment-node/src/{index.js => index.ts} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/jest-environment-node/src/__tests__/{node_environment.test.js => node_environment.test.ts} (100%) rename packages/jest-environment-node/src/{index.js => index.ts} (100%) diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.js b/packages/jest-environment-node/src/__tests__/node_environment.test.ts similarity index 100% rename from packages/jest-environment-node/src/__tests__/node_environment.test.js rename to packages/jest-environment-node/src/__tests__/node_environment.test.ts diff --git a/packages/jest-environment-node/src/index.js b/packages/jest-environment-node/src/index.ts similarity index 100% rename from packages/jest-environment-node/src/index.js rename to packages/jest-environment-node/src/index.ts From 0cfe10bbac0b48d4e56f489e67947d6bd078f579 Mon Sep 17 00:00:00 2001 From: Mikael Lirbank Date: Mon, 25 Feb 2019 12:01:59 -0800 Subject: [PATCH 2/7] Migrate jest-environment-node --- packages/jest-environment-node/package.json | 2 + packages/jest-environment-node/src/index.ts | 43 +++++++++----------- packages/jest-environment-node/tsconfig.json | 12 ++++++ 3 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 packages/jest-environment-node/tsconfig.json diff --git a/packages/jest-environment-node/package.json b/packages/jest-environment-node/package.json index 0914ca74f5e8..5b89a62f15b2 100644 --- a/packages/jest-environment-node/package.json +++ b/packages/jest-environment-node/package.json @@ -8,7 +8,9 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { + "@jest/types": "^24.1.0", "jest-mock": "^24.0.0", "jest-util": "^24.0.0" }, diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index f09b3f7c30a1..27052b860cce 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -3,32 +3,28 @@ * * 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 {Global} from 'types/Global'; -import type {ModuleMocker} from 'jest-mock'; - -import vm from 'vm'; -import {FakeTimers, installCommonGlobals} from 'jest-util'; -import mock from 'jest-mock'; +import vm, {Script, Context} from 'vm'; +import {Global, Config} from '@jest/types'; +import {ModuleMocker} from 'jest-mock'; +import {installCommonGlobals} from 'jest-util'; +import FakeTimers from '../../jest-util/build/FakeTimers'; +// import FakeTimers from 'jest-util/build/FakeTimers'; -type Timer = {| - id: number, - ref: () => Timer, - unref: () => Timer, -|}; +type Timer = { + id: number; + ref: () => Timer; + unref: () => Timer; +}; class NodeEnvironment { - context: ?vm$Context; - fakeTimers: ?FakeTimers; - global: ?Global; - moduleMocker: ?ModuleMocker; + context?: Context | null; + fakeTimers?: FakeTimers | null; + global?: Global.Global | null; + moduleMocker?: ModuleMocker | null; - constructor(config: ProjectConfig) { + constructor(config: Config.ProjectConfig) { this.context = vm.createContext(); const global = (this.global = vm.runInContext( 'this', @@ -47,7 +43,7 @@ class NodeEnvironment { global.URLSearchParams = URLSearchParams; } installCommonGlobals(global, config.globals); - this.moduleMocker = new mock.ModuleMocker(global); + this.moduleMocker = new ModuleMocker(global); const timerIdToRef = (id: number) => ({ id, @@ -59,7 +55,8 @@ class NodeEnvironment { }, }); - const timerRefToId = (timer: Timer): ?number => (timer && timer.id) || null; + const timerRefToId = (timer: Timer): number | undefined => + timer && timer.id; const timerConfig = { idToRef: timerIdToRef, @@ -88,7 +85,7 @@ class NodeEnvironment { } // Disabling rule as return type depends on script's return type. - runScript(script: Script): ?any { + runScript(script: Script): any | null | undefined { if (this.context) { return script.runInContext(this.context); } diff --git a/packages/jest-environment-node/tsconfig.json b/packages/jest-environment-node/tsconfig.json new file mode 100644 index 000000000000..96c241ae4d5e --- /dev/null +++ b/packages/jest-environment-node/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "build", + "rootDir": "src" + }, + "references": [ + {"path": "../jest-mock"}, + {"path": "../jest-types"}, + {"path": "../jest-util"} + ] +} From 57a3567b069af8344d65e87a2544ded0a5dca64c Mon Sep 17 00:00:00 2001 From: Mikael Lirbank Date: Mon, 25 Feb 2019 12:54:59 -0800 Subject: [PATCH 3/7] Minor --- packages/jest-environment-node/src/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 27052b860cce..07e4f93f6644 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -9,8 +9,7 @@ import vm, {Script, Context} from 'vm'; import {Global, Config} from '@jest/types'; import {ModuleMocker} from 'jest-mock'; import {installCommonGlobals} from 'jest-util'; -import FakeTimers from '../../jest-util/build/FakeTimers'; -// import FakeTimers from 'jest-util/build/FakeTimers'; +import FakeTimers from 'jest-util/build/FakeTimers'; type Timer = { id: number; @@ -56,7 +55,7 @@ class NodeEnvironment { }); const timerRefToId = (timer: Timer): number | undefined => - timer && timer.id; + (timer && timer.id) || undefined; const timerConfig = { idToRef: timerIdToRef, From 2c3afe4bf8311250850655c68955a96736a95907 Mon Sep 17 00:00:00 2001 From: Mikael Lirbank Date: Tue, 26 Feb 2019 09:38:55 -0800 Subject: [PATCH 4/7] Use new fake timers package --- packages/jest-environment-node/package.json | 2 +- packages/jest-environment-node/src/index.ts | 2 +- packages/jest-environment-node/tsconfig.json | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/jest-environment-node/package.json b/packages/jest-environment-node/package.json index 353d4632e91f..2fbbf37f0429 100644 --- a/packages/jest-environment-node/package.json +++ b/packages/jest-environment-node/package.json @@ -10,8 +10,8 @@ "main": "build/index.js", "types": "build/index.d.ts", "dependencies": { - "@jest/types": "^24.1.0", "@jest/fake-timers": "^24.1.0", + "@jest/types": "^24.1.0", "jest-mock": "^24.0.0", "jest-util": "^24.0.0" }, diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 07e4f93f6644..28473c6c5281 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -9,7 +9,7 @@ import vm, {Script, Context} from 'vm'; import {Global, Config} from '@jest/types'; import {ModuleMocker} from 'jest-mock'; import {installCommonGlobals} from 'jest-util'; -import FakeTimers from 'jest-util/build/FakeTimers'; +import {JestFakeTimers as FakeTimers} from '@jest/fake-timers'; type Timer = { id: number; diff --git a/packages/jest-environment-node/tsconfig.json b/packages/jest-environment-node/tsconfig.json index 96c241ae4d5e..6e8fbba86471 100644 --- a/packages/jest-environment-node/tsconfig.json +++ b/packages/jest-environment-node/tsconfig.json @@ -5,6 +5,7 @@ "rootDir": "src" }, "references": [ + {"path": "../jest-fake-timers"}, {"path": "../jest-mock"}, {"path": "../jest-types"}, {"path": "../jest-util"} From ae3c45c674509e546408a2e56ffb79132ef4c8c1 Mon Sep 17 00:00:00 2001 From: Mikael Lirbank Date: Tue, 26 Feb 2019 10:17:08 -0800 Subject: [PATCH 5/7] Implement JestEnvironment --- packages/jest-environment-node/package.json | 1 + packages/jest-environment-node/src/index.ts | 22 +++++++++++--------- packages/jest-environment-node/tsconfig.json | 1 + 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/jest-environment-node/package.json b/packages/jest-environment-node/package.json index 2fbbf37f0429..4b354dc856d5 100644 --- a/packages/jest-environment-node/package.json +++ b/packages/jest-environment-node/package.json @@ -10,6 +10,7 @@ "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", diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 28473c6c5281..b0b10569374d 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -10,6 +10,7 @@ import {Global, Config} from '@jest/types'; import {ModuleMocker} from 'jest-mock'; import {installCommonGlobals} from 'jest-util'; import {JestFakeTimers as FakeTimers} from '@jest/fake-timers'; +import {JestEnvironment} from '@jest/environment'; type Timer = { id: number; @@ -17,11 +18,11 @@ type Timer = { unref: () => Timer; }; -class NodeEnvironment { - context?: Context | null; - fakeTimers?: FakeTimers | null; - global?: Global.Global | null; - moduleMocker?: ModuleMocker | null; +class NodeEnvironment implements JestEnvironment { + context: Context | null; + fakeTimers: FakeTimers | null; + global: Global.Global; + moduleMocker: ModuleMocker | null; constructor(config: Config.ProjectConfig) { this.context = vm.createContext(); @@ -70,11 +71,11 @@ class NodeEnvironment { }); } - setup(): Promise { + setup() { return Promise.resolve(); } - teardown(): Promise { + teardown() { if (this.fakeTimers) { this.fakeTimers.dispose(); } @@ -83,8 +84,9 @@ class NodeEnvironment { return Promise.resolve(); } - // Disabling rule as return type depends on script's return type. - runScript(script: Script): any | null | undefined { + // TS infers the return type to be `any`, since that's what `runInContext` + // returns. + runScript(script: Script) { if (this.context) { return script.runInContext(this.context); } @@ -92,4 +94,4 @@ class NodeEnvironment { } } -module.exports = NodeEnvironment; +export = NodeEnvironment; diff --git a/packages/jest-environment-node/tsconfig.json b/packages/jest-environment-node/tsconfig.json index 6e8fbba86471..655d8c8aab7c 100644 --- a/packages/jest-environment-node/tsconfig.json +++ b/packages/jest-environment-node/tsconfig.json @@ -5,6 +5,7 @@ "rootDir": "src" }, "references": [ + {"path": "../jest-environment"}, {"path": "../jest-fake-timers"}, {"path": "../jest-mock"}, {"path": "../jest-types"}, From 71048f6ae7db66278a813daa55ba6f5ee55e5ad7 Mon Sep 17 00:00:00 2001 From: Mikael Lirbank Date: Tue, 26 Feb 2019 10:58:34 -0800 Subject: [PATCH 6/7] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6e9e2d3890b..fe3a1932a876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ ### Chore & Maintenance +- `[jest-environment-node]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/7985)) - `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855), [#7951](https://github.com/facebook/jest/pull/7951)) - `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972)) - `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820)) From cc4eacbfed94208b481273abb7b08d16740ccb22 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 26 Feb 2019 20:38:54 +0100 Subject: [PATCH 7/7] add to tsconfigs --- packages/jest-config/tsconfig.json | 6 ++++-- packages/jest-runtime/tsconfig.json | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/jest-config/tsconfig.json b/packages/jest-config/tsconfig.json index 50965f82bc07..55f2ed3ff353 100644 --- a/packages/jest-config/tsconfig.json +++ b/packages/jest-config/tsconfig.json @@ -4,9 +4,11 @@ "rootDir": "src", "outDir": "build" }, - // TODO: This is missing `jest-validate`, in addition to `jest-environment-jsdom`, `jest-environment-node` and - // `jest-jasmine2`, but those are just `require.resolve`d, so no real use for their types + // TODO: This is missing `jest-validate`, in addition to + // `jest-environment-jsdom` and `jest-jasmine2`, but those are just + // `require.resolve`d, so no real use for their types "references": [ + {"path": "../jest-environment-node"}, {"path": "../jest-get-type"}, {"path": "../jest-regex-util"}, {"path": "../jest-resolve"}, diff --git a/packages/jest-runtime/tsconfig.json b/packages/jest-runtime/tsconfig.json index 1b373a713a15..d94c744cb988 100644 --- a/packages/jest-runtime/tsconfig.json +++ b/packages/jest-runtime/tsconfig.json @@ -4,10 +4,11 @@ "rootDir": "src", "outDir": "build" }, - // TODO: Missing `jest-validate` and `jest-environment-node` + // TODO: Missing `jest-validate` "references": [ {"path": "../jest-config"}, {"path": "../jest-environment"}, + {"path": "../jest-environment-node"}, {"path": "../jest-haste-map"}, {"path": "../jest-message-util"}, {"path": "../jest-mock"},