From 63c1f3f3c61be17eb3febdd4c14294a9f37fe7a2 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sun, 17 May 2026 10:05:58 -0700 Subject: [PATCH] test: deflake watch mode worker test Trigger watch restarts by appending whitespace instead of rewriting watched modules. This avoids transient empty or partial ESM dependency contents while the restarted worker is loading. Use a separate temporary directory for each subtest so concurrent subtests do not share worker and dependency file names. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 --- test/sequential/test-watch-mode-worker.mjs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/test/sequential/test-watch-mode-worker.mjs b/test/sequential/test-watch-mode-worker.mjs index b7bc1a94a87ba4..41cdf05782cd38 100644 --- a/test/sequential/test-watch-mode-worker.mjs +++ b/test/sequential/test-watch-mode-worker.mjs @@ -5,7 +5,7 @@ import path from 'node:path'; import { execPath } from 'node:process'; import { describe, it } from 'node:test'; import { spawn } from 'node:child_process'; -import { writeFileSync, readFileSync } from 'node:fs'; +import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs'; import { inspect } from 'node:util'; import { pathToFileURL } from 'node:url'; import { createInterface } from 'node:readline'; @@ -13,9 +13,9 @@ import { createInterface } from 'node:readline'; if (common.isIBMi) common.skip('IBMi does not support `fs.watch()`'); -function restart(file, content = readFileSync(file)) { - writeFileSync(file, content); - const timer = setInterval(() => writeFileSync(file, content), common.platformTimeout(250)); +function restart(file) { + appendFileSync(file, '\n'); + const timer = setInterval(() => appendFileSync(file, '\n'), common.platformTimeout(250)); return () => clearInterval(timer); } @@ -26,6 +26,12 @@ function createTmpFile(content = 'console.log(\'running\');', ext = '.js', basen return file; } +function createTmpDir() { + const dir = path.join(tmpdir.path, `${tmpFiles++}`); + mkdirSync(dir); + return dir; +} + async function runWriteSucceed({ file, watchedFile, @@ -78,10 +84,10 @@ async function runWriteSucceed({ } tmpdir.refresh(); -const dir = tmpdir.path; describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => { it('should watch changes to worker - cjs', async () => { + const dir = createTmpDir(); const worker = path.join(dir, 'worker.js'); writeFileSync(worker, ` @@ -109,6 +115,7 @@ const w = new Worker(${JSON.stringify(worker)}); }); it('should watch changes to worker dependencies - cjs', async () => { + const dir = createTmpDir(); const dep = path.join(dir, 'dep.js'); const worker = path.join(dir, 'worker.js'); @@ -142,6 +149,7 @@ const w = new Worker(${JSON.stringify(worker)}); }); it('should watch changes to nested worker dependencies - cjs', async () => { + const dir = createTmpDir(); const subDep = path.join(dir, 'sub-dep.js'); const dep = path.join(dir, 'dep.js'); const worker = path.join(dir, 'worker.js'); @@ -181,6 +189,7 @@ const w = new Worker(${JSON.stringify(worker)}); }); it('should watch changes to worker - esm', async () => { + const dir = createTmpDir(); const worker = path.join(dir, 'worker.mjs'); writeFileSync(worker, ` @@ -208,6 +217,7 @@ new Worker(new URL(${JSON.stringify(pathToFileURL(worker))})); }); it('should watch changes to worker dependencies - esm', async () => { + const dir = createTmpDir(); const dep = path.join(dir, 'dep.mjs'); const worker = path.join(dir, 'worker.mjs'); @@ -241,6 +251,7 @@ new Worker(new URL(${JSON.stringify(pathToFileURL(worker))})); }); it('should watch changes to nested worker dependencies - esm', async () => { + const dir = createTmpDir(); const subDep = path.join(dir, 'sub-dep.mjs'); const dep = path.join(dir, 'dep.mjs'); const worker = path.join(dir, 'worker.mjs');