Skip to content

Commit 8834f4b

Browse files
committed
test_runner: fix #50665 .skip, .todo and .only missing in subtests
1 parent 83e6350 commit 8834f4b

File tree

6 files changed

+100
-21
lines changed

6 files changed

+100
-21
lines changed

lib/internal/test_runner/test.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeShift,
77
ArrayPrototypeSlice,
88
ArrayPrototypeSome,
9+
ArrayPrototypeForEach,
910
ArrayPrototypeUnshift,
1011
FunctionPrototype,
1112
MathMax,
@@ -122,6 +123,30 @@ class TestContext {
122123

123124
constructor(test) {
124125
this.#test = test;
126+
127+
this.test = (name, options, fn) => {
128+
const overrides = {
129+
__proto__: null,
130+
loc: getCallerLocation(),
131+
};
132+
// eslint-disable-next-line no-use-before-define
133+
const subtest = this.#test.createSubtest(Test, name, options, fn, overrides);
134+
return subtest.start();
135+
};
136+
137+
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
138+
this.test[keyword] = (name, options, fn) => {
139+
const overrides = {
140+
__proto__: null,
141+
[keyword]: true,
142+
loc: getCallerLocation(),
143+
};
144+
145+
// eslint-disable-next-line no-use-before-define
146+
const subtest = this.#test.createSubtest(Test, name, options, fn, overrides);
147+
return subtest.start();
148+
};
149+
});
125150
}
126151

127152
get signal() {
@@ -153,20 +178,6 @@ class TestContext {
153178
this.#test.todo(message);
154179
}
155180

156-
test(name, options, fn) {
157-
const overrides = {
158-
__proto__: null,
159-
loc: getCallerLocation(),
160-
};
161-
162-
const subtest = this.#test.createSubtest(
163-
// eslint-disable-next-line no-use-before-define
164-
Test, name, options, fn, overrides,
165-
);
166-
167-
return subtest.start();
168-
}
169-
170181
before(fn, options) {
171182
this.#test.createHook('before', fn, options);
172183
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ test('only = true, with subtests', { only: true }, async (t) => {
4747
await t.test('skipped subtest 4', { skip: true });
4848
});
4949

50+
test('only = true, with subtests and test.only', { only: true }, async (t) => {
51+
await t.test('skipped subtest 1');
52+
await t.test.skip('skipped subtest 2');
53+
await t.test.only('running subtest 3');
54+
});
55+
5056
describe.only('describe only = true, with subtests', () => {
5157
it.only('`it` subtest 1 should run', () => {});
5258

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ ok 11 - only = true, with subtests
116116
---
117117
duration_ms: *
118118
...
119+
# Subtest: only = true, with subtests and test.only
120+
# Subtest: skipped subtest 1
121+
ok 1 - skipped subtest 1
122+
---
123+
duration_ms: *
124+
...
125+
# Subtest: skipped subtest 2
126+
ok 2 - skipped subtest 2 # SKIP
127+
---
128+
duration_ms: *
129+
...
130+
# Subtest: running subtest 3
131+
ok 3 - running subtest 3
132+
---
133+
duration_ms: *
134+
...
135+
1..3
136+
ok 12 - only = true, with subtests and test.only
137+
---
138+
duration_ms: *
139+
...
119140
# Subtest: describe only = true, with subtests
120141
# Subtest: `it` subtest 1 should run
121142
ok 1 - `it` subtest 1 should run
@@ -128,7 +149,7 @@ ok 11 - only = true, with subtests
128149
duration_ms: *
129150
...
130151
1..2
131-
ok 12 - describe only = true, with subtests
152+
ok 13 - describe only = true, with subtests
132153
---
133154
duration_ms: *
134155
type: 'suite'
@@ -195,7 +216,7 @@ ok 12 - describe only = true, with subtests
195216
duration_ms: *
196217
...
197218
1..12
198-
ok 13 - describe only = true, with a mixture of subtests
219+
ok 14 - describe only = true, with a mixture of subtests
199220
---
200221
duration_ms: *
201222
type: 'suite'
@@ -217,17 +238,17 @@ ok 13 - describe only = true, with a mixture of subtests
217238
duration_ms: *
218239
...
219240
1..3
220-
ok 14 - describe only = true, with subtests
241+
ok 15 - describe only = true, with subtests
221242
---
222243
duration_ms: *
223244
type: 'suite'
224245
...
225-
1..14
226-
# tests 40
246+
1..15
247+
# tests 44
227248
# suites 3
228-
# pass 15
249+
# pass 18
229250
# fail 0
230251
# cancelled 0
231-
# skipped 25
252+
# skipped 26
232253
# todo 0
233254
# duration_ms *
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
@@ -101,6 +101,7 @@ const tests = [
101101
{ name: 'test-runner/output/no_refs.js' },
102102
{ name: 'test-runner/output/no_tests.js' },
103103
{ name: 'test-runner/output/only_tests.js' },
104+
{ name: 'test-runner/output/sub_tests.js' },
104105
{ name: 'test-runner/output/dot_reporter.js' },
105106
{ name: 'test-runner/output/junit_reporter.js', transform: junitTransform },
106107
{ name: 'test-runner/output/spec_reporter_successful.js', transform: specTransform },

0 commit comments

Comments
 (0)