Skip to content

Commit 25deaa7

Browse files
committed
test: remove setInterval
1 parent 46e63f3 commit 25deaa7

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)
@@ -67,10 +71,10 @@ async function testWatch({
6771
currentRun = '';
6872
const content = fixtureContent[fileToUpdate];
6973
const path = fixturePaths[fileToUpdate];
70-
const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000));
74+
writeFileSync(path, content);
75+
await wait(common.platformTimeout(1000));
7176
await ran2.promise;
7277
runs.push(currentRun);
73-
clearInterval(interval);
7478
child.kill();
7579
await once(child, 'exit');
7680

@@ -90,10 +94,8 @@ async function testWatch({
9094
currentRun = '';
9195
const fileToRenamePath = tmpdir.resolve(fileToUpdate);
9296
const newFileNamePath = tmpdir.resolve(`test-renamed-${fileToUpdate}`);
93-
const interval = setInterval(() => {
94-
renameSync(fileToRenamePath, newFileNamePath);
95-
clearInterval(interval);
96-
}, common.platformTimeout(1000));
97+
renameSync(fileToRenamePath, newFileNamePath);
98+
await wait(common.platformTimeout(1000));
9799
await ran2.promise;
98100
runs.push(currentRun);
99101
child.kill();
@@ -114,15 +116,9 @@ async function testWatch({
114116
runs.push(currentRun);
115117
currentRun = '';
116118
const fileToDeletePath = tmpdir.resolve(fileToUpdate);
117-
const interval = setInterval(() => {
118-
if (existsSync(fileToDeletePath)) {
119-
unlinkSync(fileToDeletePath);
120-
} else {
121-
ran2.resolve();
122-
clearInterval(interval);
123-
}
124-
}, common.platformTimeout(2000));
125-
await ran2.promise;
119+
unlinkSync(fileToDeletePath);
120+
await wait(common.platformTimeout(2000));
121+
ran2.resolve();
126122
runs.push(currentRun);
127123
child.kill();
128124
await once(child, 'exit');
@@ -139,16 +135,10 @@ async function testWatch({
139135
runs.push(currentRun);
140136
currentRun = '';
141137
const newFilePath = tmpdir.resolve(fileToCreate);
142-
const interval = setInterval(
143-
() => writeFileSync(
144-
newFilePath,
145-
'module.exports = {};'
146-
),
147-
common.platformTimeout(1000)
148-
);
138+
writeFileSync(newFilePath, 'module.exports = {};');
139+
await wait(common.platformTimeout(1000));
149140
await ran2.promise;
150141
runs.push(currentRun);
151-
clearInterval(interval);
152142
child.kill();
153143
await once(child, 'exit');
154144

0 commit comments

Comments
 (0)