File tree 4 files changed +33
-7
lines changed
4 files changed +33
-7
lines changed Original file line number Diff line number Diff line change 1
- import type { Mock } from 'vitest'
2
1
import {
3
2
createBehaviorStack ,
4
3
type BehaviorStack ,
5
4
BehaviorType ,
6
5
} from './behaviors.ts'
7
6
import { NotAMockFunctionError } from './errors.ts'
8
- import type { AnyFunction } from './types.ts'
7
+ import type { AnyFunction , MockInstance } from './types.ts'
9
8
10
9
const BEHAVIORS_KEY = Symbol ( 'behaviors' )
11
10
@@ -63,7 +62,7 @@ export const configureStub = <TFunc extends AnyFunction>(
63
62
return behaviors
64
63
}
65
64
66
- export const validateSpy = ( maybeSpy : unknown ) : Mock => {
65
+ export const validateSpy = ( maybeSpy : unknown ) : MockInstance => {
67
66
if (
68
67
typeof maybeSpy === 'function' &&
69
68
'mockImplementation' in maybeSpy &&
@@ -73,14 +72,14 @@ export const validateSpy = (maybeSpy: unknown): Mock => {
73
72
'getMockName' in maybeSpy &&
74
73
typeof maybeSpy . getMockName === 'function'
75
74
) {
76
- return maybeSpy as Mock
75
+ return maybeSpy as unknown as MockInstance
77
76
}
78
77
79
78
throw new NotAMockFunctionError ( maybeSpy )
80
79
}
81
80
82
81
export const getBehaviorStack = < TFunc extends AnyFunction > (
83
- spy : Mock ,
82
+ spy : MockInstance ,
84
83
) : BehaviorStack < TFunc > | undefined => {
85
84
const existingImplementation = spy . getMockImplementation ( ) as
86
85
| WhenStubImplementation < TFunc >
Original file line number Diff line number Diff line change 2
2
3
3
/** Any function, for use in `extends` */
4
4
export type AnyFunction = ( ...args : never [ ] ) => unknown
5
+
6
+ /**
7
+ * Minimally typed version of Vitest's `MockInstance`.
8
+ *
9
+ * Used to ensure backwards compatibility
10
+ * with older versions of Vitest.
11
+ */
12
+ export interface MockInstance < TFunc extends AnyFunction = AnyFunction > {
13
+ getMockName ( ) : string
14
+ getMockImplementation ( ) : TFunc | undefined
15
+ mockImplementation : ( impl : TFunc ) => this
16
+ mock : MockContext < TFunc >
17
+ }
18
+
19
+ export interface MockContext < TFunc extends AnyFunction > {
20
+ calls : Parameters < TFunc > [ ]
21
+ }
Original file line number Diff line number Diff line change 1
1
import { configureStub } from './stubs.ts'
2
2
import type { WhenOptions } from './behaviors.ts'
3
- import type { AnyFunction } from './types.ts'
3
+ import type { AnyFunction , MockInstance } from './types.ts'
4
4
import { getDebug , type DebugResult } from './debug.ts'
5
5
6
6
export { type WhenOptions , type Behavior , BehaviorType } from './behaviors.ts'
@@ -22,7 +22,7 @@ export interface Stub<TArgs extends unknown[], TReturn> {
22
22
}
23
23
24
24
export const when = < TFunc extends AnyFunction > (
25
- spy : TFunc ,
25
+ spy : TFunc | MockInstance < TFunc > ,
26
26
options : WhenOptions = { } ,
27
27
) : StubWrapper < TFunc > => {
28
28
const behaviorStack = configureStub ( spy )
Original file line number Diff line number Diff line change @@ -23,6 +23,16 @@ describe('vitest-when type signatures', () => {
23
23
assertType < subject . Stub < [ number ] , any > > ( stub )
24
24
} )
25
25
26
+ it ( 'should handle an spied function' , ( ) => {
27
+ const target = { simple }
28
+ const spy = vi . spyOn ( target , 'simple' )
29
+ const stub = subject . when ( spy ) . calledWith ( 1 )
30
+
31
+ stub . thenReturn ( 'hello' )
32
+
33
+ assertType < subject . Stub < [ number ] , any > > ( stub )
34
+ } )
35
+
26
36
it ( 'should handle a simple function' , ( ) => {
27
37
const stub = subject . when ( simple ) . calledWith ( 1 )
28
38
You can’t perform that action at this time.
0 commit comments