@@ -76,7 +76,7 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
7676const kUnwrapErrors = new SafeSet ( )
7777 . add ( kTestCodeFailure ) . add ( kHookFailure )
7878 . add ( 'uncaughtException' ) . add ( 'unhandledRejection' ) ;
79- const { testNamePatterns, testOnlyFlag } = parseCommandLine ( ) ;
79+ const { forceExit , testNamePatterns, testOnlyFlag } = parseCommandLine ( ) ;
8080let kResistStopPropagation ;
8181
8282function stopTest ( timeout , signal ) {
@@ -722,6 +722,16 @@ class Test extends AsyncResource {
722722 // This helps catch any asynchronous activity that occurs after the tests
723723 // have finished executing.
724724 this . postRun ( ) ;
725+ } else if ( forceExit ) {
726+ // This is the root test, and all known tests and hooks have finished
727+ // executing. If the user wants to force exit the process regardless of
728+ // any remaining ref'ed handles, then do that now. It is theoretically
729+ // possible that a ref'ed handle could asynchronously create more tests,
730+ // but the user opted into this behavior.
731+ this . reporter . once ( 'close' , ( ) => {
732+ process . exit ( ) ;
733+ } ) ;
734+ this . harness . teardown ( ) ;
725735 }
726736 }
727737
@@ -772,12 +782,11 @@ class Test extends AsyncResource {
772782 if ( this . parent === this . root &&
773783 this . root . activeSubtests === 0 &&
774784 this . root . pendingSubtests . length === 0 &&
775- this . root . readySubtests . size === 0 &&
776- this . root . hooks . after . length > 0 ) {
777- // This is done so that any global after() hooks are run. At this point
778- // all of the tests have finished running. However, there might be
779- // ref'ed handles keeping the event loop alive. This gives the global
780- // after() hook a chance to clean them up.
785+ this . root . readySubtests . size === 0 ) {
786+ // At this point all of the tests have finished running. However, there
787+ // might be ref'ed handles keeping the event loop alive. This gives the
788+ // global after() hook a chance to clean them up. The user may also
789+ // want to force the test runner to exit despite ref'ed handles.
781790 this . root . run ( ) ;
782791 }
783792 } else if ( ! this . reported ) {
0 commit comments