@@ -85,8 +85,13 @@ describe('useFunctionWatcher', () => {
8585 appWatcher : new AppEventWatcher ( APP ) ,
8686 } ) ,
8787 )
88- // needed to await the render
89- await vi . advanceTimersByTimeAsync ( 0 )
88+
89+ // Wait for the async initialReplay() to complete
90+ // The hook uses a promise chain that needs multiple ticks to resolve
91+ await vi . advanceTimersByTimeAsync ( 10 )
92+
93+ // Also flush any pending microtasks
94+ await vi . runOnlyPendingTimersAsync ( )
9095
9196 // Then
9297 expect ( runFunction ) . toHaveBeenCalledOnce ( )
@@ -112,14 +117,16 @@ describe('useFunctionWatcher', () => {
112117 } ) ,
113118 )
114119
115- // needed to await the render
116- await vi . advanceTimersByTimeAsync ( 0 )
120+ // Wait for the async initialReplay() to complete
121+ await vi . advanceTimersByTimeAsync ( 10 )
122+ await vi . runOnlyPendingTimersAsync ( )
117123
118124 expect ( hook . lastResult ?. recentFunctionRuns [ 0 ] ) . toEqual ( { ...EXEC_RESPONSE , type : 'functionRun' } )
119125 expect ( hook . lastResult ?. recentFunctionRuns [ 1 ] ) . toEqual ( { ...EXEC_RESPONSE , type : 'functionRun' } )
120126
121127 appWatcher . emit ( 'all' , event )
122- await vi . advanceTimersByTimeAsync ( 0 )
128+ await vi . advanceTimersByTimeAsync ( 10 )
129+ await vi . runOnlyPendingTimersAsync ( )
123130
124131 expect ( hook . lastResult ?. recentFunctionRuns [ 0 ] ) . toEqual ( { ...SECOND_EXEC_RESPONSE , type : 'functionRun' } )
125132 expect ( hook . lastResult ?. recentFunctionRuns [ 1 ] ) . toEqual ( { ...EXEC_RESPONSE , type : 'functionRun' } )
@@ -150,11 +157,13 @@ describe('useFunctionWatcher', () => {
150157 } ) ,
151158 )
152159
153- // needed to await the render
154- await vi . advanceTimersByTimeAsync ( 0 )
160+ // Wait for the async initialReplay() to complete
161+ await vi . advanceTimersByTimeAsync ( 10 )
162+ await vi . runOnlyPendingTimersAsync ( )
155163
156164 appWatcher . emit ( 'all' , event )
157- await vi . advanceTimersByTimeAsync ( 0 )
165+ await vi . advanceTimersByTimeAsync ( 10 )
166+ await vi . runOnlyPendingTimersAsync ( )
158167
159168 // Then
160169 expect ( runFunction ) . toHaveBeenCalledOnce ( )
0 commit comments