From cab5dcedbe2b2651e8aae1054cb663179860f660 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 4 Sep 2026 08:22:22 +0000 Subject: [PATCH] test: only count restarts after the write in watch emit-restarted test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Gürgün Dayıoğlu Reviewed-By: Moshe Atlow --- .../test-run-watch-emit-restarted.mjs | 28 ++++++++----------- test/test-runner/test-runner.status | 1 - 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/test/test-runner/test-run-watch-emit-restarted.mjs b/test/test-runner/test-run-watch-emit-restarted.mjs index 200231c6904..fd596bd818c 100644 --- a/test/test-runner/test-run-watch-emit-restarted.mjs +++ b/test/test-runner/test-run-watch-emit-restarted.mjs @@ -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']); diff --git a/test/test-runner/test-runner.status b/test/test-runner/test-runner.status index 149d6ab689c..7d2d59d3540 100644 --- a/test/test-runner/test-runner.status +++ b/test/test-runner/test-runner.status @@ -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