Skip to content

Commit 77128c7

Browse files
committed
test_runner: fix #50665 .skip, .todo and .only missing in subtests
1 parent a0cb507 commit 77128c7

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

lib/internal/test_runner/test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,44 @@ class TestContext {
215215

216216
constructor(test) {
217217
this.#test = test;
218+
219+
this.test = (name, options, fn) => {
220+
const overrides = {
221+
__proto__: null,
222+
loc: getCallerLocation(),
223+
};
224+
225+
const { plan } = this.#test;
226+
if (plan !== null) {
227+
plan.actual++;
228+
}
229+
230+
const subtest = this.#test.createSubtest(
231+
// eslint-disable-next-line no-use-before-define
232+
Test, name, options, fn, overrides,
233+
);
234+
235+
return subtest.start();
236+
};
237+
238+
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
239+
this.test[keyword] = (name, options, fn) => {
240+
const overrides = {
241+
__proto__: null,
242+
[keyword]: true,
243+
loc: getCallerLocation(),
244+
};
245+
246+
const { plan } = this.#test;
247+
if (plan !== null) {
248+
plan.actual++;
249+
}
250+
251+
// eslint-disable-next-line no-use-before-define
252+
const subtest = this.#test.createSubtest(Test, name, options, fn, overrides);
253+
return subtest.start();
254+
};
255+
});
218256
}
219257

220258
get signal() {

test/fixtures/test-runner/output/only_tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,9 @@ describe('describe only = false, with nested only subtests', { only: false }, co
119119
test.only('nested test should run', common.mustNotCall());
120120
}));
121121
}));
122+
123+
test('only = true, with subtests and test.only', { only: true }, async (t) => {
124+
await t.test('skipped subtest 1');
125+
await t.test.skip('skipped subtest 2');
126+
await t.test.only('running subtest 3');
127+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
require('../../../common');
3+
const { test } = require('node:test');
4+
5+
test('parent 1', async (t) => {
6+
await t.test('running subtest 1');
7+
await t.test.skip('skipped subtest 2');
8+
await t.test.todo('mark subtest 3 as todo');
9+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
TAP version 13
2+
# Subtest: parent 1
3+
# Subtest: running subtest 1
4+
ok 1 - running subtest 1
5+
---
6+
duration_ms: *
7+
...
8+
# Subtest: skipped subtest 2
9+
ok 2 - skipped subtest 2 # SKIP
10+
---
11+
duration_ms: *
12+
...
13+
# Subtest: mark subtest 3 as todo
14+
ok 3 - mark subtest 3 as todo # TODO
15+
---
16+
duration_ms: *
17+
...
18+
1..3
19+
ok 1 - parent 1
20+
---
21+
duration_ms: *
22+
...
23+
1..1
24+
# tests 4
25+
# suites 0
26+
# pass 2
27+
# fail 0
28+
# cancelled 0
29+
# skipped 1
30+
# todo 1
31+
# duration_ms *

test/parallel/test-runner-output.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const tests = [
110110
{ name: 'test-runner/output/no_refs.js' },
111111
{ name: 'test-runner/output/no_tests.js' },
112112
{ name: 'test-runner/output/only_tests.js' },
113+
{ name: 'test-runner/output/sub_tests.js' },
113114
{ name: 'test-runner/output/dot_reporter.js', transform: specTransform },
114115
{ name: 'test-runner/output/junit_reporter.js', transform: junitTransform },
115116
{ name: 'test-runner/output/spec_reporter_successful.js', transform: specTransform },

0 commit comments

Comments
 (0)