Skip to content

Commit 3bd384b

Browse files
Mikael LirbankSimenB
authored andcommitted
chore: migrate jest-environment-node to TypeScript (#7985)
1 parent a782ce5 commit 3bd384b

File tree

7 files changed

+49
-31
lines changed

7 files changed

+49
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
### Chore & Maintenance
2828

29+
- `[jest-environment-node]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/7985))
2930
- `[*]`: 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))
3031
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972))
3132
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))

packages/jest-config/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"rootDir": "src",
55
"outDir": "build"
66
},
7-
// TODO: This is missing `jest-validate`, in addition to `jest-environment-jsdom`, `jest-environment-node` and
8-
// `jest-jasmine2`, but those are just `require.resolve`d, so no real use for their types
7+
// TODO: This is missing `jest-validate`, in addition to
8+
// `jest-environment-jsdom` and `jest-jasmine2`, but those are just
9+
// `require.resolve`d, so no real use for their types
910
"references": [
11+
{"path": "../jest-environment-node"},
1012
{"path": "../jest-get-type"},
1113
{"path": "../jest-regex-util"},
1214
{"path": "../jest-resolve"},

packages/jest-environment-node/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
},
99
"license": "MIT",
1010
"main": "build/index.js",
11+
"types": "build/index.d.ts",
1112
"dependencies": {
13+
"@jest/environment": "^24.1.0",
1214
"@jest/fake-timers": "^24.1.0",
15+
"@jest/types": "^24.1.0",
1316
"jest-mock": "^24.0.0",
1417
"jest-util": "^24.0.0"
1518
},

packages/jest-environment-node/src/index.js renamed to packages/jest-environment-node/src/index.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,28 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {Script} from 'vm';
11-
import type {ProjectConfig} from 'types/Config';
12-
import type {Global} from 'types/Global';
13-
import type {ModuleMocker} from 'jest-mock';
14-
15-
import vm from 'vm';
16-
import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
8+
import vm, {Script, Context} from 'vm';
9+
import {Global, Config} from '@jest/types';
10+
import {ModuleMocker} from 'jest-mock';
1711
import {installCommonGlobals} from 'jest-util';
18-
import mock from 'jest-mock';
12+
import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
13+
import {JestEnvironment} from '@jest/environment';
1914

20-
type Timer = {|
21-
id: number,
22-
ref: () => Timer,
23-
unref: () => Timer,
24-
|};
15+
type Timer = {
16+
id: number;
17+
ref: () => Timer;
18+
unref: () => Timer;
19+
};
2520

26-
class NodeEnvironment {
27-
context: ?vm$Context;
28-
fakeTimers: ?FakeTimers<Timer>;
29-
global: ?Global;
30-
moduleMocker: ?ModuleMocker;
21+
class NodeEnvironment implements JestEnvironment {
22+
context: Context | null;
23+
fakeTimers: FakeTimers<Timer> | null;
24+
global: Global.Global;
25+
moduleMocker: ModuleMocker | null;
3126

32-
constructor(config: ProjectConfig) {
27+
constructor(config: Config.ProjectConfig) {
3328
this.context = vm.createContext();
3429
const global = (this.global = vm.runInContext(
3530
'this',
@@ -48,7 +43,7 @@ class NodeEnvironment {
4843
global.URLSearchParams = URLSearchParams;
4944
}
5045
installCommonGlobals(global, config.globals);
51-
this.moduleMocker = new mock.ModuleMocker(global);
46+
this.moduleMocker = new ModuleMocker(global);
5247

5348
const timerIdToRef = (id: number) => ({
5449
id,
@@ -60,7 +55,8 @@ class NodeEnvironment {
6055
},
6156
});
6257

63-
const timerRefToId = (timer: Timer): ?number => (timer && timer.id) || null;
58+
const timerRefToId = (timer: Timer): number | undefined =>
59+
(timer && timer.id) || undefined;
6460

6561
const timerConfig = {
6662
idToRef: timerIdToRef,
@@ -75,11 +71,11 @@ class NodeEnvironment {
7571
});
7672
}
7773

78-
setup(): Promise<void> {
74+
setup() {
7975
return Promise.resolve();
8076
}
8177

82-
teardown(): Promise<void> {
78+
teardown() {
8379
if (this.fakeTimers) {
8480
this.fakeTimers.dispose();
8581
}
@@ -88,13 +84,14 @@ class NodeEnvironment {
8884
return Promise.resolve();
8985
}
9086

91-
// Disabling rule as return type depends on script's return type.
92-
runScript(script: Script): ?any {
87+
// TS infers the return type to be `any`, since that's what `runInContext`
88+
// returns.
89+
runScript(script: Script) {
9390
if (this.context) {
9491
return script.runInContext(this.context);
9592
}
9693
return null;
9794
}
9895
}
9996

100-
module.exports = NodeEnvironment;
97+
export = NodeEnvironment;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "build",
5+
"rootDir": "src"
6+
},
7+
"references": [
8+
{"path": "../jest-environment"},
9+
{"path": "../jest-fake-timers"},
10+
{"path": "../jest-mock"},
11+
{"path": "../jest-types"},
12+
{"path": "../jest-util"}
13+
]
14+
}

packages/jest-runtime/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"rootDir": "src",
55
"outDir": "build"
66
},
7-
// TODO: Missing `jest-validate` and `jest-environment-node`
7+
// TODO: Missing `jest-validate`
88
"references": [
99
{"path": "../jest-config"},
1010
{"path": "../jest-environment"},
11+
{"path": "../jest-environment-node"},
1112
{"path": "../jest-haste-map"},
1213
{"path": "../jest-message-util"},
1314
{"path": "../jest-mock"},

0 commit comments

Comments
 (0)