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