@@ -31,6 +31,7 @@ class Runner extends Emittery {
3131 this . boundCompareTestSnapshot = this . compareTestSnapshot . bind ( this ) ;
3232 this . interrupted = false ;
3333 this . snapshots = null ;
34+ this . nextTaskIndex = 0 ;
3435 this . tasks = {
3536 after : [ ] ,
3637 afterAlways : [ ] ,
@@ -76,6 +77,8 @@ class Runner extends Emittery {
7677 } ) ;
7778 }
7879
80+ metadata . taskIndex = this . nextTaskIndex ++ ;
81+
7982 const { args, buildTitle, implementations, rawTitle} = parseTestArgs ( testArgs ) ;
8083
8184 if ( this . checkSelectedByLineNumbers ) {
@@ -285,7 +288,7 @@ class Runner extends Emittery {
285288 return result ;
286289 }
287290
288- async runHooks ( tasks , contextRef , titleSuffix , testPassed ) {
291+ async runHooks ( tasks , contextRef , { titleSuffix, testPassed, associatedTaskIndex } = { } ) {
289292 const hooks = tasks . map ( task => new Runnable ( {
290293 contextRef,
291294 experiments : this . experiments ,
@@ -295,7 +298,7 @@ class Runner extends Emittery {
295298 t => task . implementation . apply ( null , [ t ] . concat ( task . args ) ) ,
296299 compareTestSnapshot : this . boundCompareTestSnapshot ,
297300 updateSnapshots : this . updateSnapshots ,
298- metadata : task . metadata ,
301+ metadata : { ... task . metadata , associatedTaskIndex } ,
299302 powerAssert : this . powerAssert ,
300303 title : `${ task . title } ${ titleSuffix || '' } ` ,
301304 isHook : true ,
@@ -326,7 +329,14 @@ class Runner extends Emittery {
326329
327330 async runTest ( task , contextRef ) {
328331 const hookSuffix = ` for ${ task . title } ` ;
329- let hooksOk = await this . runHooks ( this . tasks . beforeEach , contextRef , hookSuffix ) ;
332+ let hooksOk = await this . runHooks (
333+ this . tasks . beforeEach ,
334+ contextRef ,
335+ {
336+ titleSuffix : hookSuffix ,
337+ associatedTaskIndex : task . metadata . taskIndex
338+ }
339+ ) ;
330340
331341 let testOk = false ;
332342 if ( hooksOk ) {
@@ -358,7 +368,14 @@ class Runner extends Emittery {
358368 logs : result . logs
359369 } ) ;
360370
361- hooksOk = await this . runHooks ( this . tasks . afterEach , contextRef , hookSuffix , testOk ) ;
371+ hooksOk = await this . runHooks (
372+ this . tasks . afterEach ,
373+ contextRef ,
374+ {
375+ titleSuffix : hookSuffix ,
376+ testPassed : testOk ,
377+ associatedTaskIndex : task . metadata . taskIndex
378+ } ) ;
362379 } else {
363380 this . emit ( 'stateChange' , {
364381 type : 'test-failed' ,
@@ -372,7 +389,14 @@ class Runner extends Emittery {
372389 }
373390 }
374391
375- const alwaysOk = await this . runHooks ( this . tasks . afterEachAlways , contextRef , hookSuffix , testOk ) ;
392+ const alwaysOk = await this . runHooks (
393+ this . tasks . afterEachAlways ,
394+ contextRef ,
395+ {
396+ titleSuffix : hookSuffix ,
397+ testPassed : testOk ,
398+ associatedTaskIndex : task . metadata . taskIndex
399+ } ) ;
376400 return alwaysOk && hooksOk && testOk ;
377401 }
378402
0 commit comments