forked from testdouble/testdouble.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
334 lines (290 loc) · 7.93 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
export as namespace testdouble;
//
// types and interfaces
// ----------------------------------------------------------------------------
export type DoubledObject<T> = T;
export type DoubledObjectWithKey<T extends string> = { [K in T]: any };
export type TestDouble<T> = T;
export type TestDoubleConstructor<T> = Constructor<T>;
interface Call {
context: {};
args: any[];
}
interface Constructor<T> {
new (...args: any[]): T;
}
export interface Captor {
capture(): any;
value?: any;
values?: any[];
}
export interface Explanation {
callCount: number;
calls: Call[];
description: string;
isTestDouble: boolean;
}
export interface MatcherConfig {
matches(matcherArgs: any[], actual: any): boolean;
name?: string | ((matcherArgs: any[]) => string);
onCreate?(matcherInstance: any, matcherArgs: any[]): void;
afterSatisfaction?(matcherArgs: any[], actual: any): void;
}
export interface Matchers {
anything(): any;
isA(type: Function): any;
contains(a: string | any[] | {}): any;
argThat(matcher: Function): any;
not(v: any): any;
captor(): Captor;
create(config: MatcherConfig): any;
}
export const matchers: Matchers;
export interface Stubber<D, R = D extends object ? Partial<D> : D> {
thenReturn<T>(first: R, ...args: Array<R>): TestDouble<T>;
thenDo<T>(f: Function): TestDouble<T>;
thenThrow<T>(e: Error): TestDouble<T>;
thenResolve<T>(first: R, ...args: Array<R>): TestDouble<T>;
thenReject<T>(e: Error): TestDouble<T>;
thenCallback<T>(error: any, data: any): TestDouble<T>;
}
export interface PromiseStubber<P, R = P extends object ? Partial<P> : P> {
thenResolve<T>(first: R, ...args: Array<R>): TestDouble<T>;
thenDo<T>(f: Function): TestDouble<T>;
thenReject<T>(e: Error): TestDouble<T>;
}
export interface TestdoubleConfig {
promiseConstructor?: any;
ignoreWarnings?: boolean;
suppressErrors?: boolean;
}
export interface VerificationConfig {
ignoreExtraArgs?: boolean;
times?: number;
cloneArgs?: boolean;
}
export interface WhenConfig {
ignoreExtraArgs?: boolean;
times?: number;
cloneArgs?: boolean;
defer?: boolean;
delay?: number;
}
//
// general
// ----------------------------------------------------------------------------
/**
* Update the configuration. Configuration will perist through the lifetime of
* of the entire test. If you need to change a configuration property for a
* single test, you'll need to manage undoing the change yourself (e.g. in
* beforeEach and afterEach hooks).
*
* @export
* @param {TestdoubleConfig} config
*/
export function config(config: TestdoubleConfig): void;
/**
* Reset the state.
*
* @export
*/
export function reset(): void;
/**
* Takes a test double function as an argument and will describe the current
* configuration and state of the test double.
*
* @export
* @template T
* @param {TestDouble<T>} f
* @returns {Explanation}
*/
export function explain<T>(f: TestDouble<T>): Explanation;
//
// fake: constructors
// ----------------------------------------------------------------------------
/**
* Create a fake object constructor the given class.
*
* @export
* @template T
* @param {{ new (...args: any[]): T }} constructor
* @returns {DoubledObject<T>}
*/
export function constructor<T>(
constructor: Constructor<T>
): TestDoubleConstructor<T>;
//
// fake: instance objects
//
/**
* Construct an instance of a faked class.
*
* @export
* @template T
* @param {{ new (...args: any[]): T }} constructor
* @returns {DoubledObject<typeof T>}
*/
export function instance<T>(
constructor: Constructor<T>
): DoubledObject<T>;
//
// fake: functions
// ----------------------------------------------------------------------------
/**
* Create a fake function.
*
* @param {string} [name] Name of function for better messages.
* @returns {TestDouble<Function>}
*/
declare function functionDouble(name?: string): TestDouble<Function>;
/**
* Create a fake function. Typed when type is provided.
* @example td.func<MyType>();
* @template T
* @param {T} [name] Name of function to copy.
* @returns {TestDouble<T>}
*/
declare function functionDouble<T>(name?: T): TestDouble<T>;
export { functionDouble as function };
export { functionDouble as func };
//
// fake: objects
// ----------------------------------------------------------------------------
/**
* Create a fake object that is deep copy of the given object.
*
* @export
* @template T
* @param {{ new (...args: any[]): T }} constructor
* @returns {DoubledObject<T>}
*/
export function object<T>(constructor: Constructor<T>): DoubledObject<T>;
/**
* Create a fake object that has the given list of properties.
*
* @export
* @template Key
* @param {Key[]} props Array of properties.
* @returns {DoubledObjectWithKey<Key>}
*/
export function object<T extends string>(props: T[]): DoubledObjectWithKey<T>;
/**
* Create a fake empty object that is cast as the generic using a Proxy object.
*
* @export
* @template T
* @param {T} object Name of object.
* @returns {DoubledObject<T>}
*/
export function object<T>(object: string): DoubledObject<T>;
/**
* Create a fake empty object using a Proxy object that is cast as the generic of a passed interface.
*
* @export
* @template T
* @returns {DoubledObject<T>}
*/
export function object<T>(): DoubledObject<T>;
/**
* Create a fake object that is deep copy of the given object.
*
* @export
* @template T
* @param {T} object Object to copy.
* @returns {DoubledObject<T>}
*/
export function object<T>(object: T): DoubledObject<T>;
// fake: imitations
/**
* Create a fake object constructor for the given class.
*
* @export
* @template T
* @param {{ new (...args: any[]): T }} constructor
* @param {string} [name]
* @returns {TestDoubleConstructor<T>}
*/
export function imitate<T>(
constructor: Constructor<T>,
name?: string
): TestDoubleConstructor<T>;
/**
* Create a fake object or function.
*
* @export
* @template T
* @param {T} original
* @param {string} [name]
* @returns {TestDouble<T>}
*/
export function imitate<T>(original: T, name?: string): TestDouble<T>;
//
// stubbing
// ----------------------------------------------------------------------------
/**
* Callback marker.
*
* @export
* @param {...any[]} args
*/
export function callback(...args: any[]): void;
/**
* Swap out real dependencies with fake one. Intercept calls to `require`
* that dependency module and ensure your subject is handed a fake instead.
*
* @export
* @param {string} path
* @param {*} [f]
* @returns {*}
*/
export function replace(path: string, f?: any): any;
/**
* Swap out real dependencies with fake one. Intercept calls to `require`
* that dependency module and ensure your subject is handed a fake instead.
*
* @export
* @param {string} path
* @param {*} [f]
* @returns {*}
*/
export function replaceCjs(path: string, f?: any): any;
/**
* Swap out real dependencies with fake one. Intercept calls to `import`
* that dependency module and ensure your subject is handed a fake instead.
*
* @export
* @param {string} path
* @param {*} [namedExportStubs]
* @param {*} [defaultExportStub]
* @returns {Promise<{default?: any, [namedExport: string]: any}>}
*/
export function replaceEsm(path: string, namedExportStubs?: any, defaultExportStub?: any):
Promise<{default?: any, [namedExport: string]: any}>;
/**
* Swap out real dependencies with fake one. Reference to the property will
* be replace it during your test.
*
* @export
* @param {{}} path
* @param {string} property
* @param {*} [f]
* @returns {*}
*/
export function replace(path: {}, property: string, f?: any): any;
/**
* Stub a specific function call.
*
* @export
* @param {...any[]} args
* @returns {Stubber}
*/
export function when<P>(f: Promise<P>, config?: WhenConfig): PromiseStubber<P>;
export function when<D>(f: D, config?: WhenConfig): Stubber<D>;
/**
* Verify a specific function call to a stubbed function.
*
* @export
* @param {...any[]} args
* @returns {Stubber}
*/
export function verify(a: any, check?: VerificationConfig): void;