Skip to content

Commit f9755f6

Browse files
authored
test_runner: emit diagnostics when watch mode drains
PR-URL: #52130 Fixes: #51253 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 61e5de1 commit f9755f6

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

lib/internal/test_runner/harness.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,26 @@ function setup(root) {
184184
__proto__: null,
185185
allowTestsToRun: false,
186186
bootstrapComplete: false,
187+
watching: false,
187188
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
188-
counters: {
189-
__proto__: null,
190-
all: 0,
191-
failed: 0,
192-
passed: 0,
193-
cancelled: 0,
194-
skipped: 0,
195-
todo: 0,
196-
topLevel: 0,
197-
suites: 0,
189+
resetCounters() {
190+
root.harness.counters = {
191+
__proto__: null,
192+
all: 0,
193+
failed: 0,
194+
passed: 0,
195+
cancelled: 0,
196+
skipped: 0,
197+
todo: 0,
198+
topLevel: 0,
199+
suites: 0,
200+
};
198201
},
202+
counters: null,
199203
shouldColorizeTestFiles: false,
200204
teardown: exitHandler,
201205
};
206+
root.harness.resetCounters();
202207
root.startTime = hrtime();
203208
return root;
204209
}

lib/internal/test_runner/runner.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const { createInterface } = require('readline');
3636
const { deserializeError } = require('internal/error_serdes');
3737
const { Buffer } = require('buffer');
3838
const { FilesWatcher } = require('internal/watch_mode/files_watcher');
39+
const { queueMicrotask } = require('internal/process/task_queues');
3940
const console = require('internal/console/global');
4041
const {
4142
codes: {
@@ -378,6 +379,7 @@ function runTestFile(path, filesWatcher, opts) {
378379
filesWatcher.runningSubtests.delete(path);
379380
if (filesWatcher.runningSubtests.size === 0) {
380381
opts.root.reporter[kEmitMessage]('test:watch:drained');
382+
queueMicrotask(() => opts.root.postRun());
381383
}
382384
}
383385

@@ -405,6 +407,7 @@ function watchFiles(testFiles, opts) {
405407
const runningSubtests = new SafeMap();
406408
const watcher = new FilesWatcher({ __proto__: null, debounce: 200, mode: 'filter', signal: opts.signal });
407409
const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests };
410+
opts.root.harness.watching = true;
408411

409412
watcher.on('changed', ({ owners }) => {
410413
watcher.unfilterFilesOwnedBy(owners);
@@ -431,7 +434,10 @@ function watchFiles(testFiles, opts) {
431434
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
432435
opts.signal.addEventListener(
433436
'abort',
434-
() => opts.root.postRun(),
437+
() => {
438+
opts.root.harness.watching = false;
439+
opts.root.postRun();
440+
},
435441
{ __proto__: null, once: true, [kResistStopPropagation]: true },
436442
);
437443
}

lib/internal/test_runner/test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,12 @@ class Test extends AsyncResource {
847847
reporter.coverage(nesting, loc, coverage);
848848
}
849849

850-
reporter.end();
850+
if (harness.watching) {
851+
this.reported = false;
852+
harness.resetCounters();
853+
} else {
854+
reporter.end();
855+
}
851856
}
852857
}
853858

test/parallel/test-runner-run.mjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,16 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
195195
signal: controller.signal,
196196
})
197197
.compose(async function* (source) {
198+
let waitForCancel = 2;
198199
for await (const chunk of source) {
199-
if (chunk.type === 'test:pass') {
200+
if (chunk.type === 'test:watch:drained' ||
201+
(chunk.type === 'test:diagnostic' && chunk.data.message.startsWith('duration_ms'))) {
202+
waitForCancel--;
203+
}
204+
if (waitForCancel === 0) {
200205
controller.abort();
206+
}
207+
if (chunk.type === 'test:pass') {
201208
yield chunk.data.name;
202209
}
203210
}

0 commit comments

Comments
 (0)