@@ -8,27 +8,8 @@ async function sleep(ms: number): Promise<void> {
88 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
99}
1010
11- async function waitFor ( fn : ( ) => void ) : Promise < void > {
12- return new Promise ( ( resolve , reject ) => {
13- const intervalTimer = setInterval ( ( ) => {
14- try {
15- fn ( ) ;
16- clearInterval ( intervalTimer ) ;
17- clearTimeout ( timeoutTimer ) ;
18- resolve ( ) ;
19- } catch {
20- // ignore
21- }
22- } , 50 ) ;
23- const timeoutTimer = setTimeout ( ( ) => {
24- clearInterval ( intervalTimer ) ;
25- reject ( new Error ( 'Timeout waiting for condition' ) ) ;
26- } , 1000 ) ;
27- } ) ;
28- }
29-
3011const rootDir = join ( import . meta. dirname , 'fixtures' ) ;
31- const file = join ( rootDir , 'src' , 'a.module.css' ) ;
12+ const cssModuleFile = join ( rootDir , 'src' , 'a.module.css' ) ;
3213
3314// On macOS, chokidar may detect 'add' events for files added before watching starts.
3415// To avoid test flakiness, wait for a short time before starting the watcher.
@@ -38,7 +19,7 @@ const { promise, resolve } = Promise.withResolvers<void>();
3819const watcher = chokidar
3920 . watch ( rootDir , { ignoreInitial : true } )
4021 . on ( 'change' , ( fileName ) => {
41- if ( fileName . endsWith ( 'src/a.module.css' ) ) {
22+ if ( fileName === cssModuleFile ) {
4223 globalThis . changeCount ++ ;
4324 }
4425 } )
@@ -51,15 +32,13 @@ await sleep(100);
5132globalThis . changeCount = 0 ;
5233
5334console . log ( 'update a.module.css' ) ;
54- await writeFile ( file , '.a_1 { color: blue; }' ) ;
55- await waitFor ( ( ) => {
56- assert ( globalThis . changeCount === 1 ) ;
57- } ) ;
35+ await writeFile ( cssModuleFile , '.a_1 { color: blue; }' ) ;
36+ await sleep ( 1000 ) ; // Wait for chokidar to process the change event
37+ assert ( globalThis . changeCount === 1 ) ;
5838
5939console . log ( 'update a.module.css' ) ;
60- await writeFile ( file , '.a_1 { color: yellow; }' ) ;
61- await waitFor ( ( ) => {
62- assert ( globalThis . changeCount === 2 ) ;
63- } ) ;
40+ await writeFile ( cssModuleFile , '.a_1 { color: yellow; }' ) ;
41+ await sleep ( 1000 ) ; // Wait for chokidar to process the change event
42+ assert ( globalThis . changeCount === 2 ) ;
6443
6544await watcher . close ( ) ;
0 commit comments