@@ -13,8 +13,12 @@ function isJestAsymmetricMatcher(obj: any): obj is JestAsymmetricMatcher {
13
13
return ! ! obj && typeof obj === 'object' && 'asymmetricMatch' in obj && typeof obj . asymmetricMatch === 'function' ;
14
14
}
15
15
16
- const checkCalledWith = < T , Y extends any [ ] > ( calledWithStack : CalledWithStackItem < T , Y > [ ] , actualArgs : Y ) : T => {
17
- const calledWithInstance = calledWithStack . find ( instance =>
16
+ const checkCalledWith = < T , Y extends any [ ] > (
17
+ calledWithStack : CalledWithStackItem < T , Y > [ ] ,
18
+ actualArgs : Y ,
19
+ fallbackMockImplementation ?: ( ...args : Y ) => T
20
+ ) : T => {
21
+ const calledWithInstance = calledWithStack . find ( ( instance ) =>
18
22
instance . args . every ( ( matcher , i ) => {
19
23
if ( matcher instanceof Matcher ) {
20
24
return matcher . asymmetricMatch ( actualArgs [ i ] ) ;
@@ -29,7 +33,9 @@ const checkCalledWith = <T, Y extends any[]>(calledWithStack: CalledWithStackIte
29
33
) ;
30
34
31
35
// @ts -ignore cannot return undefined, but this will fail the test if there is an expectation which is what we want
32
- return calledWithInstance ? calledWithInstance . calledWithFn ( ...actualArgs ) : undefined ;
36
+ return calledWithInstance
37
+ ? calledWithInstance . calledWithFn ( ...actualArgs )
38
+ : fallbackMockImplementation && fallbackMockImplementation ( ...actualArgs ) ;
33
39
} ;
34
40
35
41
export const calledWithFn = < T , Y extends any [ ] > ( {
@@ -42,9 +48,10 @@ export const calledWithFn = <T, Y extends any[]>({
42
48
// We create new function to delegate any interactions (mockReturnValue etc.) to for this set of args.
43
49
// If that set of args is matched, we just call that jest.fn() for the result.
44
50
const calledWithFn = jest . fn ( fallbackMockImplementation ) ;
45
- if ( ! fn . getMockImplementation ( ) ) {
51
+ const mockImplementation = fn . getMockImplementation ( ) ;
52
+ if ( ! mockImplementation || mockImplementation === fallbackMockImplementation ) {
46
53
// Our original function gets a mock implementation which handles the matching
47
- fn . mockImplementation ( ( ...args : Y ) => checkCalledWith ( calledWithStack , args ) ) ;
54
+ fn . mockImplementation ( ( ...args : Y ) => checkCalledWith ( calledWithStack , args , fallbackMockImplementation ) ) ;
48
55
calledWithStack = [ ] ;
49
56
}
50
57
calledWithStack . unshift ( { args, calledWithFn } ) ;
0 commit comments