UNPKG

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