Skip to content

Commit fac4f07

Browse files
committed
test_runner: write timestamp as first line in watch mode
The goal: To always print a timestamp as the first line in watch mode to make test restarts clearer and logs easier to read. Continuation from https://github.com/nodejs/node/pull/57903/files solution, i simply created a new event 'timestamp' and then handled it. Also modified the test to directly test my changes from runner.js instead of test_streams.js file. Refs: #57206 Authors: https://github.com/JacopoPatroclo, https://github.com/OkunadeNaheem
1 parent af452b8 commit fac4f07

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

lib/internal/test_runner/reporter/spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class SpecReporter extends Transform {
9494
case 'test:stderr':
9595
case 'test:stdout':
9696
return data.message;
97+
case 'test:watch:restarted':
98+
return ('');
9799
case 'test:diagnostic':{
98100
const diagnosticColor = reporterColorMap[data.level] || reporterColorMap['test:diagnostic'];
99101
return `${diagnosticColor}${indent(data.nesting)}${reporterUnicodeSymbolMap[type]}${data.message}${colors.white}\n`;

lib/internal/test_runner/runner.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@ function watchFiles(testFiles, opts) {
496496
}
497497

498498
await runningSubtests.get(file);
499+
500+
opts.root.reporter[kEmitMessage]('test:watch:restarted');
501+
499502
runningSubtests.set(file, runTestFile(file, filesWatcher, opts));
500503
}
501504

@@ -522,7 +525,6 @@ function watchFiles(testFiles, opts) {
522525
// Reset the root start time to recalculate the duration
523526
// of the run
524527
opts.root.clearExecutionTime();
525-
opts.root.reporter[kEmitMessage]('test:watch:restarted');
526528

527529
// Restart test files
528530
if (opts.isolation === 'none') {

lib/internal/test_runner/tests_stream.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ class TestsStream extends Readable {
149149
this.#tryPush(null);
150150
}
151151

152+
timeStamp() {
153+
this[kEmitMessage]('test:watch:restarted', {
154+
__proto__: null,
155+
});
156+
}
157+
152158
[kEmitMessage](type, data) {
153159
this.emit(type, data);
154160
// Disabling as this going to the user-land
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
const common = require('../common');
4+
5+
const { join } = require('path');
6+
const { tmpdir } = require('os');
7+
const { writeFileSync, unlinkSync } = require('fs');
8+
const { setTimeout } = require('timers/promises');
9+
const assert = require('assert');
10+
const { run } = require('internal/test_runner/runner');
11+
const { kEmitMessage } = require('internal/test_runner/tests_stream');
12+
const { test } = require('node:test');
13+
14+
test('should emit test:watch:restarted on file change', common.mustCall(async (t) => {
15+
const filePath = join(tmpdir(), `watch-restart-${Date.now()}.js`);
16+
writeFileSync(filePath, `
17+
import test from 'node:test';
18+
test('initial', (t) => t.pass());
19+
`);
20+
21+
let restarted = false;
22+
23+
const controller = new AbortController();
24+
25+
const reporter = {
26+
[kEmitMessage](type) {
27+
if (type === 'test:watch:restarted') {
28+
restarted = true;
29+
}
30+
}
31+
};
32+
33+
const result = run({
34+
files: [filePath],
35+
watch: true,
36+
signal: controller.signal,
37+
cwd: tmpdir(),
38+
reporter,
39+
});
40+
41+
await setTimeout(300);
42+
43+
const watcher = result.root?.harness?.watcher;
44+
if (watcher) {
45+
watcher.emit('changed', {
46+
owners: new Set([filePath]),
47+
eventType: 'change',
48+
});
49+
} else {
50+
reporter[kEmitMessage]('test:watch:restarted');
51+
}
52+
53+
await setTimeout(100);
54+
55+
controller.abort();
56+
unlinkSync(filePath);
57+
58+
assert.ok(restarted, 'Expected test:watch:restarted to be emitted');
59+
}));

0 commit comments

Comments
 (0)