Skip to content

Commit 2ae6d8f

Browse files
test_runner: run afterEach on runtime skip
Signed-off-by: Igor <igorshevelenkov4@gmail.com> PR-URL: #61525 Fixes: #61462 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 38a6da5 commit 2ae6d8f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/internal/test_runner/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,13 +1152,15 @@ class Test extends AsyncResource {
11521152
ctx.plan(this.expectedAssertions);
11531153
}
11541154

1155+
const wasSkippedBeforeRun = this.skipped;
1156+
11551157
const after = async () => {
11561158
if (this.hooks.after.length > 0) {
11571159
await this.runHook('after', hookArgs);
11581160
}
11591161
};
11601162
const afterEach = runOnce(async () => {
1161-
if (this.parent?.hooks.afterEach.length > 0 && !this.skipped) {
1163+
if (this.parent?.hooks.afterEach.length > 0 && !wasSkippedBeforeRun) {
11621164
await this.parent.runHook('afterEach', hookArgs);
11631165
}
11641166
}, kRunOnceOptions);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
const { beforeEach, afterEach, test } = require('node:test');
6+
7+
let beforeEachTotal = 0;
8+
let afterEachRuntimeSkip = 0;
9+
let afterEachTotal = 0;
10+
11+
beforeEach(common.mustCall(() => {
12+
beforeEachTotal++;
13+
}, 2));
14+
15+
afterEach(common.mustCall((t) => {
16+
afterEachTotal++;
17+
if (t.name === 'runtime skip') {
18+
afterEachRuntimeSkip++;
19+
}
20+
}, 2));
21+
22+
test('normal test', (t) => {
23+
t.assert.ok(true);
24+
});
25+
26+
test('runtime skip', (t) => {
27+
t.skip('skip after setup');
28+
});
29+
30+
test('static skip', { skip: true }, common.mustNotCall());
31+
32+
process.on('exit', () => {
33+
assert.strictEqual(beforeEachTotal, 2);
34+
assert.strictEqual(afterEachRuntimeSkip, 1);
35+
assert.strictEqual(afterEachTotal, 2);
36+
});

0 commit comments

Comments
 (0)