@@ -4,7 +4,7 @@ import { describe, it, beforeEach } from 'node:test';
4
4
import { once } from 'node:events' ;
5
5
import assert from 'node:assert' ;
6
6
import { spawn } from 'node:child_process' ;
7
- import { writeFileSync , renameSync , unlinkSync , existsSync } from 'node:fs' ;
7
+ import { writeFileSync , renameSync , unlinkSync } from 'node:fs' ;
8
8
import util from 'internal/util' ;
9
9
import tmpdir from '../common/tmpdir.js' ;
10
10
@@ -29,6 +29,10 @@ import('data:text/javascript,');
29
29
test('test has ran');` ,
30
30
} ;
31
31
32
+ function wait ( ms ) {
33
+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
34
+ }
35
+
32
36
function refresh ( ) {
33
37
tmpdir . refresh ( ) ;
34
38
fixturePaths = Object . keys ( fixtureContent )
@@ -67,10 +71,10 @@ async function testWatch({
67
71
currentRun = '' ;
68
72
const content = fixtureContent [ fileToUpdate ] ;
69
73
const path = fixturePaths [ fileToUpdate ] ;
70
- const interval = setInterval ( ( ) => writeFileSync ( path , content ) , common . platformTimeout ( 1000 ) ) ;
74
+ writeFileSync ( path , content ) ;
75
+ await wait ( common . platformTimeout ( 1000 ) ) ;
71
76
await ran2 . promise ;
72
77
runs . push ( currentRun ) ;
73
- clearInterval ( interval ) ;
74
78
child . kill ( ) ;
75
79
await once ( child , 'exit' ) ;
76
80
@@ -90,10 +94,8 @@ async function testWatch({
90
94
currentRun = '' ;
91
95
const fileToRenamePath = tmpdir . resolve ( fileToUpdate ) ;
92
96
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 ) ) ;
97
99
await ran2 . promise ;
98
100
runs . push ( currentRun ) ;
99
101
child . kill ( ) ;
@@ -114,15 +116,9 @@ async function testWatch({
114
116
runs . push ( currentRun ) ;
115
117
currentRun = '' ;
116
118
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 ( ) ;
126
122
runs . push ( currentRun ) ;
127
123
child . kill ( ) ;
128
124
await once ( child , 'exit' ) ;
@@ -139,16 +135,10 @@ async function testWatch({
139
135
runs . push ( currentRun ) ;
140
136
currentRun = '' ;
141
137
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 ) ) ;
149
140
await ran2 . promise ;
150
141
runs . push ( currentRun ) ;
151
- clearInterval ( interval ) ;
152
142
child . kill ( ) ;
153
143
await once ( child , 'exit' ) ;
154
144
0 commit comments