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)) 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-environment-node/package.json b/packages/jest-environment-node/package.json index 374695a24a69..4b354dc856d5 100644 --- a/packages/jest-environment-node/package.json +++ b/packages/jest-environment-node/package.json @@ -8,8 +8,11 @@ }, "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" }, 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 67% rename from packages/jest-environment-node/src/index.js rename to packages/jest-environment-node/src/index.ts index 1ba6d72de0af..b0b10569374d 100644 --- a/packages/jest-environment-node/src/index.js +++ b/packages/jest-environment-node/src/index.ts @@ -3,33 +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 {JestFakeTimers as FakeTimers} from '@jest/fake-timers'; +import vm, {Script, Context} from 'vm'; +import {Global, Config} from '@jest/types'; +import {ModuleMocker} from 'jest-mock'; import {installCommonGlobals} from 'jest-util'; -import mock from 'jest-mock'; +import {JestFakeTimers as FakeTimers} from '@jest/fake-timers'; +import {JestEnvironment} from '@jest/environment'; -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; +class NodeEnvironment implements JestEnvironment { + context: Context | null; + fakeTimers: FakeTimers | null; + global: Global.Global; + moduleMocker: ModuleMocker | null; - constructor(config: ProjectConfig) { + constructor(config: Config.ProjectConfig) { this.context = vm.createContext(); const global = (this.global = vm.runInContext( 'this', @@ -48,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, @@ -60,7 +55,8 @@ class NodeEnvironment { }, }); - const timerRefToId = (timer: Timer): ?number => (timer && timer.id) || null; + const timerRefToId = (timer: Timer): number | undefined => + (timer && timer.id) || undefined; const timerConfig = { idToRef: timerIdToRef, @@ -75,11 +71,11 @@ class NodeEnvironment { }); } - setup(): Promise { + setup() { return Promise.resolve(); } - teardown(): Promise { + teardown() { if (this.fakeTimers) { this.fakeTimers.dispose(); } @@ -88,8 +84,9 @@ class NodeEnvironment { return Promise.resolve(); } - // Disabling rule as return type depends on script's return type. - runScript(script: Script): ?any { + // 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); } @@ -97,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 new file mode 100644 index 000000000000..655d8c8aab7c --- /dev/null +++ b/packages/jest-environment-node/tsconfig.json @@ -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"} + ] +} 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"},