1 | export as namespace testdouble;
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export type DoubledObject<T> = T;
|
8 |
|
9 | export type DoubledObjectWithKey<T extends string> = { [K in T]: any };
|
10 |
|
11 | export type TestDouble<T> = T;
|
12 |
|
13 | export type TestDoubleConstructor<T> = Constructor<T>;
|
14 |
|
15 | interface Call {
|
16 | context: {};
|
17 | args: any[];
|
18 | }
|
19 |
|
20 | interface Constructor<T> {
|
21 | new (...args: any[]): T;
|
22 | }
|
23 |
|
24 | export interface Captor {
|
25 | capture(): any;
|
26 | value?: any;
|
27 | values?: any[];
|
28 | }
|
29 |
|
30 | export interface Explanation {
|
31 | callCount: number;
|
32 | calls: Call[];
|
33 | description: string;
|
34 | isTestDouble: boolean;
|
35 | }
|
36 |
|
37 | export 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 |
|
44 | export 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 |
|
54 | export const matchers: Matchers;
|
55 |
|
56 | export 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>(first: unknown, ...args: Array<unknown>): TestDouble<T>;
|
60 | thenResolve<T>(first: R, ...args: Array<R>): TestDouble<T>;
|
61 | thenReject<T>(first: unknown, ...args: Array<unknown>): TestDouble<T>;
|
62 | thenCallback<T>(error: any, data: any): TestDouble<T>;
|
63 | }
|
64 |
|
65 | export 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>(first: unknown, ...args: Array<unknown>): TestDouble<T>;
|
70 | }
|
71 |
|
72 | export interface TestdoubleConfig {
|
73 | promiseConstructor?: any;
|
74 | ignoreWarnings?: boolean;
|
75 | suppressErrors?: boolean;
|
76 | }
|
77 |
|
78 | export interface VerificationConfig {
|
79 | ignoreExtraArgs?: boolean;
|
80 | times?: number;
|
81 | cloneArgs?: boolean;
|
82 | }
|
83 |
|
84 | export 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 | */
|
105 | export function config(config: TestdoubleConfig): void;
|
106 |
|
107 | /**
|
108 | * Reset the state.
|
109 | *
|
110 | * @export
|
111 | */
|
112 | export 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 | */
|
123 | export 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 | */
|
137 | export 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 | */
|
153 | export 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 | */
|
168 | declare 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 | */
|
177 | declare function functionDouble<T>(name?: T): TestDouble<T>;
|
178 |
|
179 | export { functionDouble as function };
|
180 | export { 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 | */
|
194 | export 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 | */
|
204 | export 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 | */
|
214 | export 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 | */
|
223 | export 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 | */
|
233 | export 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 | */
|
246 | export 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 | */
|
260 | export 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 | */
|
272 | export 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} module
|
280 | * @param {*} [f] replacement
|
281 | * @returns {*}
|
282 | */
|
283 | export function replace<F>(path: string, f?: F): F;
|
284 | export function replace(path: string): TestDouble<any>;
|
285 | /**
|
286 | * Swap out real dependencies with fake one. Intercept calls to `require`
|
287 | * that dependency module and ensure your subject is handed a fake instead.
|
288 | *
|
289 | * @export
|
290 | * @param {string} obj
|
291 | * @param {*} [f]
|
292 | * @returns {*}
|
293 | */
|
294 |
|
295 | export function replace<T, K extends keyof T>(obj: T, property: K): TestDouble<T[K]>;
|
296 | export function replace(obj: {}, property: string): TestDouble<any>;
|
297 | /**
|
298 | * Swap out real dependencies with fake one. Intercept calls to `import`
|
299 | * that dependency module and ensure your subject is handed a fake instead.
|
300 | *
|
301 | * @export
|
302 | * @param {string} path
|
303 | * @param {*} [namedExportStubs]
|
304 | * @param {*} [defaultExportStub]
|
305 | * @returns {Promise<{default?: any, [namedExport: string]: any}>}
|
306 | */
|
307 | export function replaceEsm(path: string): Promise<{default?: any, [namedExport: string]: any}>;
|
308 | export function replaceEsm(path: string, namedExportStubs?: any, defaultExportStub?: any): Promise<void>;
|
309 |
|
310 | /**
|
311 | * Swap out real dependencies with fake one. Reference to the property will
|
312 | * be replace it during your test.
|
313 | *
|
314 | * @export
|
315 | * @param {{}} obj
|
316 | * @param {string} property
|
317 | * @param {*} [f]
|
318 | * @returns {*}
|
319 | */
|
320 | export function replace<F>(obj: {}, property: string, replacement: F): F;
|
321 | /**
|
322 | * Stub a specific function call.
|
323 | *
|
324 | * @export
|
325 | * @param {...any[]} args
|
326 | * @returns {Stubber}
|
327 | */
|
328 | export function when<P>(f: Promise<P>, config?: WhenConfig): PromiseStubber<P>;
|
329 | export function when<D>(f: D, config?: WhenConfig): Stubber<D>;
|
330 | /**
|
331 | * Verify a specific function call to a stubbed function.
|
332 | *
|
333 | * @export
|
334 | * @param {...any[]} args
|
335 | * @returns {Stubber}
|
336 | */
|
337 | export function verify(a: any, check?: VerificationConfig): void;
|
338 |
|
339 | export const version: string;
|
340 |
|
\ | No newline at end of file |