2
2
// types and interfaces
3
3
// ----------------------------------------------------------------------------
4
4
5
- export type DoubledObject < T > = T
5
+ export type DoubledObject < T > = T ;
6
6
7
- export type DoubledObjectWithKey < T extends string > = { [ K in T ] : any }
7
+ export type DoubledObjectWithKey < T extends string > = { [ K in T ] : any } ;
8
8
9
- export type TestDouble < T > = T
9
+ export type TestDouble < T > = T ;
10
10
11
- export type TestDoubleConstructor < T > = Constructor < T >
11
+ export type TestDoubleConstructor < T > = Constructor < T > ;
12
12
13
13
interface Call {
14
14
context : { } ;
15
15
args : any [ ] ;
16
16
}
17
17
18
18
interface Constructor < T > {
19
- new ( ...args : any [ ] ) : T
19
+ new ( ...args : any [ ] ) : T ;
20
20
}
21
21
22
22
export interface Captor {
@@ -49,15 +49,21 @@ export interface Matchers {
49
49
create ( config : MatcherConfig ) : any ;
50
50
}
51
51
52
- export const matchers : Matchers
52
+ export const matchers : Matchers ;
53
53
54
- export interface Stubber {
55
- thenReturn < T > ( ...args : any [ ] ) : TestDouble < T > ;
54
+ export interface Stubber < D , R = D extends object ? Partial < D > : D > {
55
+ thenReturn < T > ( first : R , ...args : Array < R > ) : TestDouble < T > ;
56
56
thenDo < T > ( f : Function ) : TestDouble < T > ;
57
57
thenThrow < T > ( e : Error ) : TestDouble < T > ;
58
- thenResolve < T > ( ...args : any [ ] ) : TestDouble < T > ;
58
+ thenResolve < T > ( first : R , ...args : Array < R > ) : TestDouble < T > ;
59
+ thenReject < T > ( e : Error ) : TestDouble < T > ;
60
+ thenCallback < T > ( error : any , data : any ) : TestDouble < T > ;
61
+ }
62
+
63
+ export interface PromiseStubber < P , R = P extends object ? Partial < P > : P > {
64
+ thenResolve < T > ( first : R , ...args : Array < R > ) : TestDouble < T > ;
65
+ thenDo < T > ( f : Function ) : TestDouble < T > ;
59
66
thenReject < T > ( e : Error ) : TestDouble < T > ;
60
- thenCallback < T > ( ...args : any [ ] ) : TestDouble < T > ;
61
67
}
62
68
63
69
export interface TestdoubleConfig {
@@ -72,6 +78,14 @@ export interface VerificationConfig {
72
78
cloneArgs ?: boolean ;
73
79
}
74
80
81
+ export interface WhenConfig {
82
+ ignoreExtraArgs ?: boolean ;
83
+ times ?: number ;
84
+ cloneArgs ?: boolean ;
85
+ defer ?: boolean ;
86
+ delay ?: number ;
87
+ }
88
+
75
89
//
76
90
// general
77
91
// ----------------------------------------------------------------------------
@@ -117,7 +131,9 @@ export function explain<T>(f: TestDouble<T>): Explanation;
117
131
* @param {{ new (...args: any[]): T } } constructor
118
132
* @returns {DoubledObject<T> }
119
133
*/
120
- export function constructor < T > ( constructor : Constructor < T > ) : TestDoubleConstructor < T > ;
134
+ export function constructor < T > (
135
+ constructor : Constructor < T >
136
+ ) : TestDoubleConstructor < T > ;
121
137
122
138
//
123
139
// fake: functions
@@ -140,8 +156,8 @@ declare function functionDouble(name?: string): TestDouble<Function>;
140
156
*/
141
157
declare function functionDouble < T > ( name ?: T ) : TestDouble < T > ;
142
158
143
- export { functionDouble as function }
144
- export { functionDouble as func }
159
+ export { functionDouble as function } ;
160
+ export { functionDouble as func } ;
145
161
146
162
//
147
163
// fake: objects
@@ -207,7 +223,10 @@ export function object<T>(object: T): DoubledObject<T>;
207
223
* @param {string } [name]
208
224
* @returns {TestDoubleConstructor<T> }
209
225
*/
210
- export function imitate < T > ( constructor : Constructor < T > , name ?: string ) : TestDoubleConstructor < T > ;
226
+ export function imitate < T > (
227
+ constructor : Constructor < T > ,
228
+ name ?: string
229
+ ) : TestDoubleConstructor < T > ;
211
230
212
231
/**
213
232
* Create a fake object or function.
@@ -262,8 +281,8 @@ export function replace(path: {}, property: string, f?: any): any;
262
281
* @param {...any[] } args
263
282
* @returns {Stubber }
264
283
*/
265
- export function when ( ... args : any [ ] ) : Stubber ;
266
-
284
+ export function when < P > ( f : Promise < P > , config ?: WhenConfig ) : PromiseStubber < P > ;
285
+ export function when < D > ( f : D , config ?: WhenConfig ) : Stubber < D > ;
267
286
/**
268
287
* Verify a specific function call to a stubbed function.
269
288
*
0 commit comments