@@ -19,15 +19,20 @@ describe('FileWatcher', () => {
1919 const debounceMs = 400 ;
2020 let callCount = 0 ;
2121
22+ let resolveReady ! : ( ) => void ;
23+ const ready = new Promise < void > ( ( resolve ) => {
24+ resolveReady = resolve ;
25+ } ) ;
26+
2227 const stop = startFileWatcher ( {
2328 rootPath : tempDir ,
2429 debounceMs,
30+ onReady : ( ) => resolveReady ( ) ,
2531 onChanged : ( ) => { callCount ++ ; } ,
2632 } ) ;
2733
2834 try {
29- // Give chokidar a moment to finish initializing before the first write
30- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
35+ await ready ;
3136 await fs . writeFile ( path . join ( tempDir , 'test.ts' ) , 'export const x = 1;' ) ;
3237 // Wait for chokidar to pick up the event (including awaitWriteFinish stabilityThreshold)
3338 // + debounce window + OS scheduling slack
@@ -39,25 +44,31 @@ describe('FileWatcher', () => {
3944 } , 8000 ) ;
4045
4146 it ( 'debounces rapid changes into a single callback' , async ( ) => {
42- const debounceMs = 300 ;
47+ const debounceMs = 800 ;
4348 let callCount = 0 ;
4449
50+ let resolveReady ! : ( ) => void ;
51+ const ready = new Promise < void > ( ( resolve ) => {
52+ resolveReady = resolve ;
53+ } ) ;
54+
4555 const stop = startFileWatcher ( {
4656 rootPath : tempDir ,
4757 debounceMs,
58+ onReady : ( ) => resolveReady ( ) ,
4859 onChanged : ( ) => { callCount ++ ; } ,
4960 } ) ;
5061
5162 try {
5263 // Give chokidar a moment to finish initializing before the first write
53- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
64+ await ready ;
5465 // Write 5 files in quick succession — all within the debounce window
5566 for ( let i = 0 ; i < 5 ; i ++ ) {
5667 await fs . writeFile ( path . join ( tempDir , `file${ i } .ts` ) , `export const x${ i } = ${ i } ;` ) ;
57- await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
68+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) ) ;
5869 }
5970 // Wait for debounce to settle
60- await new Promise ( ( resolve ) => setTimeout ( resolve , debounceMs + 400 ) ) ;
71+ await new Promise ( ( resolve ) => setTimeout ( resolve , debounceMs + 1200 ) ) ;
6172 expect ( callCount ) . toBe ( 1 ) ;
6273 } finally {
6374 stop ( ) ;
@@ -68,14 +79,20 @@ describe('FileWatcher', () => {
6879 const debounceMs = 500 ;
6980 let callCount = 0 ;
7081
82+ let resolveReady ! : ( ) => void ;
83+ const ready = new Promise < void > ( ( resolve ) => {
84+ resolveReady = resolve ;
85+ } ) ;
86+
7187 const stop = startFileWatcher ( {
7288 rootPath : tempDir ,
7389 debounceMs,
90+ onReady : ( ) => resolveReady ( ) ,
7491 onChanged : ( ) => { callCount ++ ; } ,
7592 } ) ;
7693
7794 // Give chokidar a moment to finish initializing before the first write
78- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
95+ await ready ;
7996 await fs . writeFile ( path . join ( tempDir , 'cancel.ts' ) , 'export const y = 99;' ) ;
8097 // Let chokidar detect the event (including awaitWriteFinish stabilityThreshold)
8198 // but stop before the debounce window expires.
@@ -90,16 +107,22 @@ describe('FileWatcher', () => {
90107 const debounceMs = 250 ;
91108 let callCount = 0 ;
92109
110+ let resolveReady ! : ( ) => void ;
111+ const ready = new Promise < void > ( ( resolve ) => {
112+ resolveReady = resolve ;
113+ } ) ;
114+
93115 const stop = startFileWatcher ( {
94116 rootPath : tempDir ,
95117 debounceMs,
118+ onReady : ( ) => resolveReady ( ) ,
96119 onChanged : ( ) => {
97120 callCount ++ ;
98121 }
99122 } ) ;
100123
101124 try {
102- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
125+ await ready ;
103126 await fs . writeFile ( path . join ( tempDir , 'notes.txt' ) , 'this should be ignored' ) ;
104127 await new Promise ( ( resolve ) => setTimeout ( resolve , debounceMs + 700 ) ) ;
105128 expect ( callCount ) . toBe ( 0 ) ;
@@ -112,16 +135,22 @@ describe('FileWatcher', () => {
112135 const debounceMs = 250 ;
113136 let callCount = 0 ;
114137
138+ let resolveReady ! : ( ) => void ;
139+ const ready = new Promise < void > ( ( resolve ) => {
140+ resolveReady = resolve ;
141+ } ) ;
142+
115143 const stop = startFileWatcher ( {
116144 rootPath : tempDir ,
117145 debounceMs,
146+ onReady : ( ) => resolveReady ( ) ,
118147 onChanged : ( ) => {
119148 callCount ++ ;
120149 }
121150 } ) ;
122151
123152 try {
124- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
153+ await ready ;
125154 await fs . writeFile ( path . join ( tempDir , '.gitignore' ) , 'dist/\n' ) ;
126155 await new Promise ( ( resolve ) => setTimeout ( resolve , debounceMs + 700 ) ) ;
127156 expect ( callCount ) . toBe ( 1 ) ;
0 commit comments