UNPKG

19 kBTypeScriptView Raw
1// Type definitions for chai-spies 1.0.0
2// Project: https://github.com/chaijs/chai-spies
3// Definitions by: Ilya Kuznetsov <https://github.com/kuzn-ilya>
4// Harm van der Werf <https://github.com/harm-less>
5// Jouni Suorsa <https://github.com/jounisuo>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 3.0
8
9/// <reference types="chai" />
10
11declare namespace Chai {
12 interface ChaiStatic {
13 spy: ChaiSpies.Spy;
14 }
15
16 interface LanguageChains {
17 on: Assertion;
18 }
19
20 interface Assertion {
21 /**
22 * ####.spy
23 * Asserts that object is a spy.
24 * ```ts
25 * expect(spy).to.be.spy;
26 * spy.should.be.spy;
27 * ```
28 */
29 spy: Assertion;
30
31 /**
32 * ####.called
33 * Assert that a spy has been called. Negation passes through.
34 * ```ts
35 * expect(spy).to.have.been.called();
36 * spy.should.have.been.called();
37 * ```
38 * Note that ```called``` can be used as a chainable method.
39 */
40 called: ChaiSpies.Called;
41
42 /**
43 * * ####.been
44 * * Assert that something has been spied on. Negation passes through.
45 * * ```ts
46 * * expect(spy).to.have.been.called();
47 * * spy.should.have.been.called();
48 * ```
49 * Note that ```been``` can be used as a chainable method.
50 */
51 been: ChaiSpies.Been;
52
53 /**
54 * * ####.nth (function)
55 * * Assert that something has been spied on on a certain index. Negation passes through.
56 * * ```ts
57 * * expect(spy).on.nth(5).be.called.with('foobar');
58 * * spy.should.on.nth(5).be.called.with('foobar');
59 * ```
60 * Note that ```nth``` can be used as a chainable method.
61 */
62 nth(index: number): Assertion;
63 }
64}
65
66declare namespace ChaiSpies {
67 interface Sandbox {
68 /**
69 * #### chai.spy.on (function)
70 *
71 * Wraps an object method into spy. All calls will pass through to the original function.
72 *
73 * @param {Object} object
74 * @param {String} methodNames names to spy on
75 * @param {function} fn replacement function
76 * @returns function to actually call
77 */
78 on(object: Object, methodNames: string | string[], fn?: (...parameters: any[]|any) => any): any;
79
80 /**
81 * #### chai.spy.restore (function)
82 *
83 * Restores previously wrapped object's method.
84 * Restores all spied objects of a sandbox if called without parameters.
85 *
86 * @function
87 * @param {Object} [object]
88 * @param {String|String[]} [methods] name or names
89 * @return {Sandbox} Sandbox instance
90 */
91 restore(object?: Object, methodNames?: string | string[]): void;
92 }
93 interface Spy {
94 /**
95 * #### chai.spy (function)
96 *
97 * Wraps a function in a proxy function. All calls will pass through to the original function.
98 * ```ts
99 * function original() {}
100 * var spy = chai.spy(original)
101 * , e_spy = chai.spy();
102 * ```
103 * @param fn function to spy on. @default ```function () {}```
104 * @returns function to actually call
105 */
106 (): SpyFunc0Proxy<void>;
107 <R>(fn: SpyFunc0<R>): SpyFunc0Proxy<R>;
108 <A1, R>(fn: SpyFunc1<A1, R>): SpyFunc1Proxy<A1, R>;
109 <A1, A2, R>(fn: SpyFunc2<A1, A2, R>): SpyFunc2Proxy<A1, A2, R>;
110 <A1, A2, A3, R>(fn: SpyFunc3<A1, A2, A3, R>): SpyFunc3Proxy<A1, A2, A3, R>;
111 <A1, A2, A3, A4, R>(fn: SpyFunc4<A1, A2, A3, A4, R>): SpyFunc4Proxy<A1, A2, A3, A4, R>;
112 <A1, A2, A3, A4, A5, R>(fn: SpyFunc5<A1, A2, A3, A4, A5, R>): SpyFunc5Proxy<A1, A2, A3, A4, A5, R>;
113 <A1, A2, A3, A4, A5, A6, R>(fn: SpyFunc6<A1, A2, A3, A4, A5, A6, R>): SpyFunc6Proxy<A1, A2, A3, A4, A5, A6, R>;
114 <A1, A2, A3, A4, A5, A6, A7, R>(fn: SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R>): SpyFunc7Proxy<A1, A2, A3, A4, A5, A6, A7, R>;
115 <A1, A2, A3, A4, A5, A6, A7, A8, R>(fn: SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R>): SpyFunc8Proxy<A1, A2, A3, A4, A5, A6, A7, A8, R>;
116 <A1, A2, A3, A4, A5, A6, A7, A8, A9, R>(fn: SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>): SpyFunc9Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>;
117 <A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>(fn: SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>): SpyFunc10Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>;
118 <R>(name: string, fn: SpyFunc0<R>): SpyFunc0Proxy<R>;
119 <A1, R>(name: string, fn: SpyFunc1<A1, R>): SpyFunc1Proxy<A1, R>;
120 <A1, A2, R>(name: string, fn: SpyFunc2<A1, A2, R>): SpyFunc2Proxy<A1, A2, R>;
121 <A1, A2, A3, R>(name: string, fn: SpyFunc3<A1, A2, A3, R>): SpyFunc3Proxy<A1, A2, A3, R>;
122 <A1, A2, A3, A4, R>(name: string, fn: SpyFunc4<A1, A2, A3, A4, R>): SpyFunc4Proxy<A1, A2, A3, A4, R>;
123 <A1, A2, A3, A4, A5, R>(name: string, fn: SpyFunc5<A1, A2, A3, A4, A5, R>): SpyFunc5Proxy<A1, A2, A3, A4, A5, R>;
124 <A1, A2, A3, A4, A5, A6, R>(name: string, fn: SpyFunc6<A1, A2, A3, A4, A5, A6, R>): SpyFunc6Proxy<A1, A2, A3, A4, A5, A6, R>;
125 <A1, A2, A3, A4, A5, A6, A7, R>(name: string, fn: SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R>): SpyFunc7Proxy<A1, A2, A3, A4, A5, A6, A7, R>;
126 <A1, A2, A3, A4, A5, A6, A7, A8, R>(name: string, fn: SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R>): SpyFunc8Proxy<A1, A2, A3, A4, A5, A6, A7, A8, R>;
127 <A1, A2, A3, A4, A5, A6, A7, A8, A9, R>(name: string, fn: SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>): SpyFunc9Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>;
128 <A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>(name: string, fn: SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>): SpyFunc10Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>;
129
130 /**
131 * #### chai.spy.on (function)
132 *
133 * Wraps an object method into spy. All calls will pass through to the original function.
134 * ```ts
135 * var spy = chai.spy.on(Array, 'isArray');
136 * ```
137 * @param {Object} object
138 * @param {String} method names to spy on
139 * @param {function} fn replacement function
140 * @returns function to actually call
141 */
142 on(object: Object, methodNames: string | string[], fn?: (...parameters: any[]|any) => any): any;
143
144 /**
145 * #### chai.spy.interface (function)
146 *
147 * Creates an object with spied methods.
148 * ```ts
149 * var object = chai.spy.interface('Array', [ 'push', 'pop' ]);
150 * ```
151 * @param {String} [name] object name
152 * @param {String[]|Object} method names or method definitions
153 * @returns object with spied methods
154 */
155 interface(name: string, methods: string[]): any;
156 interface(methods: string[]): any;
157 interface<T>(name: string, methods: T): T;
158 interface<T>(methods: T): T;
159
160 /**
161 * #### chai.spy.restore (function)
162 *
163 * Restores spy assigned to DEFAULT sandbox
164 *
165 * Restores previously wrapped object's method.
166 * Restores all spied objects of a sandbox if called without parameters.
167 *
168 * @param {Object} [object]
169 * @param {String|String[]} [methods] name or names
170 * @return {Sandbox} Sandbox instance
171 */
172 restore(object?: Object, methodNames?: string | string[]): void;
173
174 /**
175 * #### chai.spy.returns (function)
176 *
177 * Creates a spy which returns static value.
178 *```ts
179 * var method = chai.spy.returns(true);
180 *```
181 * @param {*} value static value which is returned by spy
182 * @returns new spy function which returns static value
183 * @api public
184 */
185
186 returns<T>(value: T): SpyFunc0Proxy<T>;
187
188 /**
189 * ### chai.spy.sandbox
190 *
191 * Creates a sandbox.
192 *
193 * Sandbox is a set of spies.
194 * Sandbox allows to track methods on objects and restore original methods with on restore call.
195 *
196 * @returns {Sandbox}
197 */
198 sandbox(): Sandbox;
199 }
200
201 interface Called {
202 (): Chai.Assertion;
203 with: With;
204 always: Always;
205
206 /**
207 * ####.once
208 * Assert that a spy has been called exactly once.
209 * ```ts
210 * expect(spy).to.have.been.called.once;
211 * expect(spy).to.not.have.been.called.once;
212 * spy.should.have.been.called.once;
213 * spy.should.not.have.been.called.once;
214 * ```
215 */
216 once: Chai.Assertion;
217
218 /**
219 * ####.twice
220 * Assert that a spy has been called exactly twice.
221 * ```ts
222 * expect(spy).to.have.been.called.twice;
223 * expect(spy).to.not.have.been.called.twice;
224 * spy.should.have.been.called.twice;
225 * spy.should.not.have.been.called.twice;
226 * ```
227 */
228 twice: Chai.Assertion;
229
230 /**
231 * ####.exactly(n)
232 * Assert that a spy has been called exactly ```n``` times.
233 * ```ts
234 * expect(spy).to.have.been.called.exactly(3);
235 * expect(spy).to.not.have.been.called.exactly(3);
236 * spy.should.have.been.called.exactly(3);
237 * spy.should.not.have.been.called.exactly(3);
238 * ```
239 */
240 exactly(n: number): Chai.Assertion;
241
242 /**
243 * ####.min(n) / .at.least(n)
244 * Assert that a spy has been called minimum of ```n``` times.
245 * ```ts
246 * expect(spy).to.have.been.called.min(3);
247 * expect(spy).to.not.have.been.called.at.least(3);
248 * spy.should.have.been.called.at.least(3);
249 * spy.should.not.have.been.called.min(3);
250 * ```
251 */
252 min(n: number): Chai.Assertion;
253
254 /**
255 * ####.max(n) / .at.most(n)
256 * Assert that a spy has been called maximum of ```n``` times.
257 * ```ts
258 * expect(spy).to.have.been.called.max(3);
259 * expect(spy).to.not.have.been.called.at.most(3);
260 * spy.should.have.been.called.at.most(3);
261 * spy.should.not.have.been.called.max(3);
262 * ```
263 */
264 max(n: number): Chai.Assertion;
265
266 at: At;
267 /**
268 * ####.above(n) / .gt(n)
269 * Assert that a spy has been called more than ```n``` times.
270 * ```ts
271 * expect(spy).to.have.been.called.above(3);
272 * spy.should.not.have.been.called.above(3);
273 * ```
274 */
275 above(n: number): Chai.Assertion;
276
277 /**
278 * ####.above(n) / .gt(n)
279 * Assert that a spy has been called more than ```n``` times.
280 * ```ts
281 * expect(spy).to.have.been.called.gt(3);
282 * spy.should.not.have.been.called.gt(3);
283 * ```
284 */
285 gt(n: number): Chai.Assertion;
286
287 /**
288 * ####.below(n) / .lt(n)
289 * Assert that a spy has been called fewer than ```n``` times.
290 * ```ts
291 * expect(spy).to.have.been.called.below(3);
292 * spy.should.not.have.been.called.below(3);
293 * ```
294 */
295 below(n: number): Chai.Assertion;
296
297 /**
298 * ####.below(n) / .lt(n)
299 * Assert that a spy has been called fewer than ```n``` times.
300 * ```ts
301 * expect(spy).to.have.been.called.lt(3);
302 * spy.should.not.have.been.called.lt(3);
303 * ```
304 */
305 lt(n: number): Chai.Assertion;
306 }
307
308 interface Been extends Chai.Assertion {
309 (): Chai.Assertion;
310 called: Called;
311
312 /**
313 * ####.first
314 * Assert that a spy has been called first.
315 * ```ts
316 * expect(spy).to.have.been.called.first;
317 * expect(spy).to.not.have.been.called.first;
318 * spy.should.have.been.called.first;
319 * spy.should.not.have.been.called.first;
320 * ```
321 */
322 first: Chai.Assertion;
323
324 /**
325 * ####.second
326 * Assert that a spy has been called second.
327 * ```ts
328 * expect(spy).to.have.been.called.second;
329 * expect(spy).to.not.have.been.called.second;
330 * spy.should.have.been.called.second;
331 * spy.should.not.have.been.called.second;
332 * ```
333 */
334 second: Chai.Assertion;
335
336 /**
337 * ####.third
338 * Assert that a spy has been called third.
339 * ```ts
340 * expect(spy).to.have.been.called.third;
341 * expect(spy).to.not.have.been.called.third;
342 * spy.should.have.been.called.third;
343 * spy.should.not.have.been.called.third;
344 * ```
345 */
346 third: Chai.Assertion;
347 }
348
349 interface With {
350 /**
351 * ####.with
352 * Assert that a spy has been called with a given argument at least once, even if more arguments were provided.
353 * ```ts
354 * spy('foo');
355 * expect(spy).to.have.been.called.with('foo');
356 * spy.should.have.been.called.with('foo');
357 * ```
358 * Will also pass for ```spy('foo', 'bar')``` and ```spy(); spy('foo')```.
359 * If used with multiple arguments, assert that a spy has been called with all the given arguments at least once.
360 * ```ts
361 * spy('foo', 'bar', 1);
362 * expect(spy).to.have.been.called.with('bar', 'foo');
363 * spy.should.have.been.called.with('bar', 'foo');
364 * ```
365 */
366 (a: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion;
367
368 /**
369 * ####.with.exactly
370 * Similar to .with, but will pass only if the list of arguments is exactly the same as the one provided.
371 * ```ts
372 * spy();
373 * spy('foo', 'bar');
374 * expect(spy).to.have.been.called.with.exactly('foo', 'bar');
375 * spy.should.have.been.called.with.exactly('foo', 'bar');
376 * ```
377 * Will not pass for ```spy('foo')```, ```spy('bar')```, ```spy('bar'); spy('foo')```, ```spy('foo'); spy('bar')```, ```spy('bar', 'foo')``` or ```spy('foo', 'bar', 1)```.
378 * Can be used for calls with a single argument too.
379 */
380
381 exactly(a?: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion;
382 }
383
384 interface Always {
385 with: AlwaysWith;
386 }
387
388 interface AlwaysWith {
389 /**
390 * ####.always.with
391 * Assert that every time the spy has been called the argument list contained the given arguments.
392 * ```ts
393 * spy('foo');
394 * spy('foo', 'bar');
395 * spy(1, 2, 'foo');
396 * expect(spy).to.have.been.called.always.with('foo');
397 * spy.should.have.been.called.always.with('foo');
398 * ```
399 */
400 (a: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion;
401
402 /**
403 * ####.always.with.exactly
404 * Assert that the spy has never been called with a different list of arguments than the one provided.
405 * ```ts
406 * spy('foo');
407 * spy('foo');
408 * expect(spy).to.have.been.called.always.with.exactly('foo');
409 * spy.should.have.been.called.always.with.exactly('foo');
410 * ```
411 */
412 exactly(a?: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion;
413 }
414
415 interface At {
416 /**
417 * ####.min(n) / .at.least(n)
418 * Assert that a spy has been called minimum of ```n``` times.
419 * ```ts
420 * expect(spy).to.have.been.called.min(3);
421 * expect(spy).to.not.have.been.called.at.least(3);
422 * spy.should.have.been.called.at.least(3);
423 * spy.should.not.have.been.called.min(3);
424 * ```
425 */
426 least(n: number): Chai.Assertion;
427
428 /**
429 * ####.max(n) / .at.most(n)
430 * Assert that a spy has been called maximum of ```n``` times.
431 * ```ts
432 * expect(spy).to.have.been.called.max(3);
433 * expect(spy).to.not.have.been.called.at.most(3);
434 * spy.should.have.been.called.at.most(3);
435 * spy.should.not.have.been.called.max(3);
436 * ```
437 */
438 most(n: number): Chai.Assertion;
439 }
440
441 interface Resetable {
442 /**
443 * #### proxy.reset (function)
444 *
445 * Resets __spy object parameters for instantiation and reuse
446 * @returns proxy spy object
447 */
448 reset(): this;
449 }
450
451 interface SpyFunc0<R> {
452 (): R;
453 }
454
455 interface SpyFunc1<A1, R> {
456 (a: A1): R;
457 }
458
459 interface SpyFunc2<A1, A2, R> {
460 (a: A1, b: A2): R;
461 }
462
463 interface SpyFunc3<A1, A2, A3, R> {
464 (a: A1, b: A2, c: A3): R;
465 }
466
467 interface SpyFunc4<A1, A2, A3, A4, R> {
468 (a: A1, b: A2, c: A3, d: A4): R;
469 }
470
471 interface SpyFunc5<A1, A2, A3, A4, A5, R> {
472 (a: A1, b: A2, c: A3, d: A4, e: A5): R;
473 }
474
475 interface SpyFunc6<A1, A2, A3, A4, A5, A6, R> {
476 (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6): R;
477 }
478
479 interface SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R> {
480 (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7): R;
481 }
482
483 interface SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R> {
484 (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8): R;
485 }
486
487 interface SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> {
488 (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9): R;
489 }
490
491 interface SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> {
492 (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9, j: A10): R;
493 }
494
495 interface SpyFunc0Proxy<R> extends SpyFunc0<R>, Resetable {
496 }
497
498 interface SpyFunc1Proxy<A1, R> extends SpyFunc1<A1, R>, Resetable {
499 }
500
501 interface SpyFunc2Proxy<A1, A2, R> extends SpyFunc2<A1, A2, R>, Resetable {
502 }
503
504 interface SpyFunc3Proxy<A1, A2, A3, R> extends SpyFunc3<A1, A2, A3, R>, Resetable {
505 }
506
507 interface SpyFunc4Proxy<A1, A2, A3, A4, R> extends SpyFunc4<A1, A2, A3, A4, R>, Resetable {
508 }
509
510 interface SpyFunc5Proxy<A1, A2, A3, A4, A5, R> extends SpyFunc5<A1, A2, A3, A4, A5, R>, Resetable {
511 }
512
513 interface SpyFunc6Proxy<A1, A2, A3, A4, A5, A6, R> extends SpyFunc6<A1, A2, A3, A4, A5, A6, R>, Resetable {
514 }
515
516 interface SpyFunc7Proxy<A1, A2, A3, A4, A5, A6, A7, R> extends SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R>, Resetable {
517 }
518
519 interface SpyFunc8Proxy<A1, A2, A3, A4, A5, A6, A7, A8, R> extends SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R>, Resetable {
520 }
521
522 interface SpyFunc9Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> extends SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>, Resetable {
523 }
524
525 interface SpyFunc10Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> extends SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>, Resetable {
526 }
527}
528
529declare var spies: ChaiSpies.Spy;
530
531declare module "chai-spies" {
532 export = spies;
533}