@@ -1259,6 +1259,9 @@ class Test extends AsyncResource {
12591259
12601260 let stopPromise ;
12611261
1262+ let publishEnd = ( ) => testChannel . end . publish ( channelContext ) ;
1263+ let publishError = ( err ) => testChannel . error . publish ( { __proto__ : null , ...channelContext , error : err } ) ;
1264+
12621265 try {
12631266 if ( this . parent ?. hooks . before . length > 0 ) {
12641267 // This hook usually runs immediately, we need to wait for it to finish
@@ -1277,9 +1280,11 @@ class Test extends AsyncResource {
12771280 // not the runInAsyncScope call itself, to maintain AsyncLocalStorage bindings.
12781281 let testFn = this . fn ;
12791282 if ( channelContext !== null && testChannel . start . hasSubscribers ) {
1280- testFn = ( ...fnArgs ) => testChannel . start . runStores ( channelContext ,
1281- ( ) => ReflectApply ( this . fn , this , fnArgs ) ,
1282- ) ;
1283+ testFn = ( ...fnArgs ) => testChannel . start . runStores ( channelContext , ( ) => {
1284+ publishEnd = AsyncResource . bind ( publishEnd ) ;
1285+ publishError = AsyncResource . bind ( publishError ) ;
1286+ return ReflectApply ( this . fn , this , fnArgs ) ;
1287+ } ) ;
12831288 }
12841289
12851290 ArrayPrototypeUnshift ( runArgs , testFn , ctx ) ;
@@ -1331,9 +1336,8 @@ class Test extends AsyncResource {
13311336 await afterEach ( ) ;
13321337 await after ( ) ;
13331338 } catch ( err ) {
1334- // Publish diagnostics_channel error event if the channel has subscribers
13351339 if ( channelContext !== null && testChannel . error . hasSubscribers ) {
1336- testChannel . error . publish ( { __proto__ : null , ... channelContext , error : err } ) ;
1340+ publishError ( err ) ;
13371341 }
13381342 if ( isTestFailureError ( err ) ) {
13391343 if ( err . failureType === kTestTimeoutFailure ) {
@@ -1357,7 +1361,7 @@ class Test extends AsyncResource {
13571361
13581362 // Publish diagnostics_channel end event if the channel has subscribers (in both success and error cases)
13591363 if ( channelContext !== null && testChannel . end . hasSubscribers ) {
1360- testChannel . end . publish ( channelContext ) ;
1364+ publishEnd ( ) ;
13611365 }
13621366 }
13631367
@@ -1702,6 +1706,9 @@ class Suite extends Test {
17021706 file : this . entryFile ,
17031707 type : this . reportedType ,
17041708 } ;
1709+ let publishEnd = ( ) => testChannel . end . publish ( channelContext ) ;
1710+ let publishError = ( err ) => testChannel . error . publish ( { __proto__ : null , ...channelContext , error : err } ) ;
1711+
17051712 try {
17061713 const { ctx, args } = this . getRunArgs ( ) ;
17071714
@@ -1713,9 +1720,11 @@ class Suite extends Test {
17131720 let suiteFn = this . fn ;
17141721 if ( testChannel . start . hasSubscribers ) {
17151722 const baseFn = this . fn ;
1716- suiteFn = ( ...fnArgs ) => testChannel . start . runStores ( channelContext ,
1717- ( ) => ReflectApply ( baseFn , this , fnArgs ) ,
1718- ) ;
1723+ suiteFn = ( ...fnArgs ) => testChannel . start . runStores ( channelContext , ( ) => {
1724+ publishEnd = AsyncResource . bind ( publishEnd ) ;
1725+ publishError = AsyncResource . bind ( publishError ) ;
1726+ return ReflectApply ( baseFn , this , fnArgs ) ;
1727+ } ) ;
17191728 }
17201729
17211730 const runArgs = [ suiteFn , ctx ] ;
@@ -1724,12 +1733,12 @@ class Suite extends Test {
17241733 await ReflectApply ( this . runInAsyncScope , this , runArgs ) ;
17251734 } catch ( err ) {
17261735 if ( testChannel . error . hasSubscribers ) {
1727- testChannel . error . publish ( { __proto__ : null , ... channelContext , error : err } ) ;
1736+ publishError ( err ) ;
17281737 }
17291738 this . fail ( new ERR_TEST_FAILURE ( err , kTestCodeFailure ) ) ;
17301739 } finally {
17311740 if ( testChannel . end . hasSubscribers ) {
1732- testChannel . end . publish ( channelContext ) ;
1741+ publishEnd ( ) ;
17331742 }
17341743 }
17351744
0 commit comments