Skip to content

Commit 11681c9

Browse files
MoLowyjl9903
authored andcommitted
test_runner: fix test counting
PR-URL: nodejs#47675 Fixes: nodejs#47365 Fixes: nodejs#47696 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent a8fab90 commit 11681c9

File tree

8 files changed

+27
-26
lines changed

8 files changed

+27
-26
lines changed

lib/internal/test_runner/harness.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function setup(root) {
176176
cancelled: 0,
177177
skipped: 0,
178178
todo: 0,
179-
planned: 0,
179+
topLevel: 0,
180180
suites: 0,
181181
},
182182
};

lib/internal/test_runner/runner.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ const {
55
ArrayPrototypeFilter,
66
ArrayPrototypeForEach,
77
ArrayPrototypeIncludes,
8-
ArrayPrototypeIndexOf,
98
ArrayPrototypeMap,
109
ArrayPrototypePush,
1110
ArrayPrototypeSlice,
1211
ArrayPrototypeSome,
1312
ArrayPrototypeSort,
14-
ArrayPrototypeSplice,
15-
Number,
1613
ObjectAssign,
1714
PromisePrototypeThen,
1815
SafePromiseAll,
@@ -207,7 +204,7 @@ class FileTest extends Test {
207204

208205
const diagnostics = YAMLToJs(node.diagnostics);
209206
const cancelled = kCanceledTests.has(diagnostics.error?.failureType);
210-
const testNumber = nesting === 0 ? (Number(node.id) + this.testNumber - 1) : node.id;
207+
const testNumber = nesting === 0 ? (this.root.harness.counters.topLevel + 1) : node.id;
211208
const method = pass ? 'ok' : 'fail';
212209
this.reporter[method](nesting, this.name, testNumber, node.description, diagnostics, directive);
213210
if (nesting === 0) {
@@ -335,17 +332,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
335332
throw err;
336333
}
337334
});
338-
const promise = subtest.start();
339-
if (filesWatcher) {
340-
return PromisePrototypeThen(promise, () => {
341-
const index = ArrayPrototypeIndexOf(root.subtests, subtest);
342-
if (index !== -1) {
343-
ArrayPrototypeSplice(root.subtests, index, 1);
344-
root.waitingOn--;
345-
}
346-
});
347-
}
348-
return promise;
335+
return subtest.start();
349336
}
350337

351338
function watchFiles(testFiles, root, inspectPort, testNamePatterns) {
@@ -361,6 +348,10 @@ function watchFiles(testFiles, root, inspectPort, testNamePatterns) {
361348
runningProcess.kill();
362349
await once(runningProcess, 'exit');
363350
}
351+
if (!runningSubtests.size) {
352+
// Reset the topLevel counter
353+
root.harness.counters.topLevel = 0;
354+
}
364355
await runningSubtests.get(file);
365356
runningSubtests.set(file, runTestFile(file, root, inspectPort, filesWatcher, testNamePatterns));
366357
}, undefined, (error) => {

lib/internal/test_runner/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ class Test extends AsyncResource {
637637
this.parent.processPendingSubtests();
638638
} else if (!this.reported) {
639639
this.reported = true;
640-
this.reporter.plan(this.nesting, kFilename, this.root.harness.counters.planned);
640+
this.reporter.plan(this.nesting, kFilename, this.root.harness.counters.topLevel);
641641

642642
for (let i = 0; i < this.diagnostics.length; i++) {
643643
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i]);

lib/internal/test_runner/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function parseCommandLine() {
227227

228228
function countCompletedTest(test, harness = test.root.harness) {
229229
if (test.nesting === 0) {
230-
harness.counters.planned++;
230+
harness.counters.topLevel++;
231231
}
232232
if (test.reportedType === 'suite') {
233233
harness.counters.suites++;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ const fixtures = require('../../../common/fixtures');
55
const spawn = require('node:child_process').spawn;
66

77
spawn(process.execPath,
8-
['--no-warnings', '--test', '--test-reporter', 'tap', fixtures.path('test-runner/output/output.js')],
8+
['--no-warnings', '--test', '--test-reporter', 'tap', fixtures.path('test-runner/output/output.js'), fixtures.path('test-runner/output/single.js')],
99
{ stdio: 'inherit' });

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,15 @@ not ok 66 - invalid subtest fail
672672
# Warning: Test "immediate reject - passes but warns" 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.
673673
# Warning: Test "callback called twice in different ticks" 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.
674674
# Warning: Test "callback async throw after done" 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.
675-
1..66
676-
# tests 80
675+
# Subtest: last test
676+
ok 67 - last test
677+
---
678+
duration_ms: *
679+
...
680+
1..67
681+
# tests 81
677682
# suites 0
678-
# pass 37
683+
# pass 38
679684
# fail 25
680685
# cancelled 3
681686
# skipped 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
const test = require('node:test');
4+
test('last test', () => {});

test/parallel/test-runner-watch-mode.mjs

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ async function testWatch({ files, fileToUpdate }) {
1414

1515
child.stdout.on('data', (data) => {
1616
stdout += data.toString();
17-
const matches = stdout.match(/test has ran/g);
18-
if (matches?.length >= 1) ran1.resolve();
19-
if (matches?.length >= 2) ran2.resolve();
17+
const testRuns = stdout.match(/ - test has ran/g);
18+
if (testRuns?.length >= 1) ran1.resolve();
19+
if (testRuns?.length >= 2) ran2.resolve();
2020
});
2121

2222
await ran1.promise;
23-
const interval = setInterval(() => writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8')), 50);
23+
const content = readFileSync(fileToUpdate, 'utf8');
24+
const interval = setInterval(() => writeFileSync(fileToUpdate, content), 10);
2425
await ran2.promise;
2526
clearInterval(interval);
2627
child.kill();

0 commit comments

Comments
 (0)