UNPKG

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