Skip to content

Commit caec996

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 c5fd193 commit caec996

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
@@ -182,20 +182,25 @@ function setup(root) {
182182
root.harness = {
183183
__proto__: null,
184184
bootstrapComplete: false,
185+
watching: false,
185186
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
186-
counters: {
187-
__proto__: null,
188-
all: 0,
189-
failed: 0,
190-
passed: 0,
191-
cancelled: 0,
192-
skipped: 0,
193-
todo: 0,
194-
topLevel: 0,
195-
suites: 0,
187+
resetCounters() {
188+
root.harness.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,
198+
};
196199
},
200+
counters: null,
197201
shouldColorizeTestFiles: false,
198202
};
203+
root.harness.resetCounters();
199204
root.startTime = hrtime();
200205
return root;
201206
}

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: {
@@ -418,6 +419,7 @@ function runTestFile(path, filesWatcher, opts) {
418419
filesWatcher.runningSubtests.delete(path);
419420
if (filesWatcher.runningSubtests.size === 0) {
420421
opts.root.reporter[kEmitMessage]('test:watch:drained');
422+
queueMicrotask(() => opts.root.postRun());
421423
}
422424
}
423425

@@ -445,6 +447,7 @@ function watchFiles(testFiles, opts) {
445447
const runningSubtests = new SafeMap();
446448
const watcher = new FilesWatcher({ __proto__: null, debounce: 200, mode: 'filter', signal: opts.signal });
447449
const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests };
450+
opts.root.harness.watching = true;
448451

449452
watcher.on('changed', ({ owners }) => {
450453
watcher.unfilterFilesOwnedBy(owners);
@@ -471,7 +474,10 @@ function watchFiles(testFiles, opts) {
471474
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
472475
opts.signal.addEventListener(
473476
'abort',
474-
() => opts.root.postRun(),
477+
() => {
478+
opts.root.harness.watching = false;
479+
opts.root.postRun();
480+
},
475481
{ __proto__: null, once: true, [kResistStopPropagation]: true },
476482
);
477483
}

lib/internal/test_runner/test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,12 @@ class Test extends AsyncResource {
785785
reporter.coverage(nesting, loc, coverage);
786786
}
787787

788-
reporter.end();
788+
if (harness.watching) {
789+
this.reported = false;
790+
harness.resetCounters();
791+
} else {
792+
reporter.end();
793+
}
789794
}
790795
}
791796

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)