44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
66 *
7- * @flow
87 */
98
10- import type { EnvironmentClass } from 'types/Environment' ;
11- import type { GlobalConfig , Path , ProjectConfig } from 'types/Config' ;
12- import type { Resolver } from 'types/Resolve' ;
13- import type { TestFramework , TestRunnerContext } from 'types/TestRunner' ;
14- import type { TestResult } from 'types/TestResult' ;
15- import type RuntimeClass from 'jest-runtime' ;
16-
9+ import {
10+ Environment ,
11+ Config ,
12+ TestResult ,
13+ Console as ConsoleType ,
14+ } from '@jest/types' ;
15+ // @ts -ignore: not migrated to TS
16+ import RuntimeClass from 'jest-runtime' ;
1717import fs from 'graceful-fs' ;
1818import {
1919 BufferedConsole ,
@@ -24,22 +24,31 @@ import {
2424 setGlobal ,
2525} from 'jest-util' ;
2626import LeakDetector from 'jest-leak-detector' ;
27+ import Resolver from 'jest-resolve' ;
28+ // @ts -ignore: not migrated to TS
2729import { getTestEnvironment } from 'jest-config' ;
2830import * as docblock from 'jest-docblock' ;
2931import { formatExecError } from 'jest-message-util' ;
30- import sourcemapSupport from 'source-map-support' ;
32+ import sourcemapSupport , {
33+ Options as SourceMapOptions ,
34+ } from 'source-map-support' ;
3135import chalk from 'chalk' ;
36+ import { TestFramework , TestRunnerContext } from './types' ;
3237
3338type RunTestInternalResult = {
34- leakDetector : ? LeakDetector ,
35- result : TestResult ,
39+ leakDetector : LeakDetector | null ;
40+ result : TestResult . TestResult ;
3641} ;
3742
3843function freezeConsole (
44+ // @ts -ignore: Correct types when `jest-util` is ESM
3945 testConsole : BufferedConsole | Console | NullConsole ,
40- config : ProjectConfig ,
46+ config : Config . ProjectConfig ,
4147) {
42- testConsole . _log = function fakeConsolePush ( _type , message ) {
48+ testConsole . _log = function fakeConsolePush (
49+ _type : ConsoleType . LogType ,
50+ message : ConsoleType . LogMessage ,
51+ ) {
4352 const error = new ErrorWithStack (
4453 `${ chalk . red (
4554 `${ chalk . bold (
@@ -73,11 +82,11 @@ function freezeConsole(
7382// references to verify if there is a leak, which is not maintainable and error
7483// prone. That's why "runTestInternal" CANNOT be inlined inside "runTest".
7584async function runTestInternal (
76- path : Path ,
77- globalConfig : GlobalConfig ,
78- config : ProjectConfig ,
85+ path : Config . Path ,
86+ globalConfig : Config . GlobalConfig ,
87+ config : Config . ProjectConfig ,
7988 resolver : Resolver ,
80- context : ? TestRunnerContext ,
89+ context ?: TestRunnerContext ,
8190) : Promise < RunTestInternalResult > {
8291 const testSource = fs . readFileSync ( path , 'utf8' ) ;
8392 const parsedDocblock = docblock . parse ( docblock . extract ( testSource ) ) ;
@@ -92,21 +101,22 @@ async function runTestInternal(
92101 } ) ;
93102 }
94103
95- /* $FlowFixMe */
96- const TestEnvironment = ( require ( testEnvironment ) : EnvironmentClass ) ;
97- const testFramework = ( ( process . env . JEST_CIRCUS === '1'
98- ? require ( 'jest-circus/runner' ) // eslint-disable-line import/no-extraneous-dependencies
99- : /* $FlowFixMe */
100- require ( config . testRunner ) ) : TestFramework ) ;
101- const Runtime = ( ( config . moduleLoader
102- ? /* $FlowFixMe */
103- require ( config . moduleLoader )
104- : require ( 'jest-runtime' ) ) : Class < RuntimeClass > ) ;
104+ const TestEnvironment : Environment . EnvironmentClass = require ( testEnvironment ) ;
105+ const testFramework : TestFramework =
106+ process . env . JEST_CIRCUS === '1'
107+ ? require ( 'jest-circus/runner' ) // eslint-disable-line import/no-extraneous-dependencies
108+ : require ( config . testRunner ) ;
109+ const Runtime : RuntimeClass = config . moduleLoader
110+ ? require ( config . moduleLoader )
111+ : require ( 'jest-runtime' ) ;
105112
106- let runtime = undefined ;
113+ let runtime : RuntimeClass = undefined ;
107114
108115 const consoleOut = globalConfig . useStderr ? process . stderr : process . stdout ;
109- const consoleFormatter = ( type , message ) =>
116+ const consoleFormatter = (
117+ type : ConsoleType . LogType ,
118+ message : ConsoleType . LogMessage ,
119+ ) =>
110120 getConsoleOutput (
111121 config . cwd ,
112122 ! ! globalConfig . verbose ,
@@ -150,7 +160,7 @@ async function runTestInternal(
150160
151161 const start = Date . now ( ) ;
152162
153- const sourcemapOptions = {
163+ const sourcemapOptions : SourceMapOptions = {
154164 environment : 'node' ,
155165 handleUncaughtExceptions : false ,
156166 retrieveSourceMap : source => {
@@ -160,7 +170,7 @@ async function runTestInternal(
160170 if ( sourceMapSource ) {
161171 try {
162172 return {
163- map : JSON . parse ( fs . readFileSync ( sourceMapSource ) ) ,
173+ map : JSON . parse ( fs . readFileSync ( sourceMapSource , 'utf8' ) ) ,
164174 url : source ,
165175 } ;
166176 } catch ( e ) { }
@@ -187,7 +197,7 @@ async function runTestInternal(
187197 ) {
188198 const realExit = environment . global . process . exit ;
189199
190- environment . global . process . exit = function exit ( ...args ) {
200+ environment . global . process . exit = function exit ( ...args : Array < any > ) {
191201 const error = new ErrorWithStack (
192202 `process.exit called with "${ args . join ( ', ' ) } "` ,
193203 exit ,
@@ -210,7 +220,7 @@ async function runTestInternal(
210220 try {
211221 await environment . setup ( ) ;
212222
213- let result : TestResult ;
223+ let result : TestResult . TestResult ;
214224
215225 try {
216226 result = await testFramework (
@@ -259,17 +269,18 @@ async function runTestInternal(
259269 } finally {
260270 await environment . teardown ( ) ;
261271
272+ // @ts -ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33351
262273 sourcemapSupport . resetRetrieveHandlers ( ) ;
263274 }
264275}
265276
266277export default async function runTest (
267- path : Path ,
268- globalConfig : GlobalConfig ,
269- config : ProjectConfig ,
278+ path : Config . Path ,
279+ globalConfig : Config . GlobalConfig ,
280+ config : Config . ProjectConfig ,
270281 resolver : Resolver ,
271- context : ? TestRunnerContext ,
272- ) : Promise < TestResult > {
282+ context ?: TestRunnerContext ,
283+ ) : Promise < TestResult . TestResult > {
273284 const { leakDetector, result} = await runTestInternal (
274285 path ,
275286 globalConfig ,
0 commit comments