Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-runner-aftereach-runtime-skip.js
Original file line number Diff line number Diff line change
@@ -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);
});
Comment on lines +32 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the beforeEachTotal or afterEachTotal variables. The common.mustCall()s will enforce those for you.

Loading