@@ -213,6 +213,13 @@ function enhanceSpy<TArgs extends any[], TReturns>(
213213 let onceImplementations : ( ( ...args : TArgs ) => TReturns ) [ ] = [ ]
214214 let implementationChangedTemporarily = false
215215
216+ function mockCall ( this : unknown , ...args : any ) {
217+ instances . push ( this )
218+ invocations . push ( ++ callOrder )
219+ const impl = implementationChangedTemporarily ? implementation ! : ( onceImplementations . shift ( ) || implementation || state . getOriginal ( ) || ( ( ) => { } ) )
220+ return impl . apply ( this , args )
221+ }
222+
216223 let name : string = ( stub as any ) . name
217224
218225 stub . getMockName = ( ) => name || 'vi.fn()'
@@ -237,13 +244,15 @@ function enhanceSpy<TArgs extends any[], TReturns>(
237244
238245 stub . mockRestore = ( ) => {
239246 stub . mockReset ( )
247+ state . restore ( )
240248 implementation = undefined
241249 return stub
242250 }
243251
244252 stub . getMockImplementation = ( ) => implementation
245253 stub . mockImplementation = ( fn : ( ...args : TArgs ) => TReturns ) => {
246254 implementation = fn
255+ state . willCall ( mockCall )
247256 return stub
248257 }
249258
@@ -258,6 +267,7 @@ function enhanceSpy<TArgs extends any[], TReturns>(
258267 const originalImplementation = implementation
259268
260269 implementation = fn
270+ state . willCall ( mockCall )
261271 implementationChangedTemporarily = true
262272
263273 const reset = ( ) => {
@@ -305,12 +315,7 @@ function enhanceSpy<TArgs extends any[], TReturns>(
305315 get : ( ) => mockContext ,
306316 } )
307317
308- state . willCall ( function ( this : unknown , ...args ) {
309- instances . push ( this )
310- invocations . push ( ++ callOrder )
311- const impl = implementationChangedTemporarily ? implementation ! : ( onceImplementations . shift ( ) || implementation || state . getOriginal ( ) || ( ( ) => { } ) )
312- return impl . apply ( this , args )
313- } )
318+ state . willCall ( mockCall )
314319
315320 spies . add ( stub )
316321
0 commit comments