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: fix test-watch-mode #45585

Merged
merged 1 commit into from Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions test/sequential/test-watch-mode.mjs
Expand Up @@ -15,11 +15,16 @@ if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

const supportsRecursive = common.isOSX || common.isWindows;
let disableRestart = false;

function restart(file) {
// To avoid flakiness, we save the file repeatedly until test is done
writeFileSync(file, readFileSync(file));
const timer = setInterval(() => writeFileSync(file, readFileSync(file)), 1000);
const timer = setInterval(() => {
if (!disableRestart) {
writeFileSync(file, readFileSync(file));
}
}, common.platformTimeout(1000));
return () => clearInterval(timer);
}

Expand All @@ -38,11 +43,15 @@ async function spawnWithRestarts({
let stdout = '';
let cancelRestarts;

disableRestart = true;
const child = spawn(execPath, ['--watch', '--no-warnings', ...args], { encoding: 'utf8' });
child.stderr.on('data', (data) => {
stderr += data;
});
child.stdout.on('data', async (data) => {
if (data.toString().includes('Restarting')) {
disableRestart = true;
}
stdout += data;
const restartsCount = stdout.match(new RegExp(`Restarting ${printedArgs.replace(/\\/g, '\\\\')}`, 'g'))?.length ?? 0;
if (restarts === 0 || !isReady(data.toString())) {
Expand All @@ -54,6 +63,9 @@ async function spawnWithRestarts({
return;
}
cancelRestarts ??= restart(watchedFile);
if (isReady(data.toString())) {
disableRestart = false;
}
});

await once(child, 'exit');
Expand Down Expand Up @@ -97,9 +109,9 @@ async function failWriteSucceed({ file, watchedFile }) {

tmpdir.refresh();

// Warning: this suite can run safely with concurrency: true
// only if tests do not watch/depend on the same files
describe('watch mode', { concurrency: true, timeout: 60_000 }, () => {
// Warning: this suite cannot run safely with concurrency: true
// because of the disableRestart flag used for controlling restarts
describe('watch mode', { concurrency: false, timeout: 60_000 }, () => {
it('should watch changes to a file - event loop ended', async () => {
const file = createTmpFile();
const { stderr, stdout } = await spawnWithRestarts({ file });
Expand Down