Skip to content

Commit e832333

Browse files
committed
test: remove setInterval
1 parent 17baacd commit e832333

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

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

+14-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, it, beforeEach } from 'node:test';
44
import { once } from 'node:events';
55
import assert from 'node:assert';
66
import { spawn } from 'node:child_process';
7-
import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs';
7+
import { writeFileSync, renameSync, unlinkSync } from 'node:fs';
88
import util from 'internal/util';
99
import tmpdir from '../common/tmpdir.js';
1010

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

32+
function wait(ms) {
33+
return new Promise((resolve) => setTimeout(resolve, ms));
34+
}
35+
3236
function refresh() {
3337
tmpdir.refresh();
3438
fixturePaths = Object.keys(fixtureContent)
@@ -69,10 +73,10 @@ async function testWatch({
6973
currentRun = '';
7074
const content = fixtureContent[fileToUpdate];
7175
const path = fixturePaths[fileToUpdate];
72-
const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000));
76+
writeFileSync(path, content);
77+
await wait(common.platformTimeout(1000));
7378
await ran2.promise;
7479
runs.push(currentRun);
75-
clearInterval(interval);
7680
child.kill();
7781
await once(child, 'exit');
7882

@@ -92,10 +96,8 @@ async function testWatch({
9296
currentRun = '';
9397
const fileToRenamePath = tmpdir.resolve(fileToUpdate);
9498
const newFileNamePath = tmpdir.resolve(`test-renamed-${fileToUpdate}`);
95-
const interval = setInterval(() => {
96-
renameSync(fileToRenamePath, newFileNamePath);
97-
clearInterval(interval);
98-
}, common.platformTimeout(1000));
99+
renameSync(fileToRenamePath, newFileNamePath);
100+
await wait(common.platformTimeout(1000));
99101
await ran2.promise;
100102
runs.push(currentRun);
101103
child.kill();
@@ -116,15 +118,9 @@ async function testWatch({
116118
runs.push(currentRun);
117119
currentRun = '';
118120
const fileToDeletePath = tmpdir.resolve(fileToUpdate);
119-
const interval = setInterval(() => {
120-
if (existsSync(fileToDeletePath)) {
121-
unlinkSync(fileToDeletePath);
122-
} else {
123-
ran2.resolve();
124-
clearInterval(interval);
125-
}
126-
}, common.platformTimeout(2000));
127-
await ran2.promise;
121+
unlinkSync(fileToDeletePath);
122+
await wait(common.platformTimeout(2000));
123+
ran2.resolve();
128124
runs.push(currentRun);
129125
child.kill();
130126
await once(child, 'exit');
@@ -141,16 +137,10 @@ async function testWatch({
141137
runs.push(currentRun);
142138
currentRun = '';
143139
const newFilePath = tmpdir.resolve(fileToCreate);
144-
const interval = setInterval(
145-
() => writeFileSync(
146-
newFilePath,
147-
'module.exports = {};'
148-
),
149-
common.platformTimeout(1000)
150-
);
140+
writeFileSync(newFilePath, 'module.exports = {};');
141+
await wait(common.platformTimeout(1000));
151142
await ran2.promise;
152143
runs.push(currentRun);
153-
clearInterval(interval);
154144
child.kill();
155145
await once(child, 'exit');
156146

0 commit comments

Comments
 (0)