1- import type { Config } from '@jest/types' ;
21import type { TestResult } from '@jest/test-result' ;
3- import type * as JestRunner from 'jest-runner' ;
4- import { Worker } from 'jest-worker' ;
2+ import {
3+ CallbackTestRunner ,
4+ OnTestFailure ,
5+ OnTestStart ,
6+ OnTestSuccess ,
7+ Test ,
8+ TestRunnerOptions ,
9+ TestWatcher ,
10+ } from 'jest-runner' ;
11+ import { Worker , JestWorkerFarm } from 'jest-worker' ;
512import throat from 'throat' ;
6- import type { CreateRunnerOptions , Path , TestRunner } from './types' ;
13+ import type { CreateRunnerOptions , RunTestOptions } from './types' ;
714
8- function determineSlowTestResult (
9- test : JestRunner . Test ,
10- result : TestResult ,
11- ) : TestResult {
15+ function determineSlowTestResult ( test : Test , result : TestResult ) : TestResult {
1216 // See: https://github.com/facebook/jest/blob/acd7c83c8365140f4ecf44a456ff7366ffa31fa2/packages/jest-runner/src/runTest.ts#L287
1317 if ( result . perfStats . runtime / 1000 > test . context . config . slowTestThreshold ) {
1418 return { ...result , perfStats : { ...result . perfStats , slow : true } } ;
@@ -24,24 +28,19 @@ class CancelRun extends Error {
2428}
2529
2630export default function createRunner <
27- ExtraOptionsType extends Record < string , unknown > ,
31+ ExtraOptions extends Record < string , unknown > ,
2832> (
29- runPath : Path ,
30- { getExtraOptions } : CreateRunnerOptions < ExtraOptionsType > = { } ,
31- ) : typeof TestRunner {
32- return class BaseTestRunner implements TestRunner {
33- constructor (
34- public readonly _globalConfig : Config . GlobalConfig ,
35- public readonly _context : JestRunner . TestRunnerContext = { } ,
36- ) { }
37-
33+ runPath : string ,
34+ { getExtraOptions } : CreateRunnerOptions < ExtraOptions > = { } ,
35+ ) : typeof CallbackTestRunner {
36+ return class BaseTestRunner extends CallbackTestRunner {
3837 runTests (
39- tests : Array < JestRunner . Test > ,
40- watcher : JestRunner . TestWatcher ,
41- onStart : JestRunner . OnTestStart ,
42- onResult : JestRunner . OnTestSuccess ,
43- onFailure : JestRunner . OnTestFailure ,
44- options : JestRunner . TestRunnerOptions ,
38+ tests : Array < Test > ,
39+ watcher : TestWatcher ,
40+ onStart : OnTestStart ,
41+ onResult : OnTestSuccess ,
42+ onFailure : OnTestFailure ,
43+ options : TestRunnerOptions ,
4544 ) : Promise < void > {
4645 return options . serial
4746 ? this . _createInBandTestRun (
@@ -63,12 +62,12 @@ export default function createRunner<
6362 }
6463
6564 _createInBandTestRun (
66- tests : Array < JestRunner . Test > ,
67- watcher : JestRunner . TestWatcher ,
68- onStart : JestRunner . OnTestStart ,
69- onResult : JestRunner . OnTestSuccess ,
70- onFailure : JestRunner . OnTestFailure ,
71- options : JestRunner . TestRunnerOptions ,
65+ tests : Array < Test > ,
66+ watcher : TestWatcher ,
67+ onStart : OnTestStart ,
68+ onResult : OnTestSuccess ,
69+ onFailure : OnTestFailure ,
70+ options : TestRunnerOptions ,
7271 ) : Promise < void > {
7372 const mutex = throat ( 1 ) ;
7473 return tests . reduce (
@@ -110,29 +109,31 @@ export default function createRunner<
110109 }
111110
112111 _createParallelTestRun (
113- tests : Array < JestRunner . Test > ,
114- watcher : JestRunner . TestWatcher ,
115- onStart : JestRunner . OnTestStart ,
116- onResult : JestRunner . OnTestSuccess ,
117- onFailure : JestRunner . OnTestFailure ,
118- options : JestRunner . TestRunnerOptions ,
112+ tests : Array < Test > ,
113+ watcher : TestWatcher ,
114+ onStart : OnTestStart ,
115+ onResult : OnTestSuccess ,
116+ onFailure : OnTestFailure ,
117+ options : TestRunnerOptions ,
119118 ) : Promise < void > {
120119 const worker = new Worker ( runPath , {
121120 exposedMethods : [ 'default' ] ,
122121 numWorkers : this . _globalConfig . maxWorkers ,
123122 forkOptions : { stdio : 'inherit' } ,
124- } ) ;
123+ } ) as JestWorkerFarm < {
124+ default : ( runTestOptions : RunTestOptions ) => TestResult ;
125+ } > ;
125126
126127 const mutex = throat ( this . _globalConfig . maxWorkers ) ;
127128
128- const runTestInWorker = ( test : JestRunner . Test ) =>
129+ const runTestInWorker = ( test : Test ) =>
129130 mutex ( ( ) => {
130131 if ( watcher . isInterrupted ( ) ) {
131132 throw new CancelRun ( ) ;
132133 }
133134
134135 return onStart ( test ) . then ( ( ) => {
135- const baseOptions = {
136+ const runTestOptions : RunTestOptions = {
136137 config : test . context . config ,
137138 globalConfig : this . _globalConfig ,
138139 testPath : test . path ,
@@ -143,8 +144,7 @@ export default function createRunner<
143144 extraOptions : getExtraOptions ? getExtraOptions ( ) : { } ,
144145 } ;
145146
146- // @ts -expect-error -- the required module should have a default export
147- return worker . default ( baseOptions ) ;
147+ return worker . default ( runTestOptions ) ;
148148 } ) ;
149149 } ) ;
150150
0 commit comments