From 098b4788ca01e967833df80ad97bd681df66a169 Mon Sep 17 00:00:00 2001 From: Yuya Inoue <65857152+inoway46@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:25:49 +0900 Subject: [PATCH] test: deflake test-watch-mode-files_watcher Some test cases made a single change right after creating a watcher. On macOS, libuv starts the FSEvents stream asynchronously, so that change could be missed and the test hung until it timed out. Keep making the change until the watcher reports it, as the other cases in the file already do. Refs: https://github.com/nodejs/reliability/issues/1715 Refs: https://github.com/nodejs/reliability/issues/1644 Signed-off-by: inoway46 PR-URL: https://github.com/nodejs/node/pull/67298 Refs: https://github.com/nodejs/node/actions/runs/42177414954 Reviewed-By: Christian Aurich Zanettini Martins --- .../test-watch-mode-files_watcher.mjs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-watch-mode-files_watcher.mjs b/test/parallel/test-watch-mode-files_watcher.mjs index 71b9cb2ae0f..bc10d66fe94 100644 --- a/test/parallel/test-watch-mode-files_watcher.mjs +++ b/test/parallel/test-watch-mode-files_watcher.mjs @@ -37,6 +37,9 @@ describe('watch mode file watcher', () => { afterEach(() => watcher.clear()); let counter = 0; + // A newly created watcher might not report changes right away (e.g. on + // macOS the FSEvents stream is started asynchronously), so a single write + // can be missed. Keep writing until a change is reported. function writeAndWaitForChanges(watcher, file) { return new Promise((resolve) => { const interval = setInterval(() => writeFileSync(file, `write ${counter++}`), 100); @@ -93,19 +96,29 @@ describe('watch mode file watcher', () => { ['--watch', file1], { stdio: ['ignore', 'pipe', 'ignore'] }); let completeCount = 0; + let appendInterval; for await (const line of createInterface(child.stdout)) { if (!line.startsWith('Completed running')) { continue; } completeCount++; if (completeCount === 1) { + // The watcher of the child might not report changes yet, so keep + // changing the file until it is reloaded. The interval is longer than + // the default debounce of watch mode so the restart is not deferred. appendFileSync(file1, '\n // append 1'); + appendInterval = setInterval(() => appendFileSync(file1, '\n // append'), + common.platformTimeout(1000)); } // The file is reloaded due to file watching if (completeCount === 2) { + clearInterval(appendInterval); child.kill(); } } + // The child might have exited before the reload. + clearInterval(appendInterval); + assert.strictEqual(completeCount, 2); }); it('should debounce changes', async () => { @@ -135,6 +148,7 @@ describe('watch mode file watcher', () => { watcher.filterFile(file); files.push(file); } + await writeAndWaitForChanges(watcher, files[0]); files.forEach((file) => writeFileSync(file, '1')); files.forEach((file) => writeFileSync(file, '2')); @@ -203,10 +217,7 @@ describe('watch mode file watcher', () => { const file = tmpdir.resolve('file5'); watcher.watchPath(tmpdir.path); - const changed = once(watcher, 'changed'); - await setTimeout(common.platformTimeout(100)); // avoid throttling - writeFileSync(file, 'changed'); - await changed; + await writeAndWaitForChanges(watcher, file); assert.strictEqual(changesCount, 1); });