@@ -213,6 +213,13 @@ function enhanceSpy<TArgs extends any[], TReturns>(
213
213
let onceImplementations : ( ( ...args : TArgs ) => TReturns ) [ ] = [ ]
214
214
let implementationChangedTemporarily = false
215
215
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
+
216
223
let name : string = ( stub as any ) . name
217
224
218
225
stub . getMockName = ( ) => name || 'vi.fn()'
@@ -237,13 +244,15 @@ function enhanceSpy<TArgs extends any[], TReturns>(
237
244
238
245
stub . mockRestore = ( ) => {
239
246
stub . mockReset ( )
247
+ state . restore ( )
240
248
implementation = undefined
241
249
return stub
242
250
}
243
251
244
252
stub . getMockImplementation = ( ) => implementation
245
253
stub . mockImplementation = ( fn : ( ...args : TArgs ) => TReturns ) => {
246
254
implementation = fn
255
+ state . willCall ( mockCall )
247
256
return stub
248
257
}
249
258
@@ -258,6 +267,7 @@ function enhanceSpy<TArgs extends any[], TReturns>(
258
267
const originalImplementation = implementation
259
268
260
269
implementation = fn
270
+ state . willCall ( mockCall )
261
271
implementationChangedTemporarily = true
262
272
263
273
const reset = ( ) => {
@@ -305,12 +315,7 @@ function enhanceSpy<TArgs extends any[], TReturns>(
305
315
get : ( ) => mockContext ,
306
316
} )
307
317
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 )
314
319
315
320
spies . add ( stub )
316
321
0 commit comments