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 46e63f3 commit 25deaa7
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 @@ -67,10 +71,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 @@ -90,10 +94,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 @@ -114,15 +116,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 @@ -139,16 +135,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 25deaa7

Please sign in to comment.