@@ -574,7 +574,7 @@ class Test extends AsyncResource {
574
574
}
575
575
}
576
576
577
- async run ( pendingSubtestsError ) {
577
+ async run ( ) {
578
578
if ( this . parent !== null ) {
579
579
this . parent . activeSubtests ++ ;
580
580
}
@@ -662,9 +662,16 @@ class Test extends AsyncResource {
662
662
}
663
663
}
664
664
665
- // Clean up the test. Then, try to report the results and execute any
666
- // tests that were pending due to available concurrency.
667
- this . postRun ( pendingSubtestsError ) ;
665
+ if ( this . parent !== null || typeof this . hookType === 'string' ) {
666
+ // Clean up the test. Then, try to report the results and execute any
667
+ // tests that were pending due to available concurrency.
668
+ //
669
+ // The root test is skipped here because it is a special case. Its
670
+ // postRun() method is called when the process is getting ready to exit.
671
+ // This helps catch any asynchronous activity that occurs after the tests
672
+ // have finished executing.
673
+ this . postRun ( ) ;
674
+ }
668
675
}
669
676
670
677
postRun ( pendingSubtestsError ) {
@@ -706,6 +713,18 @@ class Test extends AsyncResource {
706
713
this . parent . addReadySubtest ( this ) ;
707
714
this . parent . processReadySubtestRange ( false ) ;
708
715
this . parent . processPendingSubtests ( ) ;
716
+
717
+ if ( this . parent === this . root &&
718
+ this . root . activeSubtests === 0 &&
719
+ this . root . pendingSubtests . length === 0 &&
720
+ this . root . readySubtests . size === 0 &&
721
+ this . root . hooks . after . length > 0 ) {
722
+ // This is done so that any global after() hooks are run. At this point
723
+ // all of the tests have finished running. However, there might be
724
+ // ref'ed handles keeping the event loop alive. This gives the global
725
+ // after() hook a chance to clean them up.
726
+ this . root . run ( ) ;
727
+ }
709
728
} else if ( ! this . reported ) {
710
729
const {
711
730
diagnostics,
0 commit comments