Skip to content

Commit 7fad771

Browse files
pmarchinitargos
authored andcommitted
test_runner: fix erroneous diagnostic warning when only: false
Co-author: rstagi <r.stagi96@gmail.com> PR-URL: #54116 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent a394219 commit 7fad771

File tree

6 files changed

+227
-7
lines changed

6 files changed

+227
-7
lines changed

lib/internal/test_runner/test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ class Test extends AsyncResource {
409409

410410
this.concurrency = parent.concurrency;
411411
this.nesting = nesting;
412-
this.only = only ?? !parent.runOnlySubtests;
412+
this.only = only ?? (parent.only && !parent.runOnlySubtests);
413413
this.reporter = parent.reporter;
414-
this.runOnlySubtests = !this.only;
414+
this.runOnlySubtests = false;
415415
this.childNumber = parent.subtests.length + 1;
416416
this.timeout = parent.timeout;
417417
this.entryFile = parent.entryFile;
@@ -501,7 +501,7 @@ class Test extends AsyncResource {
501501
this.waitingOn = 0;
502502
this.finished = false;
503503

504-
if (!testOnlyFlag && (only || this.runOnlySubtests)) {
504+
if (!testOnlyFlag && (only || this.parent?.runOnlySubtests)) {
505505
const warning =
506506
"'only' and 'runOnly' require the --test-only command-line option.";
507507
this.diagnostic(warning);

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

+21
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,24 @@ 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('nested tests with run only',{only: true}, common.mustCall(async (t) => {
124+
// Within this test, all subtests are run by default.
125+
await t.test('running subtest - 1');
126+
127+
// The test context can be updated to run subtests with the 'only' option.
128+
await t.runOnly(true);
129+
await t.test('this subtest is now skipped - 2', common.mustNotCall());
130+
await t.test('this subtest is run - 3', { only: true }, common.mustCall(async (t) => {
131+
await t.test('this subtest is run - 4', common.mustCall());
132+
await t.test('this subtest is run - 5', { only: true }, common.mustCall());
133+
}));
134+
135+
// Switch the context back to execute all tests.
136+
await t.runOnly(false);
137+
await t.test('this subtest is now run - 6');
138+
139+
// Explicitly do not run these tests.
140+
await t.test('skipped subtest - 7', { only: false }, common.mustNotCall());
141+
await t.test('skipped subtest - 8', { skip: true }, common.mustNotCall());
142+
}))

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

+41-4
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,49 @@ ok 8 - describe only = true, with nested subtests
155155
duration_ms: *
156156
type: 'suite'
157157
...
158-
1..8
159-
# tests 21
158+
# Subtest: nested tests with run only
159+
# Subtest: running subtest - 1
160+
ok 1 - running subtest - 1
161+
---
162+
duration_ms: *
163+
...
164+
# Subtest: this subtest is run - 3
165+
# Subtest: this subtest is run - 4
166+
ok 1 - this subtest is run - 4
167+
---
168+
duration_ms: *
169+
...
170+
# Subtest: this subtest is run - 5
171+
ok 2 - this subtest is run - 5
172+
---
173+
duration_ms: *
174+
...
175+
1..2
176+
ok 2 - this subtest is run - 3
177+
---
178+
duration_ms: *
179+
...
180+
# Subtest: this subtest is now run - 6
181+
ok 3 - this subtest is now run - 6
182+
---
183+
duration_ms: *
184+
...
185+
# Subtest: skipped subtest - 8
186+
ok 4 - skipped subtest - 8 # SKIP
187+
---
188+
duration_ms: *
189+
...
190+
1..4
191+
ok 9 - nested tests with run only
192+
---
193+
duration_ms: *
194+
...
195+
1..9
196+
# tests 28
160197
# suites 7
161-
# pass 18
198+
# pass 24
162199
# fail 0
163200
# cancelled 0
164-
# skipped 3
201+
# skipped 4
165202
# todo 0
166203
# duration_ms *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const { test, describe, it } = require('node:test');
3+
4+
describe('should NOT print --test-only diagnostic warning - describe-only-false', {only: false}, () => {
5+
it('only false in describe');
6+
});
7+
8+
describe('should NOT print --test-only diagnostic warning - it-only-false', () => {
9+
it('only false in the subtest', {only: false});
10+
});
11+
12+
describe('should NOT print --test-only diagnostic warning - no-only', () => {
13+
it('no only');
14+
});
15+
16+
test('should NOT print --test-only diagnostic warning - test-only-false', {only: false}, async (t) => {
17+
await t.test('only false in parent test');
18+
});
19+
20+
test('should NOT print --test-only diagnostic warning - t.test-only-false', async (t) => {
21+
await t.test('only false in subtest', {only: false});
22+
});
23+
24+
test('should NOT print --test-only diagnostic warning - no-only', async (t) => {
25+
await t.test('no only');
26+
});
27+
28+
test('should print --test-only diagnostic warning - test-only-true', {only: true}, async (t) => {
29+
await t.test('only true in parent test');
30+
})
31+
32+
test('should print --test-only diagnostic warning - t.test-only-true', async (t) => {
33+
await t.test('only true in subtest', {only: true});
34+
});
35+
36+
test('should print --test-only diagnostic warning - 2 levels of only', async (t) => {
37+
await t.test('only true in parent test', {only: false}, async (t) => {
38+
await t.test('only true in subtest', {only: true});
39+
});
40+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
TAP version 13
2+
# Subtest: should NOT print --test-only diagnostic warning - describe-only-false
3+
# Subtest: only false in describe
4+
ok 1 - only false in describe
5+
---
6+
duration_ms: *
7+
...
8+
1..1
9+
ok 1 - should NOT print --test-only diagnostic warning - describe-only-false
10+
---
11+
duration_ms: *
12+
type: 'suite'
13+
...
14+
# Subtest: should NOT print --test-only diagnostic warning - it-only-false
15+
# Subtest: only false in the subtest
16+
ok 1 - only false in the subtest
17+
---
18+
duration_ms: *
19+
...
20+
1..1
21+
ok 2 - should NOT print --test-only diagnostic warning - it-only-false
22+
---
23+
duration_ms: *
24+
type: 'suite'
25+
...
26+
# Subtest: should NOT print --test-only diagnostic warning - no-only
27+
# Subtest: no only
28+
ok 1 - no only
29+
---
30+
duration_ms: *
31+
...
32+
1..1
33+
ok 3 - should NOT print --test-only diagnostic warning - no-only
34+
---
35+
duration_ms: *
36+
type: 'suite'
37+
...
38+
# Subtest: should NOT print --test-only diagnostic warning - test-only-false
39+
# Subtest: only false in parent test
40+
ok 1 - only false in parent test
41+
---
42+
duration_ms: *
43+
...
44+
1..1
45+
ok 4 - should NOT print --test-only diagnostic warning - test-only-false
46+
---
47+
duration_ms: *
48+
...
49+
# Subtest: should NOT print --test-only diagnostic warning - t.test-only-false
50+
# Subtest: only false in subtest
51+
ok 1 - only false in subtest
52+
---
53+
duration_ms: *
54+
...
55+
1..1
56+
ok 5 - should NOT print --test-only diagnostic warning - t.test-only-false
57+
---
58+
duration_ms: *
59+
...
60+
# Subtest: should NOT print --test-only diagnostic warning - no-only
61+
# Subtest: no only
62+
ok 1 - no only
63+
---
64+
duration_ms: *
65+
...
66+
1..1
67+
ok 6 - should NOT print --test-only diagnostic warning - no-only
68+
---
69+
duration_ms: *
70+
...
71+
# Subtest: should print --test-only diagnostic warning - test-only-true
72+
# Subtest: only true in parent test
73+
ok 1 - only true in parent test
74+
---
75+
duration_ms: *
76+
...
77+
1..1
78+
ok 7 - should print --test-only diagnostic warning - test-only-true
79+
---
80+
duration_ms: *
81+
...
82+
# 'only' and 'runOnly' require the --test-only command-line option.
83+
# Subtest: should print --test-only diagnostic warning - t.test-only-true
84+
# Subtest: only true in subtest
85+
ok 1 - only true in subtest
86+
---
87+
duration_ms: *
88+
...
89+
# 'only' and 'runOnly' require the --test-only command-line option.
90+
1..1
91+
ok 8 - should print --test-only diagnostic warning - t.test-only-true
92+
---
93+
duration_ms: *
94+
...
95+
# Subtest: should print --test-only diagnostic warning - 2 levels of only
96+
# Subtest: only true in parent test
97+
# Subtest: only true in subtest
98+
ok 1 - only true in subtest
99+
---
100+
duration_ms: *
101+
...
102+
# 'only' and 'runOnly' require the --test-only command-line option.
103+
1..1
104+
ok 1 - only true in parent test
105+
---
106+
duration_ms: *
107+
...
108+
1..1
109+
ok 9 - should print --test-only diagnostic warning - 2 levels of only
110+
---
111+
duration_ms: *
112+
...
113+
1..9
114+
# tests 16
115+
# suites 3
116+
# pass 16
117+
# fail 0
118+
# cancelled 0
119+
# skipped 0
120+
# todo 0
121+
# duration_ms *

test/parallel/test-runner-output.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ const tests = [
147147
},
148148
{ name: 'test-runner/output/test-runner-plan.js' },
149149
process.features.inspector ? { name: 'test-runner/output/coverage_failure.js' } : false,
150+
{ name: 'test-runner/output/test-diagnostic-warning-without-test-only-flag.js' },
150151
]
151152
.filter(Boolean)
152153
.map(({ name, tty, transform }) => ({

0 commit comments

Comments
 (0)