1
- // https://github.com/nodejs/node/blob/3e57891ee2fde0971e18fc383c25acf8f90def05 /lib/internal/test_runner/test.js
1
+ // https://github.com/nodejs/node/blob/3759935ee29d8042d917d3ceaa768521c14413ff /lib/internal/test_runner/test.js
2
2
3
3
'use strict'
4
4
@@ -43,7 +43,8 @@ const {
43
43
} = require ( '#internal/test_runner/utils' )
44
44
const {
45
45
createDeferredPromise,
46
- kEmptyObject
46
+ kEmptyObject,
47
+ once : runOnce
47
48
} = require ( '#internal/util' )
48
49
const { isPromise } = require ( '#internal/util/types' )
49
50
const {
@@ -486,8 +487,14 @@ class Test extends AsyncResource {
486
487
return
487
488
}
488
489
490
+ const { args, ctx } = this . getRunArgs ( )
491
+ const afterEach = runOnce ( async ( ) => {
492
+ if ( this . parent ?. hooks . afterEach . length > 0 ) {
493
+ await this . parent [ kRunHook ] ( 'afterEach' , { args, ctx } )
494
+ }
495
+ } )
496
+
489
497
try {
490
- const { args, ctx } = this . getRunArgs ( )
491
498
if ( this . parent ?. hooks . beforeEach . length > 0 ) {
492
499
await this . parent [ kRunHook ] ( 'beforeEach' , { args, ctx } )
493
500
}
@@ -522,12 +529,10 @@ class Test extends AsyncResource {
522
529
return
523
530
}
524
531
525
- if ( this . parent ?. hooks . afterEach . length > 0 ) {
526
- await this . parent [ kRunHook ] ( 'afterEach' , { args, ctx } )
527
- }
528
-
532
+ await afterEach ( )
529
533
this . pass ( )
530
534
} catch ( err ) {
535
+ try { await afterEach ( ) } catch { /* test is already failing, let's the error */ }
531
536
if ( isTestFailureError ( err ) ) {
532
537
if ( err . failureType === kTestTimeoutFailure ) {
533
538
this . cancel ( err )
@@ -735,6 +740,12 @@ class Suite extends Test {
735
740
}
736
741
737
742
async run ( ) {
743
+ const hookArgs = this . getRunArgs ( )
744
+ const afterEach = runOnce ( async ( ) => {
745
+ if ( this . parent ?. hooks . afterEach . length > 0 ) {
746
+ await this . parent [ kRunHook ] ( 'afterEach' , hookArgs )
747
+ }
748
+ } )
738
749
try {
739
750
this . parent . activeSubtests ++
740
751
await this . buildSuite
@@ -746,8 +757,6 @@ class Suite extends Test {
746
757
return
747
758
}
748
759
749
- const hookArgs = this . getRunArgs ( )
750
-
751
760
if ( this . parent ?. hooks . beforeEach . length > 0 ) {
752
761
await this . parent [ kRunHook ] ( 'beforeEach' , hookArgs )
753
762
}
@@ -760,13 +769,11 @@ class Suite extends Test {
760
769
761
770
await SafePromiseRace ( [ promise , stopPromise ] )
762
771
await this [ kRunHook ] ( 'after' , hookArgs )
763
-
764
- if ( this . parent ?. hooks . afterEach . length > 0 ) {
765
- await this . parent [ kRunHook ] ( 'afterEach' , hookArgs )
766
- }
772
+ await afterEach ( )
767
773
768
774
this . pass ( )
769
775
} catch ( err ) {
776
+ try { await afterEach ( ) } catch { /* test is already failing, let's the error */ }
770
777
if ( isTestFailureError ( err ) ) {
771
778
this . fail ( err )
772
779
} else {
0 commit comments