11cb0ef41Sopenharmony_ciimport * as common from '../common/index.mjs'; 21cb0ef41Sopenharmony_ciimport tmpdir from '../common/tmpdir.js'; 31cb0ef41Sopenharmony_ciimport assert from 'node:assert'; 41cb0ef41Sopenharmony_ciimport path from 'node:path'; 51cb0ef41Sopenharmony_ciimport { execPath } from 'node:process'; 61cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test'; 71cb0ef41Sopenharmony_ciimport { spawn } from 'node:child_process'; 81cb0ef41Sopenharmony_ciimport { writeFileSync, readFileSync, mkdirSync } from 'node:fs'; 91cb0ef41Sopenharmony_ciimport { inspect } from 'node:util'; 101cb0ef41Sopenharmony_ciimport { pathToFileURL } from 'node:url'; 111cb0ef41Sopenharmony_ciimport { createInterface } from 'node:readline'; 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciif (common.isIBMi) 141cb0ef41Sopenharmony_ci common.skip('IBMi does not support `fs.watch()`'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst supportsRecursive = common.isOSX || common.isWindows; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction restart(file, content = readFileSync(file)) { 191cb0ef41Sopenharmony_ci // To avoid flakiness, we save the file repeatedly until test is done 201cb0ef41Sopenharmony_ci writeFileSync(file, content); 211cb0ef41Sopenharmony_ci const timer = setInterval(() => writeFileSync(file, content), common.platformTimeout(2500)); 221cb0ef41Sopenharmony_ci return () => clearInterval(timer); 231cb0ef41Sopenharmony_ci} 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_cilet tmpFiles = 0; 261cb0ef41Sopenharmony_cifunction createTmpFile(content = 'console.log("running");', ext = '.js', basename = tmpdir.path) { 271cb0ef41Sopenharmony_ci const file = path.join(basename, `${tmpFiles++}${ext}`); 281cb0ef41Sopenharmony_ci writeFileSync(file, content); 291cb0ef41Sopenharmony_ci return file; 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciasync function runWriteSucceed({ 331cb0ef41Sopenharmony_ci file, watchedFile, args = [file], completed = 'Completed running', restarts = 2 341cb0ef41Sopenharmony_ci}) { 351cb0ef41Sopenharmony_ci const child = spawn(execPath, ['--watch', '--no-warnings', ...args], { encoding: 'utf8', stdio: 'pipe' }); 361cb0ef41Sopenharmony_ci let completes = 0; 371cb0ef41Sopenharmony_ci let cancelRestarts = () => {}; 381cb0ef41Sopenharmony_ci let stderr = ''; 391cb0ef41Sopenharmony_ci const stdout = []; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci child.stderr.on('data', (data) => { 421cb0ef41Sopenharmony_ci stderr += data; 431cb0ef41Sopenharmony_ci }); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci try { 461cb0ef41Sopenharmony_ci // Break the chunks into lines 471cb0ef41Sopenharmony_ci for await (const data of createInterface({ input: child.stdout })) { 481cb0ef41Sopenharmony_ci if (!data.startsWith('Waiting for graceful termination') && !data.startsWith('Gracefully restarted')) { 491cb0ef41Sopenharmony_ci stdout.push(data); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci if (data.startsWith(completed)) { 521cb0ef41Sopenharmony_ci completes++; 531cb0ef41Sopenharmony_ci if (completes === restarts) { 541cb0ef41Sopenharmony_ci break; 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci if (completes === 1) { 571cb0ef41Sopenharmony_ci cancelRestarts = restart(watchedFile); 581cb0ef41Sopenharmony_ci } 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci } finally { 621cb0ef41Sopenharmony_ci child.kill(); 631cb0ef41Sopenharmony_ci cancelRestarts(); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci return { stdout, stderr }; 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciasync function failWriteSucceed({ file, watchedFile }) { 691cb0ef41Sopenharmony_ci const child = spawn(execPath, ['--watch', '--no-warnings', file], { encoding: 'utf8', stdio: 'pipe' }); 701cb0ef41Sopenharmony_ci let cancelRestarts = () => {}; 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci try { 731cb0ef41Sopenharmony_ci // Break the chunks into lines 741cb0ef41Sopenharmony_ci for await (const data of createInterface({ input: child.stdout })) { 751cb0ef41Sopenharmony_ci if (data.startsWith('Completed running')) { 761cb0ef41Sopenharmony_ci break; 771cb0ef41Sopenharmony_ci } 781cb0ef41Sopenharmony_ci if (data.startsWith('Failed running')) { 791cb0ef41Sopenharmony_ci cancelRestarts = restart(watchedFile, 'console.log("test has ran");'); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci } finally { 831cb0ef41Sopenharmony_ci child.kill(); 841cb0ef41Sopenharmony_ci cancelRestarts(); 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_citmpdir.refresh(); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_cidescribe('watch mode', { concurrency: true, timeout: 60_000 }, () => { 911cb0ef41Sopenharmony_ci it('should watch changes to a file - event loop ended', async () => { 921cb0ef41Sopenharmony_ci const file = createTmpFile(); 931cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file }); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 961cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 971cb0ef41Sopenharmony_ci 'running', 981cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 991cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 1001cb0ef41Sopenharmony_ci 'running', 1011cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 1021cb0ef41Sopenharmony_ci ]); 1031cb0ef41Sopenharmony_ci }); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci it('should watch changes to a failing file', async () => { 1061cb0ef41Sopenharmony_ci const file = createTmpFile('throw new Error("fails");'); 1071cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, completed: 'Failed running' }); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci assert.match(stderr, /Error: fails\r?\n/); 1101cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 1111cb0ef41Sopenharmony_ci `Failed running ${inspect(file)}`, 1121cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 1131cb0ef41Sopenharmony_ci `Failed running ${inspect(file)}`, 1141cb0ef41Sopenharmony_ci ]); 1151cb0ef41Sopenharmony_ci }); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci it('should watch changes to a file with watch-path', { 1181cb0ef41Sopenharmony_ci skip: !supportsRecursive, 1191cb0ef41Sopenharmony_ci }, async () => { 1201cb0ef41Sopenharmony_ci const dir = path.join(tmpdir.path, 'subdir1'); 1211cb0ef41Sopenharmony_ci mkdirSync(dir); 1221cb0ef41Sopenharmony_ci const file = createTmpFile(); 1231cb0ef41Sopenharmony_ci const watchedFile = createTmpFile('', '.js', dir); 1241cb0ef41Sopenharmony_ci const args = ['--watch-path', dir, file]; 1251cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile, args }); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 1281cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 1291cb0ef41Sopenharmony_ci 'running', 1301cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 1311cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 1321cb0ef41Sopenharmony_ci 'running', 1331cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 1341cb0ef41Sopenharmony_ci ]); 1351cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 1361cb0ef41Sopenharmony_ci }); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci it('should watch when running an non-existing file - when specified under --watch-path', { 1391cb0ef41Sopenharmony_ci skip: !supportsRecursive 1401cb0ef41Sopenharmony_ci }, async () => { 1411cb0ef41Sopenharmony_ci const dir = path.join(tmpdir.path, 'subdir2'); 1421cb0ef41Sopenharmony_ci mkdirSync(dir); 1431cb0ef41Sopenharmony_ci const file = path.join(dir, 'non-existing.js'); 1441cb0ef41Sopenharmony_ci const watchedFile = createTmpFile('', '.js', dir); 1451cb0ef41Sopenharmony_ci const args = ['--watch-path', dir, file]; 1461cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile, args, completed: 'Failed running' }); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci assert.match(stderr, /Error: Cannot find module/g); 1491cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 1501cb0ef41Sopenharmony_ci `Failed running ${inspect(file)}`, 1511cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 1521cb0ef41Sopenharmony_ci `Failed running ${inspect(file)}`, 1531cb0ef41Sopenharmony_ci ]); 1541cb0ef41Sopenharmony_ci }); 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci it('should watch when running an non-existing file - when specified under --watch-path with equals', { 1571cb0ef41Sopenharmony_ci skip: !supportsRecursive 1581cb0ef41Sopenharmony_ci }, async () => { 1591cb0ef41Sopenharmony_ci const dir = path.join(tmpdir.path, 'subdir3'); 1601cb0ef41Sopenharmony_ci mkdirSync(dir); 1611cb0ef41Sopenharmony_ci const file = path.join(dir, 'non-existing.js'); 1621cb0ef41Sopenharmony_ci const watchedFile = createTmpFile('', '.js', dir); 1631cb0ef41Sopenharmony_ci const args = [`--watch-path=${dir}`, file]; 1641cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile, args, completed: 'Failed running' }); 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci assert.match(stderr, /Error: Cannot find module/g); 1671cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 1681cb0ef41Sopenharmony_ci `Failed running ${inspect(file)}`, 1691cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 1701cb0ef41Sopenharmony_ci `Failed running ${inspect(file)}`, 1711cb0ef41Sopenharmony_ci ]); 1721cb0ef41Sopenharmony_ci }); 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci it('should watch changes to a file - event loop blocked', { timeout: 10_000 }, async () => { 1751cb0ef41Sopenharmony_ci const file = createTmpFile(` 1761cb0ef41Sopenharmony_ciconsole.log("running"); 1771cb0ef41Sopenharmony_ciAtomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0); 1781cb0ef41Sopenharmony_ciconsole.log("don't show me");`); 1791cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, completed: 'running' }); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 1821cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 1831cb0ef41Sopenharmony_ci 'running', 1841cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 1851cb0ef41Sopenharmony_ci 'running', 1861cb0ef41Sopenharmony_ci ]); 1871cb0ef41Sopenharmony_ci }); 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci it('should watch changes to dependencies - cjs', async () => { 1901cb0ef41Sopenharmony_ci const dependency = createTmpFile('module.exports = {};'); 1911cb0ef41Sopenharmony_ci const file = createTmpFile(` 1921cb0ef41Sopenharmony_ciconst dependency = require(${JSON.stringify(dependency)}); 1931cb0ef41Sopenharmony_ciconsole.log(dependency); 1941cb0ef41Sopenharmony_ci`); 1951cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: dependency }); 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 1981cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 1991cb0ef41Sopenharmony_ci '{}', 2001cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2011cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 2021cb0ef41Sopenharmony_ci '{}', 2031cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2041cb0ef41Sopenharmony_ci ]); 2051cb0ef41Sopenharmony_ci }); 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci it('should watch changes to dependencies - esm', async () => { 2081cb0ef41Sopenharmony_ci const dependency = createTmpFile('module.exports = {};'); 2091cb0ef41Sopenharmony_ci const file = createTmpFile(` 2101cb0ef41Sopenharmony_ciimport dependency from ${JSON.stringify(pathToFileURL(dependency))}; 2111cb0ef41Sopenharmony_ciconsole.log(dependency); 2121cb0ef41Sopenharmony_ci`, '.mjs'); 2131cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: dependency }); 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 2161cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 2171cb0ef41Sopenharmony_ci '{}', 2181cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2191cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 2201cb0ef41Sopenharmony_ci '{}', 2211cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2221cb0ef41Sopenharmony_ci ]); 2231cb0ef41Sopenharmony_ci }); 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci it('should restart multiple times', async () => { 2261cb0ef41Sopenharmony_ci const file = createTmpFile(); 2271cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, restarts: 3 }); 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 2301cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 2311cb0ef41Sopenharmony_ci 'running', 2321cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2331cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 2341cb0ef41Sopenharmony_ci 'running', 2351cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2361cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 2371cb0ef41Sopenharmony_ci 'running', 2381cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2391cb0ef41Sopenharmony_ci ]); 2401cb0ef41Sopenharmony_ci }); 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci it('should pass arguments to file', async () => { 2431cb0ef41Sopenharmony_ci const file = createTmpFile(` 2441cb0ef41Sopenharmony_ciconst { parseArgs } = require('node:util'); 2451cb0ef41Sopenharmony_ciconst { values } = parseArgs({ options: { random: { type: 'string' } } }); 2461cb0ef41Sopenharmony_ciconsole.log(values.random); 2471cb0ef41Sopenharmony_ci `); 2481cb0ef41Sopenharmony_ci const random = Date.now().toString(); 2491cb0ef41Sopenharmony_ci const args = [file, '--random', random]; 2501cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, args }); 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 2531cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 2541cb0ef41Sopenharmony_ci random, 2551cb0ef41Sopenharmony_ci `Completed running ${inspect(`${file} --random ${random}`)}`, 2561cb0ef41Sopenharmony_ci `Restarting ${inspect(`${file} --random ${random}`)}`, 2571cb0ef41Sopenharmony_ci random, 2581cb0ef41Sopenharmony_ci `Completed running ${inspect(`${file} --random ${random}`)}`, 2591cb0ef41Sopenharmony_ci ]); 2601cb0ef41Sopenharmony_ci }); 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci it('should not load --require modules in main process', async () => { 2631cb0ef41Sopenharmony_ci const file = createTmpFile(); 2641cb0ef41Sopenharmony_ci const required = createTmpFile('setImmediate(() => process.exit(0));'); 2651cb0ef41Sopenharmony_ci const args = ['--require', required, file]; 2661cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, args }); 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 2691cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 2701cb0ef41Sopenharmony_ci 'running', 2711cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2721cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 2731cb0ef41Sopenharmony_ci 'running', 2741cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2751cb0ef41Sopenharmony_ci ]); 2761cb0ef41Sopenharmony_ci }); 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_ci it('should not load --import modules in main process', { 2791cb0ef41Sopenharmony_ci skip: 'enable once --import is backported', 2801cb0ef41Sopenharmony_ci }, async () => { 2811cb0ef41Sopenharmony_ci const file = createTmpFile(); 2821cb0ef41Sopenharmony_ci const imported = pathToFileURL(createTmpFile('setImmediate(() => process.exit(0));')); 2831cb0ef41Sopenharmony_ci const args = ['--import', imported, file]; 2841cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, args }); 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 2871cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 2881cb0ef41Sopenharmony_ci 'running', 2891cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2901cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 2911cb0ef41Sopenharmony_ci 'running', 2921cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 2931cb0ef41Sopenharmony_ci ]); 2941cb0ef41Sopenharmony_ci }); 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci // TODO: Remove skip after https://github.com/nodejs/node/pull/45271 lands 2971cb0ef41Sopenharmony_ci it('should not watch when running an missing file', { 2981cb0ef41Sopenharmony_ci skip: !supportsRecursive 2991cb0ef41Sopenharmony_ci }, async () => { 3001cb0ef41Sopenharmony_ci const nonExistingfile = path.join(tmpdir.path, `${tmpFiles++}.js`); 3011cb0ef41Sopenharmony_ci await failWriteSucceed({ file: nonExistingfile, watchedFile: nonExistingfile }); 3021cb0ef41Sopenharmony_ci }); 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci it('should not watch when running an missing mjs file', { 3051cb0ef41Sopenharmony_ci skip: !supportsRecursive 3061cb0ef41Sopenharmony_ci }, async () => { 3071cb0ef41Sopenharmony_ci const nonExistingfile = path.join(tmpdir.path, `${tmpFiles++}.mjs`); 3081cb0ef41Sopenharmony_ci await failWriteSucceed({ file: nonExistingfile, watchedFile: nonExistingfile }); 3091cb0ef41Sopenharmony_ci }); 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci it('should watch changes to previously missing dependency', { 3121cb0ef41Sopenharmony_ci skip: !supportsRecursive 3131cb0ef41Sopenharmony_ci }, async () => { 3141cb0ef41Sopenharmony_ci const dependency = path.join(tmpdir.path, `${tmpFiles++}.js`); 3151cb0ef41Sopenharmony_ci const relativeDependencyPath = `./${path.basename(dependency)}`; 3161cb0ef41Sopenharmony_ci const dependant = createTmpFile(`console.log(require('${relativeDependencyPath}'))`); 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ci await failWriteSucceed({ file: dependant, watchedFile: dependency }); 3191cb0ef41Sopenharmony_ci }); 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci it('should watch changes to previously missing ESM dependency', { 3221cb0ef41Sopenharmony_ci skip: !supportsRecursive 3231cb0ef41Sopenharmony_ci }, async () => { 3241cb0ef41Sopenharmony_ci const relativeDependencyPath = `./${tmpFiles++}.mjs`; 3251cb0ef41Sopenharmony_ci const dependency = path.join(tmpdir.path, relativeDependencyPath); 3261cb0ef41Sopenharmony_ci const dependant = createTmpFile(`import ${JSON.stringify(relativeDependencyPath)}`, '.mjs'); 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci await failWriteSucceed({ file: dependant, watchedFile: dependency }); 3291cb0ef41Sopenharmony_ci }); 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_ci it('should clear output between runs', async () => { 3321cb0ef41Sopenharmony_ci const file = createTmpFile(); 3331cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file }); 3341cb0ef41Sopenharmony_ci 3351cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 3361cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 3371cb0ef41Sopenharmony_ci 'running', 3381cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 3391cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 3401cb0ef41Sopenharmony_ci 'running', 3411cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 3421cb0ef41Sopenharmony_ci ]); 3431cb0ef41Sopenharmony_ci }); 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci it('should preserve output when --watch-preserve-output flag is passed', async () => { 3461cb0ef41Sopenharmony_ci const file = createTmpFile(); 3471cb0ef41Sopenharmony_ci const args = ['--watch-preserve-output', file]; 3481cb0ef41Sopenharmony_ci const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, args }); 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 3511cb0ef41Sopenharmony_ci assert.deepStrictEqual(stdout, [ 3521cb0ef41Sopenharmony_ci 'running', 3531cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 3541cb0ef41Sopenharmony_ci `Restarting ${inspect(file)}`, 3551cb0ef41Sopenharmony_ci 'running', 3561cb0ef41Sopenharmony_ci `Completed running ${inspect(file)}`, 3571cb0ef41Sopenharmony_ci ]); 3581cb0ef41Sopenharmony_ci }); 3591cb0ef41Sopenharmony_ci}); 360