UNPKG

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