@@ -526,7 +526,7 @@ class Test extends AsyncResource {
526526 } ;
527527
528528 #cancel( error ) {
529- if ( this . endTime !== null ) {
529+ if ( this . endTime !== null || this . error !== null ) {
530530 return ;
531531 }
532532
@@ -564,17 +564,15 @@ class Test extends AsyncResource {
564564 return ;
565565 }
566566
567- this . endTime = hrtime ( ) ;
568567 this . passed = false ;
569568 this . error = err ;
570569 }
571570
572571 pass ( ) {
573- if ( this . endTime !== null ) {
572+ if ( this . error !== null ) {
574573 return ;
575574 }
576575
577- this . endTime = hrtime ( ) ;
578576 this . passed = true ;
579577 }
580578
@@ -707,15 +705,8 @@ class Test extends AsyncResource {
707705 }
708706
709707 this . pass ( ) ;
710- try {
711- await afterEach ( ) ;
712- await after ( ) ;
713- } catch ( err ) {
714- // If one of the after hooks has thrown unset endTime so that the
715- // catch below can do its cancel/fail logic.
716- this . endTime = null ;
717- throw err ;
718- }
708+ await afterEach ( ) ;
709+ await after ( ) ;
719710 } catch ( err ) {
720711 if ( isTestFailureError ( err ) ) {
721712 if ( err . failureType === kTestTimeoutFailure ) {
@@ -761,13 +752,10 @@ class Test extends AsyncResource {
761752 }
762753
763754 postRun ( pendingSubtestsError ) {
755+ // If the test was cancelled before it started, then the start and end
756+ // times need to be corrected.
764757 this . startTime ??= hrtime ( ) ;
765-
766- // If the test was failed before it even started, then the end time will
767- // be earlier than the start time. Correct that here.
768- if ( this . endTime < this . startTime ) {
769- this . endTime = hrtime ( ) ;
770- }
758+ this . endTime ??= hrtime ( ) ;
771759
772760 // The test has run, so recursively cancel any outstanding subtests and
773761 // mark this test as failed if any subtests failed.
@@ -974,6 +962,7 @@ class TestHook extends Test {
974962 error . failureType = kHookFailure ;
975963 }
976964
965+ this . endTime ??= hrtime ( ) ;
977966 parent . reporter . fail ( 0 , loc , parent . subtests . length + 1 , loc . file , {
978967 __proto__ : null ,
979968 duration_ms : this . duration ( ) ,
0 commit comments