@@ -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 )
@@ -69,10 +73,10 @@ async function testWatch({
69
73
currentRun = '' ;
70
74
const content = fixtureContent [ fileToUpdate ] ;
71
75
const path = fixturePaths [ fileToUpdate ] ;
72
- const interval = setInterval ( ( ) => writeFileSync ( path , content ) , common . platformTimeout ( 1000 ) ) ;
76
+ writeFileSync ( path , content ) ;
77
+ await wait ( common . platformTimeout ( 1000 ) ) ;
73
78
await ran2 . promise ;
74
79
runs . push ( currentRun ) ;
75
- clearInterval ( interval ) ;
76
80
child . kill ( ) ;
77
81
await once ( child , 'exit' ) ;
78
82
@@ -92,10 +96,8 @@ async function testWatch({
92
96
currentRun = '' ;
93
97
const fileToRenamePath = tmpdir . resolve ( fileToUpdate ) ;
94
98
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 ) ) ;
99
101
await ran2 . promise ;
100
102
runs . push ( currentRun ) ;
101
103
child . kill ( ) ;
@@ -116,15 +118,9 @@ async function testWatch({
116
118
runs . push ( currentRun ) ;
117
119
currentRun = '' ;
118
120
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 ( ) ;
128
124
runs . push ( currentRun ) ;
129
125
child . kill ( ) ;
130
126
await once ( child , 'exit' ) ;
@@ -141,16 +137,10 @@ async function testWatch({
141
137
runs . push ( currentRun ) ;
142
138
currentRun = '' ;
143
139
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 ) ) ;
151
142
await ran2 . promise ;
152
143
runs . push ( currentRun ) ;
153
- clearInterval ( interval ) ;
154
144
child . kill ( ) ;
155
145
await once ( child , 'exit' ) ;
156
146
0 commit comments