Skip to content

Commit e459598

Browse files
MoLowtargos
authored andcommitted
test_runner: fix test runner watch mode when no positional arguments
PR-URL: #49578 Fixes: #49617 Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 059b194 commit e459598

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/node_options.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors,
188188
} else if (force_repl) {
189189
errors->push_back("either --watch or --interactive "
190190
"can be used, not both");
191-
} else if (argv->size() < 1 || (*argv)[1].empty()) {
191+
} else if (!test_runner && (argv->size() < 1 || (*argv)[1].empty())) {
192192
errors->push_back("--watch requires specifying a file");
193193
}
194194

test/parallel/test-runner-watch-mode.mjs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tmpdir.refresh();
1818
const fixtureContent = {
1919
'dependency.js': 'module.exports = {};',
2020
'dependency.mjs': 'export const a = 1;',
21-
'dependent.js': `
21+
'test.js': `
2222
const test = require('node:test');
2323
require('./dependency.js');
2424
import('./dependency.mjs');
@@ -30,12 +30,12 @@ const fixturePaths = Object.keys(fixtureContent)
3030
Object.entries(fixtureContent)
3131
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));
3232

33-
async function testWatch({ fileToUpdate }) {
33+
async function testWatch({ fileToUpdate, file }) {
3434
const ran1 = util.createDeferredPromise();
3535
const ran2 = util.createDeferredPromise();
3636
const child = spawn(process.execPath,
37-
['--watch', '--test', '--no-warnings', fixturePaths['dependent.js']],
38-
{ encoding: 'utf8', stdio: 'pipe' });
37+
['--watch', '--test', file ? fixturePaths[file] : undefined].filter(Boolean),
38+
{ encoding: 'utf8', stdio: 'pipe', cwd: tmpdir.path });
3939
let stdout = '';
4040

4141
child.stdout.on('data', (data) => {
@@ -48,25 +48,26 @@ async function testWatch({ fileToUpdate }) {
4848
await ran1.promise;
4949
const content = fixtureContent[fileToUpdate];
5050
const path = fixturePaths[fileToUpdate];
51-
const interval = setInterval(() => {
52-
console.log(`Updating ${path}`);
53-
writeFileSync(path, content);
54-
}, 50);
51+
const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000));
5552
await ran2.promise;
5653
clearInterval(interval);
5754
child.kill();
5855
}
5956

6057
describe('test runner watch mode', () => {
6158
it('should run tests repeatedly', async () => {
62-
await testWatch({ fileToUpdate: 'dependent.js' });
59+
await testWatch({ file: 'test.js', fileToUpdate: 'test.js' });
6360
});
6461

6562
it('should run tests with dependency repeatedly', async () => {
66-
await testWatch({ fileToUpdate: 'dependency.js' });
63+
await testWatch({ file: 'test.js', fileToUpdate: 'dependency.js' });
6764
});
6865

6966
it('should run tests with ESM dependency', async () => {
70-
await testWatch({ fileToUpdate: 'dependency.mjs' });
67+
await testWatch({ file: 'test.js', fileToUpdate: 'dependency.mjs' });
68+
});
69+
70+
it('should support running tests without a file', async () => {
71+
await testWatch({ fileToUpdate: 'test.js' });
7172
});
7273
});

0 commit comments

Comments
 (0)