From a53fd95d363f1897549a074a53d29590d58f2dc7 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 8 Jan 2024 11:03:45 +0100 Subject: [PATCH] test_runner: do not invoke after hook when test is empty PR-URL: https://github.com/nodejs/node/pull/51389 Fixes: https://github.com/nodejs/node/issues/51371 Reviewed-By: Luigi Pinca Reviewed-By: Chemi Atlow --- lib/internal/test_runner/test.js | 4 +++- test/parallel/test-runner-skip-after-hook.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-runner-skip-after-hook.js diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 19ae283ed9ad78..e38b1a67c09660 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -587,7 +587,9 @@ class Test extends AsyncResource { const { args, ctx } = this.getRunArgs(); const after = async () => { - if (this.hooks.after.length > 0) { + // If its a root test then check for global after hook else check for parent after hook + const check = this.parent ? this.parent.hooks.after.length > 0 : this.hooks.after.length > 0; + if (check) { await this.runHook('after', { __proto__: null, args, ctx }); } }; diff --git a/test/parallel/test-runner-skip-after-hook.js b/test/parallel/test-runner-skip-after-hook.js new file mode 100644 index 00000000000000..d8175135b02f0d --- /dev/null +++ b/test/parallel/test-runner-skip-after-hook.js @@ -0,0 +1,10 @@ +'use strict'; +// Refs: https://github.com/nodejs/node/issues/51371 +const common = require('../common'); +const { test } = require('node:test'); + +test('test', async (t) => { + t.after(common.mustNotCall(() => { + t.fail('should not run'); + })); +});