test: only count restarts after the write in watch emit-restarted test

test-run-watch-emit-restarted expected exactly one test:watch:restarted
event, but it starts run({ watch: true }) right after writing the
fixtures into the watched directory. Watch backends that deliver events
with some latency, FSEvents on macOS most visibly, can still report
those setup writes once the first run is under way, which restarts it
and makes the later, intentional write the second restart. The test has
been marked flaky on macOS x64 for that reason.

Wait for the first drain, then require that the write is followed by a
restart and a drain, ignoring whatever the setup produced before it, and
drop the flaky marker.

Refs: https://github.com/nodejs/node/issues/54534
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65780
Fixes: https://github.com/nodejs/node/issues/64226
Refs: https://github.com/nodejs/node/issues/59146
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
Shelley Vohr authored and Antoine du Hamel committed 2026-09-08 22:07:35 +02:00
1 parent 14c3a77bc5
commit 7eb20b1810
2 files changed
+12 -17

No files matched your search

@@ -1,5 +1,5 @@
// Test run({ watch: true }) emits test:watch:restarted when file is updated
import * as common from '../common/index.mjs';
import '../common/index.mjs';
import { run } from 'node:test';
import assert from 'node:assert';
import { writeFileSync } from 'node:fs';
@@ -11,9 +11,8 @@ import { refreshForTestRunnerWatch, skipIfNoWatch, fixtureContent } from '../com
skipIfNoWatch();
refreshForTestRunnerWatch();
let alreadyDrained = false;
const events = [];
const testWatchRestarted = common.mustCall(1);
let written = false;
const controller = new AbortController();
const stream = run({
@@ -21,27 +20,24 @@ const stream = run({
watch: true,
signal: controller.signal,
}).on('data', function({ type }) {
events.push(type);
if (type === 'test:watch:restarted') {
testWatchRestarted();
if (type !== 'test:watch:restarted' && type !== 'test:watch:drained') {
return;
}
if (type === 'test:watch:drained') {
if (alreadyDrained) {
controller.abort();
}
alreadyDrained = true;
events.push(type);
// Watchers with latency (FSEvents) can still report the fixture setup after
// the first run has started, so only a restart after the write below counts.
if (written && type === 'test:watch:drained' && events.at(-2) === 'test:watch:restarted') {
controller.abort();
}
});
await once(stream, 'test:watch:drained');
events.length = 0;
written = true;
writeFileSync(join(tmpdir.path, 'test.js'), fixtureContent['test.js']);
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
assert.partialDeepStrictEqual(events, [
'test:watch:drained',
'test:watch:restarted',
'test:watch:drained',
]);
assert.deepStrictEqual(events.slice(-2), ['test:watch:restarted', 'test:watch:drained']);
-1
View File
@@ -14,4 +14,3 @@ test-watch-create-isolation-none: SKIP
# https://github.com/nodejs/node/issues/54534#issuecomment-5423551021
test-run-watch-cwd-isolation-none: PASS, FLAKY
test-run-watch-cwd-isolation-none-argv: PASS, FLAKY
test-run-watch-emit-restarted: PASS, FLAKY