Skip to content

Commit f3268d6

Browse files
MoLowtargos
authored andcommitted
test_runner: fix global after hook
PR-URL: #48231Fixes: #48230 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 5ddca72 commit f3268d6

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

‎lib/internal/test_runner/harness.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ function setup(root){
140140
constrejectionHandler=
141141
createProcessEventHandler('unhandledRejection',root);
142142
constcoverage=configureCoverage(root,globalOptions);
143-
constexitHandler=()=>{
144-
root.postRun(newERR_TEST_FAILURE(
143+
constexitHandler=async()=>{
144+
awaitroot.run(newERR_TEST_FAILURE(
145145
'Promise resolution is still pending but the event loop has already resolved',
146146
kCancelledByParent));
147147

@@ -150,8 +150,8 @@ function setup(root){
150150
process.removeListener('uncaughtException',exceptionHandler);
151151
};
152152

153-
constterminationHandler=()=>{
154-
exitHandler();
153+
constterminationHandler=async()=>{
154+
awaitexitHandler();
155155
process.exit();
156156
};
157157

‎lib/internal/test_runner/test.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ class Test extends AsyncResource{
427427
validateOneOf(name,'hook name',kHookNames);
428428
// eslint-disable-next-line no-use-before-define
429429
consthook=newTestHook(fn,options);
430-
if(name==='before'){
430+
if(name==='before'||name==='after'){
431431
hook.run=runOnce(hook.run);
432432
}
433433
ArrayPrototypePush(this.hooks[name],hook);
@@ -514,7 +514,7 @@ class Test extends AsyncResource{
514514
}
515515
}
516516

517-
asyncrun(){
517+
asyncrun(pendingSubtestsError){
518518
if(this.parent!==null){
519519
this.parent.activeSubtests++;
520520
}
@@ -526,11 +526,11 @@ class Test extends AsyncResource{
526526
}
527527

528528
const{ args, ctx }=this.getRunArgs();
529-
constafter=runOnce(async()=>{
529+
constafter=async()=>{
530530
if(this.hooks.after.length>0){
531531
awaitthis.runHook('after',{ args, ctx });
532532
}
533-
});
533+
};
534534
constafterEach=runOnce(async()=>{
535535
if(this.parent?.hooks.afterEach.length>0){
536536
awaitthis.parent.runHook('afterEach',{ args, ctx });
@@ -579,8 +579,8 @@ class Test extends AsyncResource{
579579
awaitafter();
580580
this.pass();
581581
}catch(err){
582-
try{awaitafter();}catch{/* Ignore error. */}
583582
try{awaitafterEach();}catch{/* test is already failing, let's ignore the error */}
583+
try{awaitafter();}catch{/* Ignore error. */}
584584
if(isTestFailureError(err)){
585585
if(err.failureType===kTestTimeoutFailure){
586586
this.#cancel(err);
@@ -594,7 +594,7 @@ class Test extends AsyncResource{
594594

595595
// Clean up the test. Then, try to report the results and execute any
596596
// tests that were pending due to available concurrency.
597-
this.postRun();
597+
this.postRun(pendingSubtestsError);
598598
}
599599

600600
postRun(pendingSubtestsError){

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const assert = require('assert');
55
const{ test, describe, it, before, after, beforeEach, afterEach }=require('node:test');
66

77
before((t)=>t.diagnostic('before 1 called'));
8+
after((t)=>t.diagnostic('after 1 called'));
89

910
describe('describe hooks',()=>{
1011
consttestArr=[];
@@ -107,17 +108,20 @@ test('test hooks', async (t) =>{
107108
awaitt.test('nested 2',()=>testArr.push('nested 2'));
108109
});
109110

110-
assert.deepStrictEqual(testArr,[
111-
'before test hooks',
112-
'beforeEach 1','1','afterEach 1',
113-
'beforeEach 2','2','afterEach 2',
114-
'beforeEach nested',
115-
'nested before nested',
116-
'beforeEach nested 1','nested beforeEach nested 1','nested1','afterEach nested 1','nested afterEach nested 1',
117-
'beforeEach nested 2','nested beforeEach nested 2','nested 2','afterEach nested 2','nested afterEach nested 2',
118-
'afterEach nested',
119-
'nested after nested',
120-
]);
111+
t.after(common.mustCall(()=>{
112+
assert.deepStrictEqual(testArr,[
113+
'before test hooks',
114+
'beforeEach 1','1','afterEach 1',
115+
'beforeEach 2','2','afterEach 2',
116+
'beforeEach nested',
117+
'nested before nested',
118+
'beforeEach nested 1','nested beforeEach nested 1','nested1','afterEach nested 1','nested afterEach nested 1',
119+
'beforeEach nested 2','nested beforeEach nested 2','nested 2','afterEach nested 2','nested afterEach nested 2',
120+
'afterEach nested',
121+
'nested after nested',
122+
'after test hooks',
123+
]);
124+
}));
121125
});
122126

123127
test('t.before throws',async(t)=>{
@@ -164,3 +168,4 @@ test('t.after() is called if test body throws', (t) =>{
164168
});
165169

166170
before((t)=>t.diagnostic('before 2 called'));
171+
after((t)=>t.diagnostic('after 2 called'));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ not ok 3 - after throws
9797
*
9898
*
9999
*
100+
*
100101
...
101102
# Subtest: beforeEach throws
102103
# Subtest: 1
@@ -544,6 +545,8 @@ not ok 14 - t.after() is called if test body throws
544545
1..14
545546
# before 1 called
546547
# before 2 called
548+
# after 1 called
549+
# after 2 called
547550
# tests 38
548551
# suites 8
549552
# pass 14

0 commit comments

Comments
(0)