@@ -312,6 +312,110 @@ describe('server', () => {
312312 expect ( await exitCode ( ) ) . to . have . equal ( 15 )
313313 } )
314314
315+ it ( 'given on run_complete with exit event listener (15)' , async ( ) => {
316+ mockProcess ( process )
317+
318+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
319+ resolveExitCode ( exitCode )
320+ } )
321+
322+ // last non-zero exit code will be taken
323+ server . on ( 'exit' , ( done ) => {
324+ setTimeout ( ( ) => done ( 30 ) )
325+ } )
326+ server . on ( 'exit' , ( done ) => {
327+ setTimeout ( ( ) => done ( 15 ) )
328+ } )
329+ server . on ( 'exit' , ( done ) => {
330+ setTimeout ( ( ) => done ( 0 ) )
331+ } )
332+
333+ // Provided run_complete exitCode will be overridden by exit listeners
334+ server . emit ( 'run_complete' , browserCollection , { exitCode : 5 } )
335+
336+ function mockProcess ( process ) {
337+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
338+ }
339+ expect ( await exitCode ( ) ) . to . have . equal ( 15 )
340+ } )
341+
342+ it ( 'given on run_complete with exit event listener (0)' , async ( ) => {
343+ mockProcess ( process )
344+
345+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
346+ resolveExitCode ( exitCode )
347+ } )
348+
349+ // exit listeners can't set exit code back to 0
350+ server . on ( 'exit' , ( done ) => {
351+ setTimeout ( ( ) => done ( 0 ) )
352+ } )
353+
354+ server . emit ( 'run_complete' , browserCollection , { exitCode : 15 } )
355+
356+ function mockProcess ( process ) {
357+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
358+ }
359+ expect ( await exitCode ( ) ) . to . have . equal ( 15 )
360+ } )
361+
362+ it ( '1 on run_complete with exit event listener throws' , async ( ) => {
363+ mockProcess ( process )
364+
365+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
366+ resolveExitCode ( exitCode )
367+ } )
368+
369+ server . on ( 'exit' , ( done ) => {
370+ throw new Error ( 'async error from exit event listener' )
371+ } )
372+
373+ server . emit ( 'run_complete' , browserCollection , { exitCode : 0 } )
374+
375+ function mockProcess ( process ) {
376+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
377+ }
378+ expect ( await exitCode ( ) ) . to . have . equal ( 1 )
379+ } )
380+
381+ it ( '1 on run_complete with exit event listener rejects' , async ( ) => {
382+ mockProcess ( process )
383+
384+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
385+ resolveExitCode ( exitCode )
386+ } )
387+
388+ function onExit ( done ) {
389+ // Need to remove listener to prevent endless loop via unhandledRejection handler
390+ // which again calls disconnectBrowsers to fire the 'exit' event
391+ server . off ( 'exit' , onExit )
392+ return Promise . reject ( new Error ( 'async error from exit event listener' ) )
393+ }
394+ server . on ( 'exit' , onExit )
395+
396+ server . emit ( 'run_complete' , browserCollection , { exitCode : 0 } )
397+
398+ function mockProcess ( process ) {
399+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
400+ }
401+ expect ( await exitCode ( ) ) . to . have . equal ( 1 )
402+ } )
403+
404+ it ( '0 on server stop' , async ( ) => {
405+ mockProcess ( process )
406+
407+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
408+ resolveExitCode ( exitCode )
409+ } )
410+
411+ server . stop ( )
412+
413+ function mockProcess ( process ) {
414+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
415+ }
416+ expect ( await exitCode ( ) ) . to . have . equal ( 0 )
417+ } )
418+
315419 it ( '1 on browser_process_failure (singleRunBrowserNotCaptured)' , async ( ) => {
316420 mockProcess ( process )
317421
0 commit comments