Skip to content

Commit b6bc245

Browse files
MoLowmarco-ippolito
authored andcommitted
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 e05255d commit b6bc245

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
@@ -35,6 +35,7 @@ const { createInterface } = require('readline');
3535
const { deserializeError } = require('internal/error_serdes');
3636
const { Buffer } = require('buffer');
3737
const { FilesWatcher } = require('internal/watch_mode/files_watcher');
38+
const { queueMicrotask } = require('internal/process/task_queues');
3839
const console = require('internal/console/global');
3940
const {
4041
codes: {
@@ -421,6 +422,7 @@ function runTestFile(path, filesWatcher, opts) {
421422
filesWatcher.runningSubtests.delete(path);
422423
if (filesWatcher.runningSubtests.size === 0) {
423424
opts.root.reporter[kEmitMessage]('test:watch:drained');
425+
queueMicrotask(() => opts.root.postRun());
424426
}
425427
}
426428

@@ -448,6 +450,7 @@ function watchFiles(testFiles, opts) {
448450
const runningSubtests = new SafeMap();
449451
const watcher = new FilesWatcher({ __proto__: null, debounce: 200, mode: 'filter', signal: opts.signal });
450452
const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests };
453+
opts.root.harness.watching = true;
451454

452455
watcher.on('changed', ({ owners }) => {
453456
watcher.unfilterFilesOwnedBy(owners);
@@ -474,7 +477,10 @@ function watchFiles(testFiles, opts) {
474477
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
475478
opts.signal.addEventListener(
476479
'abort',
477-
() => opts.root.postRun(),
480+
() => {
481+
opts.root.harness.watching = false;
482+
opts.root.postRun();
483+
},
478484
{ __proto__: null, once: true, [kResistStopPropagation]: true },
479485
);
480486
}

lib/internal/test_runner/test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,12 @@ class Test extends AsyncResource {
819819
reporter.coverage(nesting, loc, coverage);
820820
}
821821

822-
reporter.end();
822+
if (harness.watching) {
823+
this.reported = false;
824+
harness.resetCounters();
825+
} else {
826+
reporter.end();
827+
}
823828
}
824829
}
825830

test/parallel/test-runner-run.mjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,16 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
203203
signal: controller.signal,
204204
})
205205
.compose(async function* (source) {
206+
let waitForCancel = 2;
206207
for await (const chunk of source) {
207-
if (chunk.type === 'test:pass') {
208+
if (chunk.type === 'test:watch:drained' ||
209+
(chunk.type === 'test:diagnostic' && chunk.data.message.startsWith('duration_ms'))) {
210+
waitForCancel--;
211+
}
212+
if (waitForCancel === 0) {
208213
controller.abort();
214+
}
215+
if (chunk.type === 'test:pass') {
209216
yield chunk.data.name;
210217
}
211218
}

0 commit comments

Comments
 (0)