@@ -174,27 +174,32 @@ export async function runScriptAndGetProcessInfo(
174174 REPORT_RESOURCE_SCRIPT_PATH ,
175175 func
176176 ) ;
177- await writeFile ( scriptName , scriptContent , { encoding : 'utf8' } ) ;
178- const logFile = name + '.logs.txt' ;
179177
178+ const logFile = name + '.logs.txt' ;
180179 const stdErrFile = 'err.out' ;
180+
181+ await unlink ( scriptName ) . catch ( ( ) => null ) ;
182+ await unlink ( logFile ) . catch ( ( ) => null ) ;
183+ await unlink ( stdErrFile ) . catch ( ( ) => null ) ;
184+
185+ await writeFile ( scriptName , scriptContent , { encoding : 'utf8' } ) ;
186+
181187 const script = spawn ( process . execPath , [ scriptName ] , {
182188 stdio : [ 'ignore' , 'ignore' , openSync ( stdErrFile , 'w' ) ]
183189 } ) ;
184190
185191 const willClose = once ( script , 'close' ) ;
186192
187193 // make sure the process ended
188- const [ exitCode ] = await willClose ;
194+ const [ exitCode ] = ( await willClose ) as [ number ] ;
189195
190196 // format messages from child process as an object
191197 const messages = ( await readFile ( logFile , 'utf-8' ) )
192198 . trim ( )
193199 . split ( '\n' )
194- . map ( line => JSON . parse ( line ) )
195- . reduce ( ( acc , curr ) => ( { ...acc , ...curr } ) , { } ) ;
200+ . map ( line => JSON . parse ( line ) ) ;
196201
197- const stdErrSize = await readFile ( stdErrFile , { encoding : 'utf8' } ) ;
202+ const stdErr = await readFile ( stdErrFile , { encoding : 'utf8' } ) ;
198203
199204 // delete temporary files
200205 await unlink ( scriptName ) ;
@@ -203,18 +208,23 @@ export async function runScriptAndGetProcessInfo(
203208
204209 // assertions about exit status
205210 if ( exitCode ) {
211+ const { error } = messages . find ( m => m . error != null ) ;
212+ expect ( error ) . to . exist ;
206213 const assertionError = new AssertionError (
207- messages . error ? .message + '\n\t' + JSON . stringify ( messages . error ? .resources , undefined , 2 )
214+ error . message + '\n\t' + JSON . stringify ( error . resources , undefined , 2 )
208215 ) ;
209- assertionError . stack = messages . error ? .stack + new Error ( ) . stack . slice ( 'Error' . length ) ;
216+ assertionError . stack = error . stack + new Error ( ) . stack . slice ( 'Error' . length ) ;
210217 throw assertionError ;
211218 }
212219
213220 // assertions about resource status
214- expect ( messages . beforeExitHappened ) . to . be . true ;
215- expect ( messages . newResources . libuvResources ) . to . be . empty ;
216- expect ( messages . newResources . activeResources ) . to . be . empty ;
221+ const { beforeExitHappened } = messages . find ( m => 'beforeExitHappened' in m ) ;
222+ const { newResources } = messages . find ( m => 'newResources' in m ) ;
223+
224+ expect ( beforeExitHappened ) . to . be . true ;
225+ expect ( newResources . libuvResources ) . to . be . empty ;
226+ expect ( newResources . activeResources ) . to . be . empty ;
217227
218228 // assertion about error output
219- expect ( stdErrSize ) . to . be . empty ;
229+ expect ( stdErr ) . to . be . empty ;
220230}
0 commit comments