Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fix test runner watch mode when no positional arguments #49578

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* 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");
}

Expand Down
23 changes: 12 additions & 11 deletions test/parallel/test-runner-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) => {
Expand All @@ -47,25 +47,26 @@ 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();
}

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' });
});
});