@@ -3,6 +3,7 @@ import { on, once } from 'node:events';
33import { openSync } from 'node:fs' ;
44import { readFile , unlink , writeFile } from 'node:fs/promises' ;
55import * as path from 'node:path' ;
6+ import { inspect } from 'node:util' ;
67
78import { AssertionError , expect } from 'chai' ;
89import type * as timers from 'timers' ;
@@ -92,6 +93,18 @@ export async function runScriptAndReturnHeapInfo(
9293 func : HeapResourceTestFunction ,
9394 { iterations = 100 } = { }
9495) {
96+ const log = ( ...args ) => {
97+ const payload =
98+ args
99+ . map ( item =>
100+ typeof item === 'string'
101+ ? item
102+ : inspect ( item , { depth : Infinity , breakLength : Infinity } )
103+ )
104+ . join ( ', ' ) + '\n' ;
105+ process . stdout . write ( payload ) ;
106+ } ;
107+ log ( 'starting' ) ;
95108 const scriptName = `${ name } .cjs` ;
96109 const heapsnapshotFile = `${ name } .heapsnapshot.json` ;
97110
@@ -105,31 +118,49 @@ export async function runScriptAndReturnHeapInfo(
105118 await writeFile ( scriptName , scriptContent , { encoding : 'utf8' } ) ;
106119
107120 const processDiedController = new AbortController ( ) ;
108- const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] } ) ;
121+ const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] , stdio : 'inherit' } ) ;
109122 // Interrupt our awaiting of messages if the process crashed
110123 script . once ( 'close' , exitCode => {
111124 if ( exitCode !== 0 ) {
125+ log ( 'process exited with non-zero: ' , exitCode ) ;
112126 processDiedController . abort ( new Error ( `process exited with: ${ exitCode } ` ) ) ;
113127 }
114128 } ) ;
115129
130+ script . once ( 'error' , error => {
131+ log ( `processed errored: ${ error } ` ) ;
132+ processDiedController . abort ( new Error ( `process errored: ` , { cause : error } ) ) ;
133+ } ) ;
134+
135+ script . once ( 'spawn' , ( ) => log ( 'script spawned successfully.' ) ) ;
136+
116137 const messages = on ( script , 'message' , { signal : processDiedController . signal } ) ;
117138 const willClose = once ( script , 'close' ) ;
118139
140+ log ( 'fetching messages 1...' ) ;
119141 const starting = await messages . next ( ) ;
142+ log ( 'fetching messages 2: ' , starting ) ;
143+
120144 const ending = await messages . next ( ) ;
121145
146+ log ( 'fetching messages 3: ' , ending ) ;
147+
122148 const startingMemoryUsed = starting . value [ 0 ] . startingMemoryUsed ;
123149 const endingMemoryUsed = ending . value [ 0 ] . endingMemoryUsed ;
124150
125151 // make sure the process ended
126152 const [ exitCode ] = await willClose ;
153+
154+ log ( 'child process closed.' ) ;
155+
127156 expect ( exitCode , 'process should have exited with zero' ) . to . equal ( 0 ) ;
128157
129158 const heap = await readFile ( heapsnapshotFile , { encoding : 'utf8' } ) . then ( c =>
130159 parseSnapshot ( JSON . parse ( c ) )
131160 ) ;
132161
162+ log ( 'done.' ) ;
163+
133164 // If any of the above throws we won't reach these unlinks that clean up the created files.
134165 // This is intentional so that when debugging the file will still be present to check it for errors
135166 await unlink ( scriptName ) ;
0 commit comments