Skip to content

Commit 7686551

Browse files
MoLowruyadorno
authored andcommitted
test_runner: fix test runner watch mode when no positional arguments
PR-URL: #49578 Fixes: #49617 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 17a05b1 commit 7686551

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/node_options.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors,
173173
} else if (force_repl) {
174174
errors->push_back("either --watch or --interactive "
175175
"can be used, not both");
176-
} else if (argv->size() < 1 || (*argv)[1].empty()) {
176+
} else if (!test_runner && (argv->size() < 1 || (*argv)[1].empty())) {
177177
errors->push_back("--watch requires specifying a file");
178178
}
179179

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tmpdir.refresh();
1717
const fixtureContent = {
1818
'dependency.js': 'module.exports = {};',
1919
'dependency.mjs': 'export const a = 1;',
20-
'dependent.js': `
20+
'test.js': `
2121
const test = require('node:test');
2222
require('./dependency.js');
2323
import('./dependency.mjs');
@@ -29,12 +29,12 @@ const fixturePaths = Object.keys(fixtureContent)
2929
Object.entries(fixtureContent)
3030
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));
3131

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

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

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

6461
it('should run tests with dependency repeatedly', async () => {
65-
await testWatch({ fileToUpdate: 'dependency.js' });
62+
await testWatch({ file: 'test.js', fileToUpdate: 'dependency.js' });
6663
});
6764

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

0 commit comments

Comments
 (0)