@@ -408,14 +408,6 @@ class Test extends AsyncResource {
408
408
this . childNumber = 0 ;
409
409
this . timeout = kDefaultTimeout ;
410
410
this . entryFile = entryFile ;
411
- this . hooks = {
412
- __proto__ : null ,
413
- before : [ ] ,
414
- after : [ ] ,
415
- beforeEach : [ ] ,
416
- afterEach : [ ] ,
417
- ownAfterEachCount : 0 ,
418
- } ;
419
411
} else {
420
412
const nesting = parent . parent === null ? parent . nesting :
421
413
parent . nesting + 1 ;
@@ -431,14 +423,6 @@ class Test extends AsyncResource {
431
423
this . childNumber = parent . subtests . length + 1 ;
432
424
this . timeout = parent . timeout ;
433
425
this . entryFile = parent . entryFile ;
434
- this . hooks = {
435
- __proto__ : null ,
436
- before : [ ] ,
437
- after : [ ] ,
438
- beforeEach : ArrayPrototypeSlice ( parent . hooks . beforeEach ) ,
439
- afterEach : ArrayPrototypeSlice ( parent . hooks . afterEach ) ,
440
- ownAfterEachCount : 0 ,
441
- } ;
442
426
443
427
if ( this . willBeFiltered ( ) ) {
444
428
this . filtered = true ;
@@ -514,6 +498,14 @@ class Test extends AsyncResource {
514
498
this . subtests = [ ] ;
515
499
this . waitingOn = 0 ;
516
500
this . finished = false ;
501
+ this . hooks = {
502
+ __proto__ : null ,
503
+ before : [ ] ,
504
+ after : [ ] ,
505
+ beforeEach : [ ] ,
506
+ afterEach : [ ] ,
507
+ ownAfterEachCount : 0 ,
508
+ } ;
517
509
518
510
if ( ! this . config . only && ( only || this . parent ?. runOnlySubtests ) ) {
519
511
const warning =
@@ -691,6 +683,21 @@ class Test extends AsyncResource {
691
683
this . abortController . abort ( ) ;
692
684
}
693
685
686
+ computeInheritedHooks ( ) {
687
+ if ( this . parent . hooks . beforeEach . length > 0 ) {
688
+ ArrayPrototypeUnshift (
689
+ this . hooks . beforeEach ,
690
+ ...ArrayPrototypeSlice ( this . parent . hooks . beforeEach ) ,
691
+ ) ;
692
+ }
693
+
694
+ if ( this . parent . hooks . afterEach . length > 0 ) {
695
+ ArrayPrototypePushApply (
696
+ this . hooks . afterEach , ArrayPrototypeSlice ( this . parent . hooks . afterEach ) ,
697
+ ) ;
698
+ }
699
+ }
700
+
694
701
createHook ( name , fn , options ) {
695
702
validateOneOf ( name , 'hook name' , kHookNames ) ;
696
703
// eslint-disable-next-line no-use-before-define
@@ -715,7 +722,6 @@ class Test extends AsyncResource {
715
722
} else {
716
723
ArrayPrototypePush ( this . hooks [ name ] , hook ) ;
717
724
}
718
- return hook ;
719
725
}
720
726
721
727
fail ( err ) {
@@ -817,6 +823,7 @@ class Test extends AsyncResource {
817
823
async run ( ) {
818
824
if ( this . parent !== null ) {
819
825
this . parent . activeSubtests ++ ;
826
+ this . computeInheritedHooks ( ) ;
820
827
}
821
828
this . startTime ??= hrtime ( ) ;
822
829
@@ -1211,6 +1218,7 @@ class Suite extends Test {
1211
1218
}
1212
1219
1213
1220
async run ( ) {
1221
+ this . computeInheritedHooks ( ) ;
1214
1222
const hookArgs = this . getRunArgs ( ) ;
1215
1223
1216
1224
let stopPromise ;
0 commit comments