From d36786bdb8471d095b45d2fb4afcb47909d512f3 Mon Sep 17 00:00:00 2001 From: marco-ippolito Date: Fri, 5 Jan 2024 14:56:43 +0100 Subject: [PATCH] test_runner: do not invoke after hook when test is empty --- lib/internal/test_runner/test.js | 2 +- test/parallel/test-runner-skip-after-hook.js | 10 ++++++++++ 2 files changed, 11 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..b1bcbd923e7144 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -587,7 +587,7 @@ class Test extends AsyncResource { const { args, ctx } = this.getRunArgs(); const after = async () => { - if (this.hooks.after.length > 0) { + if (this.parent?.hooks.after.length > 0) { 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'); + })); +});