Skip to content

Commit a53fd95

Browse files
test_runner: do not invoke after hook when test is empty
PR-URL: #51389 Fixes: #51371 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent a568aaa commit a53fd95

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/internal/test_runner/test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,9 @@ class Test extends AsyncResource {
587587

588588
const { args, ctx } = this.getRunArgs();
589589
const after = async () => {
590-
if (this.hooks.after.length > 0) {
590+
// If its a root test then check for global after hook else check for parent after hook
591+
const check = this.parent ? this.parent.hooks.after.length > 0 : this.hooks.after.length > 0;
592+
if (check) {
591593
await this.runHook('after', { __proto__: null, args, ctx });
592594
}
593595
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
// Refs: https://github.com/nodejs/node/issues/51371
3+
const common = require('../common');
4+
const { test } = require('node:test');
5+
6+
test('test', async (t) => {
7+
t.after(common.mustNotCall(() => {
8+
t.fail('should not run');
9+
}));
10+
});

0 commit comments

Comments
 (0)