Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test_runner: emit diagnostics when watch mode drains
  • Loading branch information
MoLow committed Mar 17, 2024
commit 72fbe9755073d5876a78792ea540dd1c986d27b3
25 changes: 15 additions & 10 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,26 @@ function setup(root) {
__proto__: null,
allowTestsToRun: false,
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,
teardown: exitHandler,
};
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 @@ -36,6 +36,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 @@ -378,6 +379,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 @@ -405,6 +407,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 @@ -431,7 +434,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 @@ -847,7 +847,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 @@ -195,9 +195,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