@@ -265,6 +265,14 @@ function isProcessAlive(pid: number): boolean {
265265 }
266266}
267267
268+ function readCompletePidFile ( pidPath : string ) : number | undefined {
269+ if ( ! existsSync ( pidPath ) ) {
270+ return undefined ;
271+ }
272+ const pid = Number . parseInt ( readFileSync ( pidPath , "utf8" ) , 10 ) ;
273+ return Number . isInteger ( pid ) ? pid : undefined ;
274+ }
275+
268276async function waitFor ( predicate : ( ) => boolean , timeoutMs = 5_000 ) : Promise < void > {
269277 const started = Date . now ( ) ;
270278 while ( Date . now ( ) - started < timeoutMs ) {
@@ -1275,9 +1283,10 @@ setInterval(() => {}, 1000);
12751283 timeoutMs : 250 ,
12761284 } ) ;
12771285
1278- await waitFor ( ( ) => existsSync ( grandchildPidPath ) ) ;
1279- grandchildPid = Number . parseInt ( readFileSync ( grandchildPidPath , "utf8" ) , 10 ) ;
1280- expect ( Number . isInteger ( grandchildPid ) ) . toBe ( true ) ;
1286+ await waitFor ( ( ) => {
1287+ grandchildPid = readCompletePidFile ( grandchildPidPath ) ?? 0 ;
1288+ return grandchildPid > 0 ;
1289+ } ) ;
12811290 expect ( isProcessAlive ( grandchildPid ) ) . toBe ( true ) ;
12821291
12831292 await expect ( runPromise ) . resolves . toMatchObject ( { timedOut : true } ) ;
@@ -1494,9 +1503,10 @@ await runShellCommand({
14941503 cwd : process . cwd ( ) ,
14951504 stdio : [ "ignore" , "ignore" , "pipe" ] ,
14961505 } ) ;
1497- await waitFor ( ( ) => existsSync ( readyPath ) && existsSync ( grandchildPidPath ) ) ;
1498- grandchildPid = Number . parseInt ( readFileSync ( grandchildPidPath , "utf8" ) , 10 ) ;
1499- expect ( Number . isInteger ( grandchildPid ) ) . toBe ( true ) ;
1506+ await waitFor ( ( ) => {
1507+ grandchildPid = readCompletePidFile ( grandchildPidPath ) ?? 0 ;
1508+ return existsSync ( readyPath ) && grandchildPid > 0 ;
1509+ } ) ;
15001510 expect ( isProcessAlive ( grandchildPid ) ) . toBe ( true ) ;
15011511
15021512 runner . kill ( "SIGTERM" ) ;
0 commit comments