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..7a6ba2ea9cbddf --- /dev/null +++ b/test/parallel/test-runner-aftereach-runtime-skip.js @@ -0,0 +1,36 @@ +'use strict'; + +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(() => { + beforeEachTotal++; +}, 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(beforeEachTotal, 2); + assert.strictEqual(afterEachRuntimeSkip, 1); + assert.strictEqual(afterEachTotal, 2); +});