From 062059d625fe874ac494dc8a4d23c3c92416cf08 Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 26 Jan 2026 13:06:41 +0300 Subject: [PATCH 1/2] test_runner: run afterEach on runtime skip Signed-off-by: Igor --- lib/internal/test_runner/test.js | 4 ++- .../test-runner-aftereach-runtime-skip.js | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-runner-aftereach-runtime-skip.js diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index e426438faba75f..ef9967b99f53fe 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -1062,13 +1062,15 @@ class Test extends AsyncResource { ctx.plan(this.expectedAssertions); } + const wasSkippedBeforeRun = this.skipped; + const after = async () => { if (this.hooks.after.length > 0) { await this.runHook('after', hookArgs); } }; const afterEach = runOnce(async () => { - if (this.parent?.hooks.afterEach.length > 0 && !this.skipped) { + if (this.parent?.hooks.afterEach.length > 0 && !wasSkippedBeforeRun) { await this.parent.runHook('afterEach', hookArgs); } }, kRunOnceOptions); diff --git a/test/parallel/test-runner-aftereach-runtime-skip.js b/test/parallel/test-runner-aftereach-runtime-skip.js new file mode 100644 index 00000000000000..5988d40400c496 --- /dev/null +++ b/test/parallel/test-runner-aftereach-runtime-skip.js @@ -0,0 +1,32 @@ +'use strict'; + +const assert = require('node:assert'); +const common = require('../common'); +const { beforeEach, afterEach, test } = require('node:test'); + +let afterEachRuntimeSkip = 0; +let afterEachTotal = 0; + +beforeEach(common.mustCall(() => {}, 2)); + +afterEach(common.mustCall((t) => { + afterEachTotal++; + if (t.name === 'runtime skip') { + afterEachRuntimeSkip++; + } +}, 2)); + +test('normal test', (t) => { + t.assert.ok(true); +}); + +test('runtime skip', (t) => { + t.skip('skip after setup'); +}); + +test('static skip', { skip: true }, common.mustNotCall()); + +process.on('exit', () => { + assert.strictEqual(afterEachRuntimeSkip, 1); + assert.strictEqual(afterEachTotal, 2); +}); From 9979c2f73f4772cbb42b1262d970d696ee9a4154 Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 26 Jan 2026 17:46:47 +0300 Subject: [PATCH 2/2] test: track beforeEach calls in runtime-skip test Increment a beforeEach counter and assert it runs twice when a test is skipped at runtime. Signed-off-by: Igor --- test/parallel/test-runner-aftereach-runtime-skip.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-runner-aftereach-runtime-skip.js b/test/parallel/test-runner-aftereach-runtime-skip.js index 5988d40400c496..7a6ba2ea9cbddf 100644 --- a/test/parallel/test-runner-aftereach-runtime-skip.js +++ b/test/parallel/test-runner-aftereach-runtime-skip.js @@ -1,13 +1,16 @@ 'use strict'; -const assert = require('node:assert'); const common = require('../common'); +const assert = require('node:assert'); const { beforeEach, afterEach, test } = require('node:test'); +let beforeEachTotal = 0; let afterEachRuntimeSkip = 0; let afterEachTotal = 0; -beforeEach(common.mustCall(() => {}, 2)); +beforeEach(common.mustCall(() => { + beforeEachTotal++; +}, 2)); afterEach(common.mustCall((t) => { afterEachTotal++; @@ -27,6 +30,7 @@ test('runtime skip', (t) => { test('static skip', { skip: true }, common.mustNotCall()); process.on('exit', () => { + assert.strictEqual(beforeEachTotal, 2); assert.strictEqual(afterEachRuntimeSkip, 1); assert.strictEqual(afterEachTotal, 2); });