From 1a4ae462f8d2c08a6f35c64fc0598d64de1605d5 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 14 Sep 2023 14:46:18 +0300 Subject: [PATCH] test_runner: fix test runner watch mode when no positional arguments PR-URL: https://github.com/nodejs/node/pull/49578 Fixes: https://github.com/nodejs/node/issues/49617 Reviewed-By: Chemi Atlow Reviewed-By: Benjamin Gruenbaum --- src/node_options.cc | 2 +- test/parallel/test-runner-watch-mode.mjs | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/node_options.cc b/src/node_options.cc index 6ea85e3399be69..b544f1209143c0 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -173,7 +173,7 @@ void EnvironmentOptions::CheckOptions(std::vector* errors, } else if (force_repl) { errors->push_back("either --watch or --interactive " "can be used, not both"); - } else if (argv->size() < 1 || (*argv)[1].empty()) { + } else if (!test_runner && (argv->size() < 1 || (*argv)[1].empty())) { errors->push_back("--watch requires specifying a file"); } diff --git a/test/parallel/test-runner-watch-mode.mjs b/test/parallel/test-runner-watch-mode.mjs index 92a2b972cf6f0d..5ec847330c2c2c 100644 --- a/test/parallel/test-runner-watch-mode.mjs +++ b/test/parallel/test-runner-watch-mode.mjs @@ -17,7 +17,7 @@ tmpdir.refresh(); const fixtureContent = { 'dependency.js': 'module.exports = {};', 'dependency.mjs': 'export const a = 1;', - 'dependent.js': ` + 'test.js': ` const test = require('node:test'); require('./dependency.js'); import('./dependency.mjs'); @@ -29,12 +29,12 @@ const fixturePaths = Object.keys(fixtureContent) Object.entries(fixtureContent) .forEach(([file, content]) => writeFileSync(fixturePaths[file], content)); -async function testWatch({ fileToUpdate }) { +async function testWatch({ fileToUpdate, file }) { const ran1 = util.createDeferredPromise(); const ran2 = util.createDeferredPromise(); const child = spawn(process.execPath, - ['--watch', '--test', '--no-warnings', fixturePaths['dependent.js']], - { encoding: 'utf8', stdio: 'pipe' }); + ['--watch', '--test', file ? fixturePaths[file] : undefined].filter(Boolean), + { encoding: 'utf8', stdio: 'pipe', cwd: tmpdir.path }); let stdout = ''; child.stdout.on('data', (data) => { @@ -47,10 +47,7 @@ async function testWatch({ fileToUpdate }) { await ran1.promise; const content = fixtureContent[fileToUpdate]; const path = fixturePaths[fileToUpdate]; - const interval = setInterval(() => { - console.log(`Updating ${path}`); - writeFileSync(path, content); - }, 50); + const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000)); await ran2.promise; clearInterval(interval); child.kill(); @@ -58,14 +55,18 @@ async function testWatch({ fileToUpdate }) { describe('test runner watch mode', () => { it('should run tests repeatedly', async () => { - await testWatch({ fileToUpdate: 'dependent.js' }); + await testWatch({ file: 'test.js', fileToUpdate: 'test.js' }); }); it('should run tests with dependency repeatedly', async () => { - await testWatch({ fileToUpdate: 'dependency.js' }); + await testWatch({ file: 'test.js', fileToUpdate: 'dependency.js' }); }); it('should run tests with ESM dependency', async () => { - await testWatch({ fileToUpdate: 'dependency.mjs' }); + await testWatch({ file: 'test.js', fileToUpdate: 'dependency.mjs' }); + }); + + it('should support running tests without a file', async () => { + await testWatch({ fileToUpdate: 'test.js' }); }); });