Skip to content

Commit 4737314

Browse files
philnashMoLow
authored andcommitted
test_runner: fix ordering of test hooks
For tests with subtests the before hook was being run after the beforeEach hook, which is the opposite to test suites and expectations. Also, a function was being used to close over the after hooks, but at the point it was being run the after hooks were not yet set up. Fixes#47915 PR-URL: #47931 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 4bece05 commit 4737314

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎lib/internal/test_runner/test.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,12 @@ class Test extends AsyncResource {
537537
});
538538

539539
try{
540-
if(this.parent?.hooks.beforeEach.length>0){
541-
awaitthis.parent.runHook('beforeEach',{ args, ctx });
542-
}
543540
if(this.parent?.hooks.before.length>0){
544541
awaitthis.parent.runHook('before',this.parent.getRunArgs());
545542
}
543+
if(this.parent?.hooks.beforeEach.length>0){
544+
awaitthis.parent.runHook('beforeEach',{ args, ctx });
545+
}
546546
conststopPromise=stopTest(this.timeout,this.signal);
547547
construnArgs=ArrayPrototypeSlice(args);
548548
ArrayPrototypeUnshift(runArgs,this.fn,ctx);
@@ -574,8 +574,8 @@ class Test extends AsyncResource {
574574
return;
575575
}
576576

577-
awaitafter();
578577
awaitafterEach();
578+
awaitafter();
579579
this.pass();
580580
}catch(err){
581581
try{awaitafter();}catch{/* Ignore error. */}

‎test/fixtures/test-runner/output/hooks.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,24 @@ test('test hooks', async (t) => {
9999
awaitt.test('2',()=>testArr.push('2'));
100100

101101
awaitt.test('nested',async(t)=>{
102+
t.before((t)=>testArr.push('nested before '+t.name));
103+
t.after((t)=>testArr.push('nested after '+t.name));
102104
t.beforeEach((t)=>testArr.push('nested beforeEach '+t.name));
103105
t.afterEach((t)=>testArr.push('nested afterEach '+t.name));
104106
awaitt.test('nested 1',()=>testArr.push('nested1'));
105107
awaitt.test('nested 2',()=>testArr.push('nested 2'));
106108
});
107109

108110
assert.deepStrictEqual(testArr,[
109-
'beforeEach 1','before test hooks','1','afterEach 1',
111+
'before test hooks',
112+
'beforeEach 1','1','afterEach 1',
110113
'beforeEach 2','2','afterEach 2',
111114
'beforeEach nested',
115+
'nested before nested',
112116
'beforeEach nested 1','nested beforeEach nested 1','nested1','afterEach nested 1','nested afterEach nested 1',
113117
'beforeEach nested 2','nested beforeEach nested 2','nested 2','afterEach nested 2','nested afterEach nested 2',
114118
'afterEach nested',
119+
'nested after nested',
115120
]);
116121
});
117122

0 commit comments

Comments
 (0)