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'); + })); +});