Skip to content

Commit

Permalink
test: remove setInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarchini committed Sep 20, 2024
1 parent 462fd34 commit 29d93c6
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions test/parallel/test-runner-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeEach } from 'node:test';
import { once } from 'node:events';
import assert from 'node:assert';
import { spawn } from 'node:child_process';
import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs';
import { writeFileSync, renameSync, unlinkSync } from 'node:fs';
import util from 'internal/util';
import tmpdir from '../common/tmpdir.js';

Expand All @@ -29,6 +29,10 @@ import('data:text/javascript,');
test('test has ran');`,
};

function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function refresh() {
tmpdir.refresh();
fixturePaths = Object.keys(fixtureContent)
Expand Down Expand Up @@ -69,10 +73,10 @@ async function testWatch({
currentRun = '';
const content = fixtureContent[fileToUpdate];
const path = fixturePaths[fileToUpdate];
const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000));
writeFileSync(path, content);
await wait(common.platformTimeout(1000));
await ran2.promise;
runs.push(currentRun);
clearInterval(interval);
child.kill();
await once(child, 'exit');

Expand All @@ -92,10 +96,8 @@ async function testWatch({
currentRun = '';
const fileToRenamePath = tmpdir.resolve(fileToUpdate);
const newFileNamePath = tmpdir.resolve(`test-renamed-${fileToUpdate}`);
const interval = setInterval(() => {
renameSync(fileToRenamePath, newFileNamePath);
clearInterval(interval);
}, common.platformTimeout(1000));
renameSync(fileToRenamePath, newFileNamePath);
await wait(common.platformTimeout(1000));
await ran2.promise;
runs.push(currentRun);
child.kill();
Expand All @@ -116,15 +118,9 @@ async function testWatch({
runs.push(currentRun);
currentRun = '';
const fileToDeletePath = tmpdir.resolve(fileToUpdate);
const interval = setInterval(() => {
if (existsSync(fileToDeletePath)) {
unlinkSync(fileToDeletePath);
} else {
ran2.resolve();
clearInterval(interval);
}
}, common.platformTimeout(2000));
await ran2.promise;
unlinkSync(fileToDeletePath);
await wait(common.platformTimeout(2000));
ran2.resolve();
runs.push(currentRun);
child.kill();
await once(child, 'exit');
Expand All @@ -141,16 +137,10 @@ async function testWatch({
runs.push(currentRun);
currentRun = '';
const newFilePath = tmpdir.resolve(fileToCreate);
const interval = setInterval(
() => writeFileSync(
newFilePath,
'module.exports = {};'
),
common.platformTimeout(1000)
);
writeFileSync(newFilePath, 'module.exports = {};');
await wait(common.platformTimeout(1000));
await ran2.promise;
runs.push(currentRun);
clearInterval(interval);
child.kill();
await once(child, 'exit');

Expand Down

0 comments on commit 29d93c6

Please sign in to comment.