Skip to content

Commit dbaef33

Browse files
authored
test_runner: detect only tests when --test is not used
This commit updates the way the test runner processes 'only' tests when node:test files are run without the --test CLI. This is a breaking change. PR-URL: #54881 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 86bdca9 commit dbaef33

11 files changed

+46
-48
lines changed

lib/internal/test_runner/harness.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function createTestTree(rootTestOptions, globalOptions) {
3838
const buildPhaseDeferred = createDeferredPromise();
3939
const isFilteringByName = globalOptions.testNamePatterns ||
4040
globalOptions.testSkipPatterns;
41-
const isFilteringByOnly = globalOptions.only || (globalOptions.isTestRunner &&
42-
globalOptions.isolation === 'none');
41+
const isFilteringByOnly = (globalOptions.isolation === 'process' || process.env.NODE_TEST_CONTEXT) ?
42+
globalOptions.only : true;
4343
const harness = {
4444
__proto__: null,
4545
buildPromise: buildPhaseDeferred.promise,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
..XX...X..XXX.X.....
22
XXX.....X..X...X....
33
.....X...XXX.XX.....
4-
.XXXXXXX...XXXXX
4+
XXXXXXX...XXXXX
55

66
Failed tests:
77

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,11 @@ Error [ERR_TEST_FAILURE]: thrown from callback async throw
309309
</failure>
310310
</testcase>
311311
<testcase name="callback async throw after done" time="*" classname="test"/>
312-
<testsuite name="only is set but not in only mode" time="*" disabled="0" errors="0" tests="4" failures="0" skipped="0" hostname="HOSTNAME">
312+
<testsuite name="only is set on subtests but not in only mode" time="*" disabled="0" errors="0" tests="3" failures="0" skipped="0" hostname="HOSTNAME">
313313
<testcase name="running subtest 1" time="*" classname="test"/>
314-
<testcase name="running subtest 2" time="*" classname="test"/>
315-
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
316314
<testcase name="running subtest 3" time="*" classname="test"/>
317-
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
318315
<testcase name="running subtest 4" time="*" classname="test"/>
319316
</testsuite>
320-
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
321317
<testcase name="custom inspect symbol fail" time="*" classname="test" failure="customized">
322318
<failure type="testCodeFailure" message="customized">
323319
[Error [ERR_TEST_FAILURE]: customized] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: customized }
@@ -521,9 +517,9 @@ Error [ERR_TEST_FAILURE]: test could not be started because its parent finished
521517
<!-- Error: Test "immediate reject - passes but warns" at test/fixtures/test-runner/output/output.js:86:1 generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event. -->
522518
<!-- Error: Test "callback called twice in different ticks" at test/fixtures/test-runner/output/output.js:251:1 generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event. -->
523519
<!-- Error: Test "callback async throw after done" at test/fixtures/test-runner/output/output.js:269:1 generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event. -->
524-
<!-- tests 76 -->
520+
<!-- tests 75 -->
525521
<!-- suites 0 -->
526-
<!-- pass 35 -->
522+
<!-- pass 34 -->
527523
<!-- fail 25 -->
528524
<!-- cancelled 3 -->
529525
<!-- skipped 9 -->

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ test('callback async throw after done', (t, done) => {
274274
done();
275275
});
276276

277-
test('only is set but not in only mode', { only: true }, async (t) => {
277+
test('only is set on subtests but not in only mode', async (t) => {
278278
// All of these subtests should run.
279279
await t.test('running subtest 1');
280280
t.runOnly(true);

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

+7-15
Original file line numberDiff line numberDiff line change
@@ -463,35 +463,27 @@ ok 48 - callback async throw after done
463463
---
464464
duration_ms: *
465465
...
466-
# Subtest: only is set but not in only mode
466+
# Subtest: only is set on subtests but not in only mode
467467
# Subtest: running subtest 1
468468
ok 1 - running subtest 1
469469
---
470470
duration_ms: *
471471
...
472-
# Subtest: running subtest 2
473-
ok 2 - running subtest 2
474-
---
475-
duration_ms: *
476-
...
477-
# 'only' and 'runOnly' require the --test-only command-line option.
478472
# Subtest: running subtest 3
479-
ok 3 - running subtest 3
473+
ok 2 - running subtest 3
480474
---
481475
duration_ms: *
482476
...
483-
# 'only' and 'runOnly' require the --test-only command-line option.
484477
# Subtest: running subtest 4
485-
ok 4 - running subtest 4
478+
ok 3 - running subtest 4
486479
---
487480
duration_ms: *
488481
...
489-
1..4
490-
ok 49 - only is set but not in only mode
482+
1..3
483+
ok 49 - only is set on subtests but not in only mode
491484
---
492485
duration_ms: *
493486
...
494-
# 'only' and 'runOnly' require the --test-only command-line option.
495487
# Subtest: custom inspect symbol fail
496488
not ok 50 - custom inspect symbol fail
497489
---
@@ -718,9 +710,9 @@ not ok 62 - invalid subtest fail
718710
# Error: Test "immediate reject - passes but warns" at test/fixtures/test-runner/output/output.js:(LINE):1 generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
719711
# Error: Test "callback called twice in different ticks" at test/fixtures/test-runner/output/output.js:(LINE):1 generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.
720712
# Error: Test "callback async throw after done" at test/fixtures/test-runner/output/output.js:(LINE):1 generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
721-
# tests 76
713+
# tests 75
722714
# suites 0
723-
# pass 35
715+
# pass 34
724716
# fail 25
725717
# cancelled 3
726718
# skipped 9

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ ok 48 - callback async throw after done
463463
---
464464
duration_ms: *
465465
...
466-
# Subtest: only is set but not in only mode
466+
# Subtest: only is set on subtests but not in only mode
467467
# Subtest: running subtest 1
468468
ok 1 - running subtest 1
469469
---
@@ -487,11 +487,10 @@ ok 48 - callback async throw after done
487487
duration_ms: *
488488
...
489489
1..4
490-
ok 49 - only is set but not in only mode
490+
ok 49 - only is set on subtests but not in only mode
491491
---
492492
duration_ms: *
493493
...
494-
# 'only' and 'runOnly' require the --test-only command-line option.
495494
# Subtest: custom inspect symbol fail
496495
not ok 50 - custom inspect symbol fail
497496
---

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,11 @@
185185
*
186186

187187
callback async throw after done (*ms)
188-
only is set but not in only mode
188+
only is set on subtests but not in only mode
189189
running subtest 1 (*ms)
190-
running subtest 2 (*ms)
191-
'only' and 'runOnly' require the --test-only command-line option.
192190
running subtest 3 (*ms)
193-
'only' and 'runOnly' require the --test-only command-line option.
194191
running subtest 4 (*ms)
195-
only is set but not in only mode (*ms)
196-
'only' and 'runOnly' require the --test-only command-line option.
192+
only is set on subtests but not in only mode (*ms)
197193
custom inspect symbol fail (*ms)
198194
customized
199195

@@ -304,9 +300,9 @@
304300
Error: Test "immediate reject - passes but warns" at test/fixtures/test-runner/output/output.js:86:1 generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
305301
Error: Test "callback called twice in different ticks" at test/fixtures/test-runner/output/output.js:251:1 generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.
306302
Error: Test "callback async throw after done" at test/fixtures/test-runner/output/output.js:269:1 generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
307-
tests 76
303+
tests 75
308304
suites 0
309-
pass 35
305+
pass 34
310306
fail 25
311307
cancelled 3
312308
skipped 9

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,14 @@
185185
*
186186

187187
callback async throw after done (*ms)
188-
only is set but not in only mode
188+
only is set on subtests but not in only mode
189189
running subtest 1 (*ms)
190190
running subtest 2 (*ms)
191191
'only' and 'runOnly' require the --test-only command-line option.
192192
running subtest 3 (*ms)
193193
'only' and 'runOnly' require the --test-only command-line option.
194194
running subtest 4 (*ms)
195-
only is set but not in only mode (*ms)
196-
'only' and 'runOnly' require the --test-only command-line option.
195+
only is set on subtests but not in only mode (*ms)
197196
custom inspect symbol fail (*ms)
198197
customized
199198

test/parallel/test-runner-no-isolation-filtering.js

+22
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ test('works with --test-only', () => {
3131
assert.match(stdout, /ok 1 - suite two - test/);
3232
});
3333

34+
test('works without --test-only', () => {
35+
const args = [
36+
'--test',
37+
'--test-reporter=tap',
38+
'--experimental-test-isolation=none',
39+
fixture1,
40+
fixture2,
41+
];
42+
const child = spawnSync(process.execPath, args);
43+
const stdout = child.stdout.toString();
44+
45+
assert.strictEqual(child.status, 0);
46+
assert.strictEqual(child.signal, null);
47+
assert.match(stdout, /# tests 2/);
48+
assert.match(stdout, /# suites 2/);
49+
assert.match(stdout, /# pass 2/);
50+
assert.match(stdout, /ok 1 - suite one/);
51+
assert.match(stdout, /ok 1 - suite one - test/);
52+
assert.match(stdout, /ok 2 - suite two/);
53+
assert.match(stdout, /ok 1 - suite two - test/);
54+
});
55+
3456
test('works with --test-name-pattern', () => {
3557
const args = [
3658
'--test',

test/parallel/test-runner-no-isolation.mjs

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const stream = run({
1212
});
1313

1414
stream.on('test:fail', mustNotCall());
15-
stream.on('test:pass', mustCall(5));
15+
stream.on('test:pass', mustCall(4));
1616
// eslint-disable-next-line no-unused-vars
1717
for await (const _ of stream);
1818
allowGlobals(globalThis.GLOBAL_ORDER);
@@ -28,12 +28,6 @@ deepStrictEqual(globalThis.GLOBAL_ORDER, [
2828
'afterEach one: suite one - test',
2929
'afterEach two: suite one - test',
3030

31-
'beforeEach one: test one',
32-
'beforeEach two: test one',
33-
'test one',
34-
'afterEach one: test one',
35-
'afterEach two: test one',
36-
3731
'before suite two: suite two',
3832

3933
'beforeEach one: suite two - test',

test/parallel/test-runner-output.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const tests = [
225225
} : false,
226226
{
227227
name: 'test-runner/output/test-diagnostic-warning-without-test-only-flag.js',
228-
flags: ['--test-reporter=tap'],
228+
flags: ['--test', '--test-reporter=tap'],
229229
},
230230
process.features.inspector ? {
231231
name: 'test-runner/output/coverage-width-80.mjs',

0 commit comments

Comments
 (0)