@@ -14,11 +14,6 @@ async function sleep(ms: number): Promise<void> {
1414 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
1515}
1616
17- async function waitForWatcherEmitAndReportDiagnostics ( ) : Promise < void > {
18- // In watch mode, emits and diagnostic reports are batched with a 250ms delay. Therefore, a wait longer than 250ms is required.
19- await sleep ( 500 ) ;
20- }
21-
2217describe ( 'runCMK' , ( ) => {
2318 test ( 'emits .d.ts files' , async ( ) => {
2419 const iff = await createIFF ( {
@@ -230,8 +225,10 @@ describe('runCMKInWatchMode', () => {
230225 throw new Error ( 'test error' ) ;
231226 } ) ;
232227 await writeFile ( iff . join ( 'src/a.module.css' ) , '.a_1 { color: yellow; }' ) ;
233- await waitForWatcherEmitAndReportDiagnostics ( ) ;
234- expect ( loggerSpy . logError ) . toHaveBeenCalledTimes ( 3 ) ;
228+ await vi . waitFor ( ( ) => {
229+ // Wait for watcher to emit and report diagnostics
230+ expect ( loggerSpy . logError ) . toHaveBeenCalledTimes ( 3 ) ;
231+ } ) ;
235232 } ) ;
236233 test ( 'reports diagnostics and emits files on changes' , async ( ) => {
237234 const iff = await createIFF ( {
@@ -246,21 +243,27 @@ describe('runCMKInWatchMode', () => {
246243
247244 // Add a file
248245 await writeFile ( iff . join ( 'src/b.module.css' ) , '.b_1 {' ) ;
249- await waitForWatcherEmitAndReportDiagnostics ( ) ;
250- expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 1 ) ;
251- expect ( await iff . readFile ( 'generated/src/b.module.css.d.ts' ) ) . contain ( 'b_1' ) ;
246+ await vi . waitFor ( async ( ) => {
247+ // Wait for watcher to emit and report diagnostics
248+ expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 1 ) ;
249+ expect ( await iff . readFile ( 'generated/src/b.module.css.d.ts' ) ) . contain ( 'b_1' ) ;
250+ } ) ;
252251
253252 // Change a file
254253 await writeFile ( iff . join ( 'src/b.module.css' ) , '.b_2 {' ) ;
255- await waitForWatcherEmitAndReportDiagnostics ( ) ;
256- expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 2 ) ;
257- expect ( await iff . readFile ( 'generated/src/b.module.css.d.ts' ) ) . contain ( 'b_2' ) ;
254+ await vi . waitFor ( async ( ) => {
255+ // Wait for watcher to emit and report diagnostics
256+ expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 2 ) ;
257+ expect ( await iff . readFile ( 'generated/src/b.module.css.d.ts' ) ) . contain ( 'b_2' ) ;
258+ } ) ;
258259
259260 // Remove a file
260261 await rm ( iff . join ( 'src/a.module.css' ) ) ;
261- await waitForWatcherEmitAndReportDiagnostics ( ) ;
262- expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 3 ) ;
263- await expect ( access ( iff . join ( 'generated/src/a.module.css.d.ts' ) ) ) . resolves . not . toThrow ( ) ;
262+ await vi . waitFor ( async ( ) => {
263+ // Wait for watcher to emit and report diagnostics
264+ expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 3 ) ;
265+ await expect ( access ( iff . join ( 'generated/src/a.module.css.d.ts' ) ) ) . resolves . not . toThrow ( ) ;
266+ } ) ;
264267 } ) ;
265268 test ( 'batches rapid file changes' , async ( ) => {
266269 const iff = await createIFF ( {
@@ -279,9 +282,11 @@ describe('runCMKInWatchMode', () => {
279282 writeFile ( iff . join ( 'src/b.module.css' ) , '.b_1 {' ) ,
280283 writeFile ( iff . join ( 'src/c.module.css' ) , '.c_1 {' ) ,
281284 ] ) ;
282- await waitForWatcherEmitAndReportDiagnostics ( ) ;
283- expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 1 ) ;
284- // Diagnostics for three files are reported at once.
285- expect ( formatDiagnostics ( loggerSpy . logDiagnostics . mock . calls [ 0 ] ! [ 0 ] , iff . rootDir ) ) . length ( 3 ) ;
285+ await vi . waitFor ( ( ) => {
286+ // Wait for watcher to emit and report diagnostics
287+ expect ( loggerSpy . logDiagnostics ) . toHaveBeenCalledTimes ( 1 ) ;
288+ // Diagnostics for three files are reported at once.
289+ expect ( formatDiagnostics ( loggerSpy . logDiagnostics . mock . calls [ 0 ] ! [ 0 ] , iff . rootDir ) ) . length ( 3 ) ;
290+ } ) ;
286291 } ) ;
287292} ) ;
0 commit comments