@@ -170,44 +170,52 @@ describe('Watch Service Integration', () => {
170170 } ) ;
171171
172172 describe ( 'handleFileChange() - Callbacks' , ( ) => {
173- it ( 'should call onChange callback when file changes' , ( done ) => {
173+ it ( 'should call onChange callback when file changes' , async ( ) => {
174174 const testFile = path . join ( tmpDir , 'test.txt' ) ;
175175 fs . writeFileSync ( testFile , 'Hello' ) ;
176176
177- const onChangeMock = jest . fn ( ( filePath : string ) => {
178- expect ( filePath ) . toBe ( testFile ) ;
179- done ( ) ;
180- } ) ;
177+ const callbackPromise = new Promise < void > ( ( resolve ) => {
178+ const onChangeMock = jest . fn ( ( filePath : string ) => {
179+ expect ( filePath ) . toBe ( testFile ) ;
180+ resolve ( ) ;
181+ } ) ;
181182
182- watchService . watch ( testFile , {
183- targetLangs : [ 'es' ] ,
184- outputDir : tmpDir ,
185- onChange : onChangeMock ,
183+ watchService . watch ( testFile , {
184+ targetLangs : [ 'es' ] ,
185+ outputDir : tmpDir ,
186+ onChange : onChangeMock ,
187+ } ) ;
188+
189+ watchService . handleFileChange ( testFile ) ;
186190 } ) ;
187191
188- watchService . handleFileChange ( testFile ) ;
192+ await callbackPromise ;
189193 } ) ;
190194
191- it ( 'should call onError callback when translation fails' , ( done ) => {
195+ it ( 'should call onError callback when translation fails' , async ( ) => {
192196 const testFile = path . join ( tmpDir , 'test.txt' ) ;
193197 fs . writeFileSync ( testFile , 'Hello' ) ;
194198
195199 // Mock translation service to throw error
196200 jest . spyOn ( fileTranslationService , 'translateFile' ) . mockRejectedValue ( new Error ( 'API error' ) ) ;
197201
198- const onErrorMock = jest . fn ( ( filePath : string , error : Error ) => {
199- expect ( filePath ) . toBe ( testFile ) ;
200- expect ( error . message ) . toContain ( 'API error' ) ;
201- done ( ) ;
202- } ) ;
202+ const callbackPromise = new Promise < void > ( ( resolve ) => {
203+ const onErrorMock = jest . fn ( ( filePath : string , error : Error ) => {
204+ expect ( filePath ) . toBe ( testFile ) ;
205+ expect ( error . message ) . toContain ( 'API error' ) ;
206+ resolve ( ) ;
207+ } ) ;
203208
204- watchService . watch ( testFile , {
205- targetLangs : [ 'es' ] ,
206- outputDir : tmpDir ,
207- onError : onErrorMock ,
209+ watchService . watch ( testFile , {
210+ targetLangs : [ 'es' ] ,
211+ outputDir : tmpDir ,
212+ onError : onErrorMock ,
213+ } ) ;
214+
215+ watchService . handleFileChange ( testFile ) ;
208216 } ) ;
209217
210- watchService . handleFileChange ( testFile ) ;
218+ await callbackPromise ;
211219 } ) ;
212220 } ) ;
213221
@@ -234,23 +242,27 @@ describe('Watch Service Integration', () => {
234242 expect ( stats . isWatching ) . toBe ( true ) ;
235243 } ) ;
236244
237- it ( 'should increment error count on translation failure' , ( done ) => {
245+ it ( 'should increment error count on translation failure' , async ( ) => {
238246 const testFile = path . join ( tmpDir , 'test.txt' ) ;
239247 fs . writeFileSync ( testFile , 'Hello' ) ;
240248
241249 jest . spyOn ( fileTranslationService , 'translateFile' ) . mockRejectedValue ( new Error ( 'Fail' ) ) ;
242250
243- watchService . watch ( testFile , {
244- targetLangs : [ 'es' ] ,
245- outputDir : tmpDir ,
246- onError : ( ) => {
247- const stats = watchService . getStats ( ) ;
248- expect ( stats . errorsCount ) . toBeGreaterThan ( 0 ) ;
249- done ( ) ;
250- } ,
251+ const callbackPromise = new Promise < void > ( ( resolve ) => {
252+ watchService . watch ( testFile , {
253+ targetLangs : [ 'es' ] ,
254+ outputDir : tmpDir ,
255+ onError : ( ) => {
256+ const stats = watchService . getStats ( ) ;
257+ expect ( stats . errorsCount ) . toBeGreaterThan ( 0 ) ;
258+ resolve ( ) ;
259+ } ,
260+ } ) ;
261+
262+ watchService . handleFileChange ( testFile ) ;
251263 } ) ;
252264
253- watchService . handleFileChange ( testFile ) ;
265+ await callbackPromise ;
254266 } ) ;
255267 } ) ;
256268
@@ -355,7 +367,7 @@ describe('Watch Service Integration', () => {
355367 } ) ;
356368
357369 describe ( 'Debouncing' , ( ) => {
358- it ( 'should debounce rapid file changes' , ( done ) => {
370+ it ( 'should debounce rapid file changes' , async ( ) => {
359371 const testFile = path . join ( tmpDir , 'test.txt' ) ;
360372 fs . writeFileSync ( testFile , 'Hello' ) ;
361373
@@ -376,10 +388,8 @@ describe('Watch Service Integration', () => {
376388 watchService . handleFileChange ( testFile ) ;
377389
378390 // onChange should be called 3 times (once per trigger)
379- setTimeout ( ( ) => {
380- expect ( changeCount ) . toBe ( 3 ) ;
381- done ( ) ;
382- } , 100 ) ;
391+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
392+ expect ( changeCount ) . toBe ( 3 ) ;
383393 } ) ;
384394 } ) ;
385395} ) ;
0 commit comments