Skip to content

Commit 5f6d79a

Browse files
committed
chore: migrate jest-runtime to TypeScript
1 parent fddf439 commit 5f6d79a

25 files changed

Lines changed: 522 additions & 281 deletions

File tree

packages/jest-circus/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"types": "build/index.d.ts",
1212
"dependencies": {
1313
"@babel/traverse": "^7.1.0",
14+
"@jest/environment": "^24.1.0",
1415
"@jest/types": "^24.1.0",
1516
"@types/node": "*",
1617
"chalk": "^2.0.1",

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77

88
import path from 'path';
9-
import {Config, TestResult, Environment} from '@jest/types';
9+
import {Config, TestResult} from '@jest/types';
10+
import {JestEnvironment} from '@jest/environment';
1011
// @ts-ignore TODO Remove ignore when jest-runtime is migrated to ts
1112
import Runtime from 'jest-runtime'; // eslint-disable-line import/no-extraneous-dependencies
1213
import {SnapshotState} from 'jest-snapshot';
@@ -16,7 +17,7 @@ const FRAMEWORK_INITIALIZER = require.resolve('./jestAdapterInit');
1617
const jestAdapter = async (
1718
globalConfig: Config.GlobalConfig,
1819
config: Config.ProjectConfig,
19-
environment: Environment.$JestEnvironment,
20+
environment: JestEnvironment,
2021
runtime: Runtime,
2122
testPath: string,
2223
): Promise<TestResult.TestResult> => {

packages/jest-circus/tsconfig.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@
44
"outDir": "build",
55
"rootDir": "src"
66
},
7-
"references": [{"path": "../jest-types"}, {"path": "../jest-snapshot"}, {"path": "../jest-matcher-utils"}, {"path": "../jest-message-util"}, {"path": "../jest-util"}, {"path": "../pretty-format"}, {"path": "../jest-diff"}]
7+
"references": [
8+
{"path": "../jest-environment"},
9+
{"path": "../jest-types"},
10+
{"path": "../jest-snapshot"},
11+
{"path": "../jest-matcher-utils"},
12+
{"path": "../jest-message-util"},
13+
{"path": "../jest-util"},
14+
{"path": "../pretty-format"},
15+
{"path": "../jest-diff"}
16+
]
817
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@jest/environment",
3+
"version": "24.1.0",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/facebook/jest.git",
7+
"directory": "packages/jest-environment"
8+
},
9+
"license": "MIT",
10+
"main": "build/index.js",
11+
"types": "build/index.d.ts",
12+
"dependencies": {
13+
"@jest/transform": "^24.1.0",
14+
"@jest/types": "^24.1.0",
15+
"@types/node": "*",
16+
"jest-mock": "^24.0.0"
17+
},
18+
"engines": {
19+
"node": ">= 6"
20+
},
21+
"gitHead": "b16789230fd45056a7f2fa199bae06c7a1780deb"
22+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {Script} from 'vm';
9+
import {Config} from '@jest/types';
10+
import moduleMocker from 'jest-mock';
11+
import {ScriptTransformer} from '@jest/transform';
12+
13+
export type EnvironmentContext = {
14+
console?: Console;
15+
testPath?: Config.Path;
16+
};
17+
18+
// TODO: Get rid of this at some point
19+
type JasmineType = {_DEFAULT_TIMEOUT_INTERVAL?: number; addMatchers: Function};
20+
21+
// TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper
22+
type ModuleWrapper = (...args: Array<unknown>) => unknown;
23+
24+
export interface JestEnvironment {
25+
new (
26+
config: Config.ProjectConfig,
27+
context?: EnvironmentContext,
28+
): JestEnvironment;
29+
runScript(
30+
script: Script,
31+
): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null;
32+
// TODO: Maybe add `| Window` in the future?
33+
global: NodeJS.Global & {__coverage__: any; jasmine: JasmineType};
34+
// TODO: When `jest-util` is ESM, this can just be `fakeTimers: import('jest-util').FakeTimers`
35+
fakeTimers: {
36+
clearAllTimers(): void;
37+
runAllImmediates(): void;
38+
runAllTicks(): void;
39+
runAllTimers(): void;
40+
advanceTimersByTime(msToRun: number): void;
41+
runOnlyPendingTimers(): void;
42+
runWithRealTimers(callback: any): void;
43+
getTimerCount(): number;
44+
useFakeTimers(): void;
45+
useRealTimers(): void;
46+
};
47+
testFilePath: Config.Path;
48+
moduleMocker: typeof moduleMocker;
49+
setup(): Promise<void>;
50+
teardown(): Promise<void>;
51+
}
52+
53+
export type Module = typeof module;
54+
55+
export type LocalModuleRequire = typeof require & {
56+
requireActual: (moduleName: string) => unknown;
57+
requireMock: (moduleName: string) => unknown;
58+
};
59+
60+
export type Jest = {
61+
addMatchers(matchers: Object): void;
62+
autoMockOff(): Jest;
63+
autoMockOn(): Jest;
64+
clearAllMocks(): Jest;
65+
clearAllTimers(): void;
66+
deepUnmock(moduleName: string): Jest;
67+
disableAutomock(): Jest;
68+
doMock(moduleName: string, moduleFactory?: () => unknown): Jest;
69+
dontMock(moduleName: string): Jest;
70+
enableAutomock(): Jest;
71+
fn: typeof moduleMocker.fn;
72+
genMockFromModule(moduleName: string): any;
73+
isMockFunction(fn: Function): boolean;
74+
mock(
75+
moduleName: string,
76+
moduleFactory?: () => unknown,
77+
options?: {virtual?: boolean},
78+
): Jest;
79+
requireActual: LocalModuleRequire['requireActual'];
80+
requireMock: LocalModuleRequire['requireMock'];
81+
resetAllMocks(): Jest;
82+
resetModuleRegistry(): Jest;
83+
resetModules(): Jest;
84+
restoreAllMocks(): Jest;
85+
retryTimes(numRetries: number): Jest;
86+
runAllImmediates(): void;
87+
runAllTicks(): void;
88+
runAllTimers(): void;
89+
runOnlyPendingTimers(): void;
90+
advanceTimersByTime(msToRun: number): void;
91+
runTimersToTime(msToRun: number): void;
92+
getTimerCount(): number;
93+
setMock(moduleName: string, moduleExports: any): Jest;
94+
setTimeout(timeout: number): Jest;
95+
spyOn: typeof moduleMocker.spyOn;
96+
unmock(moduleName: string): Jest;
97+
useFakeTimers(): Jest;
98+
useRealTimers(): Jest;
99+
isolateModules(fn: () => void): Jest;
100+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [
8+
{"path": "../jest-transform"},
9+
{"path": "../jest-types"},
10+
{"path": "../jest-util"}
11+
]
12+
}

