Skip to content

Commit

Permalink
test_runner: emit diagnostics when watch mode drains
Browse files Browse the repository at this point in the history
PR-URL: #52130
Fixes: #51253
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
MoLow authored and marco-ippolito committed May 3, 2024
1 parent c5fd193 commit caec996
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
25 changes: 15 additions & 10 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,25 @@ function setup(root) {
root.harness = {
__proto__: null,
bootstrapComplete: false,
watching: false,
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
counters: {
__proto__: null,
all: 0,
failed: 0,
passed: 0,
cancelled: 0,
skipped: 0,
todo: 0,
topLevel: 0,
suites: 0,
resetCounters() {
root.harness.counters = {
__proto__: null,
all: 0,
failed: 0,
passed: 0,
cancelled: 0,
skipped: 0,
todo: 0,
topLevel: 0,
suites: 0,
};
},
counters: null,
shouldColorizeTestFiles: false,
};
root.harness.resetCounters();
root.startTime = hrtime();
return root;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const { createInterface } = require('readline');
const { deserializeError } = require('internal/error_serdes');
const { Buffer } = require('buffer');
const { FilesWatcher } = require('internal/watch_mode/files_watcher');
const { queueMicrotask } = require('internal/process/task_queues');
const console = require('internal/console/global');
const {
codes: {
Expand Down Expand Up @@ -418,6 +419,7 @@ function runTestFile(path, filesWatcher, opts) {
filesWatcher.runningSubtests.delete(path);
if (filesWatcher.runningSubtests.size === 0) {
opts.root.reporter[kEmitMessage]('test:watch:drained');
queueMicrotask(() => opts.root.postRun());
}
}

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

watcher.on('changed', ({ owners }) => {
watcher.unfilterFilesOwnedBy(owners);
Expand All @@ -471,7 +474,10 @@ function watchFiles(testFiles, opts) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
opts.signal.addEventListener(
'abort',
() => opts.root.postRun(),
() => {
opts.root.harness.watching = false;
opts.root.postRun();
},
{ __proto__: null, once: true, [kResistStopPropagation]: true },
);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,12 @@ class Test extends AsyncResource {
reporter.coverage(nesting, loc, coverage);
}

reporter.end();
if (harness.watching) {
this.reported = false;
harness.resetCounters();
} else {
reporter.end();
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,16 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
signal: controller.signal,
})
.compose(async function* (source) {
let waitForCancel = 2;
for await (const chunk of source) {
if (chunk.type === 'test:pass') {
if (chunk.type === 'test:watch:drained' ||
(chunk.type === 'test:diagnostic' && chunk.data.message.startsWith('duration_ms'))) {
waitForCancel--;
}
if (waitForCancel === 0) {
controller.abort();
}
if (chunk.type === 'test:pass') {
yield chunk.data.name;
}
}
Expand Down

0 comments on commit caec996

Please sign in to comment.