packages/jest-resolve/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type FindNodeModuleConfig = {
2121
extensions?: Array<string>;
2222
moduleDirectory?: Array<string>;
2323
paths?: Array<Config.Path>;
24-
resolver?: Config.Path;
24+
resolver?: Config.Path | null;
2525
rootDir?: Config.Path;
2626
};
2727

@@ -403,7 +403,7 @@ const createNoMappedModuleFoundError = (
403403
updatedName: string,
404404
mappedModuleName: string,
405405
regex: RegExp,
406-
resolver: Function | string,
406+
resolver?: Function | string | null,
407407
) => {
408408
const error = new Error(
409409
chalk.red(`${chalk.bold('Configuration error')}:

packages/jest-resolve/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {Config} from '@jest/types';
99

1010
export type ResolverConfig = {
1111
browser?: boolean;
12-
defaultPlatform?: string;
12+
defaultPlatform?: string | null;
1313
extensions: Array<string>;
1414
hasCoreModules: boolean;
1515
moduleDirectories: Array<string>;
16-
moduleNameMapper?: Array<ModuleNameMapperConfig>;
16+
moduleNameMapper?: Array<ModuleNameMapperConfig> | null;
1717
modulePaths: Array<Config.Path>;
1818
platforms?: Array<string>;
19-
resolver: Config.Path;
19+
resolver?: Config.Path | null;
2020
rootDir: Config.Path;
2121
};
2222

packages/jest-runtime/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
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/transform": "^24.1.0",
15+
"@jest/types": "^24.1.0",
1316
"chalk": "^2.0.1",
1417
"exit": "^0.1.2",
1518
"glob": "^7.1.3",
1619
"graceful-fs": "^4.1.15",
1720
"jest-config": "^24.1.0",
1821
"jest-haste-map": "^24.0.0",
1922
"jest-message-util": "^24.0.0",
23+
"jest-mock": "^24.0.0",
2024
"jest-regex-util": "^24.0.0",
2125
"jest-resolve": "^24.1.0",
2226
"jest-snapshot": "^24.1.0",

0 commit comments

Comments
 (0)