UNPKG

70.8 kBTypeScriptView Raw
1// Type definitions for Sinon 7.5
2// Project: https://sinonjs.org
3// Definitions by: William Sears <https://github.com/mrbigdog2u>
4// Jonathan Little <https://github.com/rationull>
5// Lukas Spieß <https://github.com/lumaxis>
6// Nico Jansen <https://github.com/nicojs>
7// James Garbutt <https://github.com/43081j>
8// Josh Goldberg <https://github.com/joshuakgoldberg>
9// Greg Jednaszewski <https://github.com/gjednaszewski>
10// John Wood <https://github.com/johnjesse>
11// Alec Flett <https://github.com/alecf>
12// Simon Schick <https://github.com/SimonSchick>
13// Roey Berman <https://github.com/bergundy>
14// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
15// TypeScript Version: 2.8
16
17// sinon uses DOM dependencies which are absent in browser-less environment like node.js
18// to avoid compiler errors this monkey patch is used
19// see more details in https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11351
20interface Event { } // tslint:disable-line no-empty-interface
21interface Document { } // tslint:disable-line no-empty-interface
22
23declare namespace Sinon {
24 interface SinonSpyCallApi {
25 // Properties
26 /**
27 * Array of received arguments.
28 */
29 args: any[];
30
31 // Methods
32 /**
33 * Returns true if the spy was called at least once with @param obj as this.
34 * calledOn also accepts a matcher spyCall.calledOn(sinon.match(fn)) (see matchers).
35 * @param obj
36 */
37 calledOn(obj: any): boolean;
38 /**
39 * Returns true if spy was called at least once with the provided arguments.
40 * Can be used for partial matching, Sinon only checks the provided arguments against actual arguments,
41 * so a call that received the provided arguments (in the same spots) and possibly others as well will return true.
42 * @param args
43 */
44 calledWith(...args: any[]): boolean;
45 /**
46 * Returns true if spy was called at least once with the provided arguments and no others.
47 */
48 calledWithExactly(...args: any[]): boolean;
49 /**
50 * Returns true if spy/stub was called the new operator.
51 * Beware that this is inferred based on the value of the this object and the spy function’s prototype,
52 * so it may give false positives if you actively return the right kind of object.
53 */
54 calledWithNew(): boolean;
55 /**
56 * Returns true if spy was called at exactly once with the provided arguments.
57 * @param args
58 */
59 calledOnceWith(...args: any[]): boolean;
60 calledOnceWithExactly(...args: any[]): boolean;
61 /**
62 * Returns true if spy was called with matching arguments (and possibly others).
63 * This behaves the same as spy.calledWith(sinon.match(arg1), sinon.match(arg2), ...).
64 * @param args
65 */
66 calledWithMatch(...args: any[]): boolean;
67 /**
68 * Returns true if call did not receive provided arguments.
69 * @param args
70 */
71 notCalledWith(...args: any[]): boolean;
72 /**
73 * Returns true if call did not receive matching arguments.
74 * This behaves the same as spyCall.notCalledWith(sinon.match(arg1), sinon.match(arg2), ...).
75 * @param args
76 */
77 notCalledWithMatch(...args: any[]): boolean;
78 /**
79 * Returns true if spy returned the provided value at least once.
80 * Uses deep comparison for objects and arrays. Use spy.returned(sinon.match.same(obj)) for strict comparison (see matchers).
81 * @param value
82 */
83 returned(value: any): boolean;
84 /**
85 * Returns true if spy threw an exception at least once.
86 */
87 threw(): boolean;
88 /**
89 * Returns true if spy threw an exception of the provided type at least once.
90 */
91 threw(type: string): boolean;
92 /**
93 * Returns true if spy threw the provided exception object at least once.
94 */
95 threw(obj: any): boolean;
96 /**
97 * Like yield, but with an explicit argument number specifying which callback to call.
98 * Useful if a function is called with more than one callback, and simply calling the first callback is not desired.
99 * @param pos
100 */
101 callArg(pos: number): void;
102 callArgOn(pos: number, obj: any, ...args: any[]): void;
103 /**
104 * Like callArg, but with arguments.
105 */
106 callArgWith(pos: number, ...args: any[]): void;
107 callArgOnWith(pos: number, obj: any, ...args: any[]): void;
108 /**
109 * Invoke callbacks passed to the stub with the given arguments.
110 * If the stub was never called with a function argument, yield throws an error.
111 * Returns an Array with all callbacks return values in the order they were called, if no error is thrown.
112 * Also aliased as invokeCallback.
113 */
114 yield(...args: any[]): void;
115 yieldOn(obj: any, ...args: any[]): void;
116 /**
117 * Invokes callbacks passed as a property of an object to the stub.
118 * Like yield, yieldTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.
119 */
120 yieldTo(property: string, ...args: any[]): void;
121 yieldToOn(property: string, obj: any, ...args: any[]): void;
122 }
123
124 interface SinonSpyCall extends SinonSpyCallApi {
125 /**
126 * The call’s this value.
127 */
128 thisValue: any;
129 /**
130 * Exception thrown, if any.
131 */
132 exception: any;
133 /**
134 * Return value.
135 */
136 returnValue: any;
137 /**
138 * This property is a convenience for a call’s callback.
139 * When the last argument in a call is a Function, then callback will reference that. Otherwise it will be undefined.
140 */
141 callback: Function | undefined;
142 /**
143 * This property is a convenience for the last argument of the call.
144 */
145 lastArg: any;
146
147 /**
148 * Returns true if the spy call occurred before another spy call.
149 * @param call
150 */
151 calledBefore(call: SinonSpyCall): boolean;
152 /**
153 * Returns true if the spy call occurred after another spy call.
154 * @param call
155 */
156 calledAfter(call: SinonSpyCall): boolean;
157 }
158
159 interface SinonSpy extends SinonSpyCallApi {
160 // Properties
161 /**
162 * The number of recorded calls.
163 */
164 callCount: number;
165 /**
166 * true if the spy was called at least once
167 */
168 called: boolean;
169 /**
170 * true if the spy was not called
171 */
172 notCalled: boolean;
173 /**
174 * true if spy was called exactly once
175 */
176 calledOnce: boolean;
177 /**
178 * true if the spy was called exactly twice
179 */
180 calledTwice: boolean;
181 /**
182 * true if the spy was called exactly thrice
183 */
184 calledThrice: boolean;
185 /**
186 * The first call
187 */
188 firstCall: SinonSpyCall;
189 /**
190 * The second call
191 */
192 secondCall: SinonSpyCall;
193 /**
194 * The third call
195 */
196 thirdCall: SinonSpyCall;
197 /**
198 * The last call
199 */
200 lastCall: SinonSpyCall;
201 /**
202 * Array of this objects, spy.thisValues[0] is the this object for the first call.
203 */
204 thisValues: any[];
205 /**
206 * Array of arguments received, spy.args[0] is an array of arguments received in the first call.
207 */
208 args: any[][];
209 /**
210 * Array of exception objects thrown, spy.exceptions[0] is the exception thrown by the first call.
211 * If the call did not throw an error, the value at the call’s location in .exceptions will be undefined.
212 */
213 exceptions: any[];
214 /**
215 * Array of return values, spy.returnValues[0] is the return value of the first call.
216 * If the call did not explicitly return a value, the value at the call’s location in .returnValues will be undefined.
217 */
218 returnValues: any[];
219
220 // Methods
221 (...args: any[]): any;
222 /**
223 * Returns true if the spy was called before @param anotherSpy
224 * @param anotherSpy
225 */
226 calledBefore(anotherSpy: SinonSpy): boolean;
227 /**
228 * Returns true if the spy was called after @param anotherSpy
229 * @param anotherSpy
230 */
231 calledAfter(anotherSpy: SinonSpy): boolean;
232 /**
233 * Returns true if spy was called before @param anotherSpy, and no spy calls occurred between spy and @param anotherSpy.
234 * @param anotherSpy
235 */
236 calledImmediatelyBefore(anotherSpy: SinonSpy): boolean;
237 /**
238 * Returns true if spy was called after @param anotherSpy, and no spy calls occurred between @param anotherSpy and spy.
239 * @param anotherSpy
240 */
241 calledImmediatelyAfter(anotherSpy: SinonSpy): boolean;
242 /**
243 * Creates a spy that only records calls when the received arguments match those passed to withArgs.
244 * This is useful to be more expressive in your assertions, where you can access the spy with the same call.
245 * @param args Expected args
246 */
247 withArgs(...args: any[]): SinonSpy;
248 /**
249 * Returns true if the spy was always called with @param obj as this.
250 * @param obj
251 */
252 alwaysCalledOn(obj: any): boolean;
253 /**
254 * Returns true if spy was always called with the provided arguments (and possibly others).
255 */
256 alwaysCalledWith(...args: any[]): boolean;
257 /**
258 * Returns true if spy was always called with the exact provided arguments.
259 * @param args
260 */
261 alwaysCalledWithExactly(...args: any[]): boolean;
262 /**
263 * Returns true if spy was always called with matching arguments (and possibly others).
264 * This behaves the same as spy.alwaysCalledWith(sinon.match(arg1), sinon.match(arg2), ...).
265 * @param args
266 */
267 alwaysCalledWithMatch(...args: any[]): boolean;
268 /**
269 * Returns true if the spy/stub was never called with the provided arguments.
270 * @param args
271 */
272 neverCalledWith(...args: any[]): boolean;
273 /**
274 * Returns true if the spy/stub was never called with matching arguments.
275 * This behaves the same as spy.neverCalledWith(sinon.match(arg1), sinon.match(arg2), ...).
276 * @param args
277 */
278 neverCalledWithMatch(...args: any[]): boolean;
279 /**
280 * Returns true if spy always threw an exception.
281 */
282 alwaysThrew(): boolean;
283 /**
284 * Returns true if spy always threw an exception of the provided type.
285 */
286 alwaysThrew(type: string): boolean;
287 /**
288 * Returns true if spy always threw the provided exception object.
289 */
290 alwaysThrew(obj: any): boolean;
291 /**
292 * Returns true if spy always returned the provided value.
293 * @param obj
294 */
295 alwaysReturned(obj: any): boolean;
296 /**
297 * Invoke callbacks passed to the stub with the given arguments.
298 * If the stub was never called with a function argument, yield throws an error.
299 * Returns an Array with all callbacks return values in the order they were called, if no error is thrown.
300 */
301 invokeCallback(...args: any[]): void;
302 /**
303 * Set the displayName of the spy or stub.
304 * @param name
305 */
306 named(name: string): SinonSpy;
307 /**
308 * Returns the nth call.
309 * Accessing individual calls helps with more detailed behavior verification when the spy is called more than once.
310 * @param n
311 */
312 getCall(n: number): SinonSpyCall;
313 /**
314 * Returns an Array of all calls recorded by the spy.
315 */
316 getCalls(): SinonSpyCall[];
317 /**
318 * Resets the state of a spy.
319 */
320 resetHistory(): void;
321 /**
322 * Returns the passed format string with the following replacements performed:
323 * * %n - the name of the spy "spy" by default)
324 * * %c - the number of times the spy was called, in words ("once", "twice", etc.)
325 * * %C - a list of string representations of the calls to the spy, with each call prefixed by a newline and four spaces
326 * * %t - a comma-delimited list of this values the spy was called on
327 * * %n - the formatted value of the nth argument passed to printf
328 * * %* - a comma-delimited list of the (non-format string) arguments passed to printf
329 * * %D - a multi-line list of the arguments received by all calls to the spy
330 * @param format
331 * @param args
332 */
333 printf(format: string, ...args: any[]): string;
334 /**
335 * Replaces the spy with the original method. Only available if the spy replaced an existing method.
336 */
337 restore(): void;
338 }
339
340 interface SinonSpyStatic {
341 /**
342 * Creates an anonymous function that records arguments, this value, exceptions and return values for all calls.
343 */
344 (): SinonSpy;
345 /**
346 * Spies on the provided function
347 */
348 (func: Function): SinonSpy;
349 /**
350 * Creates a spy for object.method and replaces the original method with the spy.
351 * An exception is thrown if the property is not already a function.
352 * The spy acts exactly like the original method in all cases.
353 * The original method can be restored by calling object.method.restore().
354 * The returned spy is the function object which replaced the original method. spy === object.method.
355 */
356 <T>(obj: T, method: keyof T, types?: string[]): SinonSpy;
357 }
358
359 interface SinonStub extends SinonSpy {
360 /**
361 * Resets the stub’s behaviour to the default behaviour
362 * You can reset behaviour of all stubs using sinon.resetBehavior()
363 */
364 resetBehavior(): void;
365 /**
366 * Resets both behaviour and history of the stub.
367 * This is equivalent to calling both stub.resetBehavior() and stub.resetHistory()
368 * Updated in sinon@2.0.0
369 * Since sinon@5.0.0
370 * As a convenience, you can apply stub.reset() to all stubs using sinon.reset()
371 */
372 reset(): void;
373 /**
374 * Causes the stub to return promises using a specific Promise library instead of the global one when using stub.rejects or stub.resolves.
375 * Returns the stub to allow chaining.
376 */
377 usingPromise(promiseLibrary: any): SinonStub;
378
379 /**
380 * Makes the stub return the provided @param obj value.
381 * @param obj
382 */
383 returns(obj: any): SinonStub;
384 /**
385 * Causes the stub to return the argument at the provided @param index.
386 * stub.returnsArg(0); causes the stub to return the first argument.
387 * If the argument at the provided index is not available, prior to sinon@6.1.2, an undefined value will be returned;
388 * starting from sinon@6.1.2, a TypeError will be thrown.
389 * @param index
390 */
391 returnsArg(index: number): SinonStub;
392 /**
393 * Causes the stub to return its this value.
394 * Useful for stubbing jQuery-style fluent APIs.
395 */
396 returnsThis(): SinonStub;
397 /**
398 * Causes the stub to return a Promise which resolves to the provided value.
399 * When constructing the Promise, sinon uses the Promise.resolve method.
400 * You are responsible for providing a polyfill in environments which do not provide Promise.
401 * The Promise library can be overwritten using the usingPromise method.
402 * Since sinon@2.0.0
403 */
404 resolves(value?: any): SinonStub;
405 /**
406 * Causes the stub to return a Promise which resolves to the argument at the provided index.
407 * stub.resolvesArg(0); causes the stub to return a Promise which resolves to the first argument.
408 * If the argument at the provided index is not available, a TypeError will be thrown.
409 */
410 resolvesArg(index: number): SinonStub;
411 /**
412 * Causes the stub to return a Promise which resolves to its this value.
413 */
414 resolvesThis(): SinonStub;
415 /**
416 * Causes the stub to throw an exception (Error).
417 * @param type
418 */
419 throws(type?: string): SinonStub;
420 /**
421 * Causes the stub to throw the provided exception object.
422 */
423 throws(obj: any): SinonStub;
424 /**
425 * Causes the stub to throw the argument at the provided index.
426 * stub.throwsArg(0); causes the stub to throw the first argument as the exception.
427 * If the argument at the provided index is not available, a TypeError will be thrown.
428 * Since sinon@2.3.0
429 * @param index
430 */
431 throwsArg(index: number): SinonStub;
432 throwsException(type?: string): SinonStub;
433 throwsException(obj: any): SinonStub;
434 /**
435 * Causes the stub to return a Promise which rejects with an exception (Error).
436 * When constructing the Promise, sinon uses the Promise.reject method.
437 * You are responsible for providing a polyfill in environments which do not provide Promise.
438 * The Promise library can be overwritten using the usingPromise method.
439 * Since sinon@2.0.0
440 */
441 rejects(): SinonStub;
442 /**
443 * Causes the stub to return a Promise which rejects with an exception of the provided type.
444 * Since sinon@2.0.0
445 */
446 rejects(errorType: string): SinonStub;
447 /**
448 * Causes the stub to return a Promise which rejects with the provided exception object.
449 * Since sinon@2.0.0
450 */
451 rejects(value: any): SinonStub;
452 /**
453 * Causes the stub to call the argument at the provided index as a callback function.
454 * stub.callsArg(0); causes the stub to call the first argument as a callback.
455 * If the argument at the provided index is not available or is not a function, a TypeError will be thrown.
456 */
457 callsArg(index: number): SinonStub;
458 /**
459 * Causes the original method wrapped into the stub to be called when none of the conditional stubs are matched.
460 */
461 callThrough(): SinonStub;
462 /**
463 * Like stub.callsArg(index); but with an additional parameter to pass the this context.
464 * @param index
465 * @param context
466 */
467 callsArgOn(index: number, context: any): SinonStub;
468 /**
469 * Like callsArg, but with arguments to pass to the callback.
470 * @param index
471 * @param args
472 */
473 callsArgWith(index: number, ...args: any[]): SinonStub;
474 /**
475 * Like above but with an additional parameter to pass the this context.
476 * @param index
477 * @param context
478 * @param args
479 */
480 callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub;
481 /**
482 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
483 * In Node environment the callback is deferred with process.nextTick.
484 * In a browser the callback is deferred with setTimeout(callback, 0).
485 * @param index
486 */
487 callsArgAsync(index: number): SinonStub;
488 /**
489 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
490 * In Node environment the callback is deferred with process.nextTick.
491 * In a browser the callback is deferred with setTimeout(callback, 0).
492 * @param index
493 * @param context
494 */
495 callsArgOnAsync(index: number, context: any): SinonStub;
496 /**
497 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
498 * In Node environment the callback is deferred with process.nextTick.
499 * In a browser the callback is deferred with setTimeout(callback, 0).
500 */
501 callsArgWithAsync(index: number, ...args: any[]): SinonStub;
502 /**
503 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
504 * In Node environment the callback is deferred with process.nextTick.
505 * In a browser the callback is deferred with setTimeout(callback, 0).
506 */
507 callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub;
508 /**
509 * Makes the stub call the provided @param func when invoked.
510 * @param func
511 */
512 callsFake(func: (...args: any[]) => any): SinonStub;
513 /**
514 * Replaces a new getter for this stub.
515 */
516 get(func: () => any): SinonStub;
517 /**
518 * Defines a new setter for this stub.
519 * @param func
520 */
521 set(func: (v: any) => void): SinonStub;
522 /**
523 * Defines the behavior of the stub on the @param n call. Useful for testing sequential interactions.
524 * There are methods onFirstCall, onSecondCall,onThirdCall to make stub definitions read more naturally.
525 * onCall can be combined with all of the behavior defining methods in this section. In particular, it can be used together with withArgs.
526 * @param n
527 */
528 onCall(n: number): SinonStub;
529 /**
530 * Alias for stub.onCall(0);
531 */
532 onFirstCall(): SinonStub;
533 /**
534 * Alias for stub.onCall(1);
535 */
536 onSecondCall(): SinonStub;
537 /**
538 * Alias for stub.onCall(2);
539 */
540 onThirdCall(): SinonStub;
541 /**
542 * Defines a new value for this stub.
543 * @param val
544 */
545 value(val: any): SinonStub;
546 /**
547 * Set the displayName of the spy or stub.
548 * @param name
549 */
550 named(name: string): SinonStub;
551 /**
552 * Similar to callsArg.
553 * Causes the stub to call the first callback it receives with the provided arguments (if any).
554 * If a method accepts more than one callback, you need to use callsArg to have the stub invoke other callbacks than the first one.
555 */
556 yields(...args: any[]): SinonStub;
557 /**
558 * Like above but with an additional parameter to pass the this context.
559 */
560 yieldsOn(context: any, ...args: any[]): SinonStub;
561 yieldsRight(...args: any[]): SinonStub;
562 /**
563 * Causes the spy to invoke a callback passed as a property of an object to the spy.
564 * Like yields, yieldsTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.
565 * @param property
566 * @param args
567 */
568 yieldsTo(property: string, ...args: any[]): SinonStub;
569 /**
570 * Like above but with an additional parameter to pass the this context.
571 */
572 yieldsToOn(property: string, context: any, ...args: any[]): SinonStub;
573 /**
574 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
575 * In Node environment the callback is deferred with process.nextTick.
576 * In a browser the callback is deferred with setTimeout(callback, 0).
577 * @param args
578 */
579 yieldsAsync(...args: any[]): SinonStub;
580 /**
581 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
582 * In Node environment the callback is deferred with process.nextTick.
583 * In a browser the callback is deferred with setTimeout(callback, 0).
584 * @param context
585 * @param args
586 */
587 yieldsOnAsync(context: any, ...args: any[]): SinonStub;
588 /**
589 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
590 * In Node environment the callback is deferred with process.nextTick.
591 * In a browser the callback is deferred with setTimeout(callback, 0).
592 * @param property
593 * @param args
594 */
595 yieldsToAsync(property: string, ...args: any[]): SinonStub;
596 /**
597 * Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
598 * In Node environment the callback is deferred with process.nextTick.
599 * In a browser the callback is deferred with setTimeout(callback, 0).
600 * @param property
601 * @param context
602 * @param args
603 */
604 yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub;
605 /**
606 * Stubs the method only for the provided arguments.
607 * This is useful to be more expressive in your assertions, where you can access the spy with the same call.
608 * It is also useful to create a stub that can act differently in response to different arguments.
609 * @param args
610 */
611 withArgs(...args: any[]): SinonStub;
612 }
613
614 interface SinonStubStatic {
615 /**
616 * Creates an anonymous stub function
617 */
618 (): SinonStub;
619 /**
620 * Stubs all the object’s methods.
621 * Note that it’s usually better practice to stub individual methods, particularly on objects that you don’t understand or control all the methods for (e.g. library dependencies).
622 * Stubbing individual methods tests intent more precisely and is less susceptible to unexpected behavior as the object’s code evolves.
623 * If you want to create a stub object of MyConstructor, but don’t want the constructor to be invoked, use this utility function.
624 */
625 <T>(obj: T): SinonStubbedInstance<T>;
626 /**
627 * Replaces obj.method with a stub function.
628 * An exception is thrown if the property is not already a function.
629 * The original function can be restored by calling object.method.restore(); (or stub.restore();).
630 */
631 <T>(obj: T, method: keyof T): SinonStub;
632 }
633
634 interface SinonExpectation extends SinonStub {
635 /**
636 * Specify the minimum amount of calls expected.
637 */
638 atLeast(n: number): SinonExpectation;
639 /**
640 * Specify the maximum amount of calls expected.
641 * @param n
642 */
643 atMost(n: number): SinonExpectation;
644 /**
645 * Expect the method to never be called.
646 */
647 never(): SinonExpectation;
648 /**
649 * Expect the method to be called exactly once.
650 */
651 once(): SinonExpectation;
652 /**
653 * Expect the method to be called exactly twice.
654 */
655 twice(): SinonExpectation;
656 /**
657 * Expect the method to be called exactly thrice.
658 */
659 thrice(): SinonExpectation;
660 /**
661 * Expect the method to be called exactly @param n times.
662 */
663 exactly(n: number): SinonExpectation;
664 /**
665 * Expect the method to be called with the provided arguments and possibly others.
666 * An expectation instance only holds onto a single set of arguments specified with withArgs.
667 * Subsequent calls will overwrite the previously-specified set of arguments (even if they are different),
668 * so it is generally not intended that this method be invoked more than once per test case.
669 * @param args
670 */
671 withArgs(...args: any[]): SinonExpectation;
672 /**
673 * Expect the method to be called with the provided arguments and no others.
674 * An expectation instance only holds onto a single set of arguments specified with withExactArgs.
675 * Subsequent calls will overwrite the previously-specified set of arguments (even if they are different),
676 * so it is generally not intended that this method be invoked more than once per test case.
677 * @param args
678 */
679 withExactArgs(...args: any[]): SinonExpectation;
680 on(obj: any): SinonExpectation;
681 /**
682 * Verifies all expectations on the mock.
683 * If any expectation is not satisfied, an exception is thrown.
684 * Also restores the mocked methods.
685 */
686 verify(): SinonExpectation;
687 /**
688 * Restores all mocked methods.
689 */
690 restore(): void;
691 }
692
693 interface SinonExpectationStatic {
694 /**
695 * Creates an expectation without a mock object, basically an anonymous mock function.
696 * Method name is optional and is used in exception messages to make them more readable.
697 * @param methodName
698 */
699 create(methodName?: string): SinonExpectation;
700 }
701
702 interface SinonMock {
703 /**
704 * Overrides obj.method with a mock function and returns it.
705 */
706 expects(method: string): SinonExpectation;
707 /**
708 * Restores all mocked methods.
709 */
710 restore(): void;
711 /**
712 * Verifies all expectations on the mock.
713 * If any expectation is not satisfied, an exception is thrown.
714 * Also restores the mocked methods.
715 */
716 verify(): void;
717 }
718
719 interface SinonMockStatic {
720 (): SinonExpectation;
721 /**
722 * Creates a mock for the provided object.
723 * Does not change the object, but returns a mock object to set expectations on the object’s methods.
724 */
725 (obj: any): SinonMock;
726 }
727
728 type SinonTimerId = number | { id: number };
729
730 interface SinonFakeTimers {
731 now: number;
732 loopLimit: number;
733
734 setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): SinonTimerId;
735 clearTimeout(id: SinonTimerId): void;
736
737 setInterval(callback: (...args: any[]) => void, timeout: number, ...args: any[]): SinonTimerId;
738 clearInterval(id: SinonTimerId): void;
739
740 setImmediate(callback: (...args: any[]) => void, ...args: any[]): SinonTimerId;
741 clearImmediate(id: SinonTimerId): void;
742
743 requestAnimationFrame(callback: (time: number) => void): SinonTimerId;
744 cancelAnimationFrame(id: SinonTimerId): void;
745
746 nextTick(callback: (...args: any[]) => void, ...args: any[]): void;
747 queueMicrotask(callback: () => void): void;
748
749 requestIdleCallback(func: (...args: any[]) => void, timeout?: number, ...args: any[]): SinonTimerId;
750 cancelIdleCallback(timerId: SinonTimerId): void;
751
752 /**
753 * Tick the clock ahead time milliseconds.
754 * Causes all timers scheduled within the affected time range to be called.
755 * time may be the number of milliseconds to advance the clock by or a human-readable string.
756 * Valid string formats are “08” for eight seconds, “01:00” for one minute and “02:34:10” for two hours, 34 minutes and ten seconds.
757 * time may be negative, which causes the clock to change but won’t fire any callbacks.
758 * @param ms
759 */
760 tick(ms: number | string): number;
761 /**
762 * Advances the clock to the the moment of the first scheduled timer, firing it.
763 */
764 next(): number;
765 /**
766 * This runs all pending timers until there are none remaining. If new timers are added while it is executing they will be run as well.
767 * This makes it easier to run asynchronous tests to completion without worrying about the number of timers they use, or the delays in those timers.
768 */
769 runAll(): number;
770 runToLast(): number;
771 reset(): void;
772 runMicrotasks(): void;
773 runToFrame(): number;
774
775 Date(): Date;
776 Date(year: number): Date;
777 Date(year: number, month: number): Date;
778 Date(year: number, month: number, day: number): Date;
779 Date(year: number, month: number, day: number, hour: number): Date;
780 Date(year: number, month: number, day: number, hour: number, minute: number): Date;
781 Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date;
782 Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date;
783
784 /**
785 * Restore the faked methods.
786 * Call in e.g. tearDown.
787 */
788 restore(): void;
789 uninstall(): void;
790
791 /**
792 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
793 * without affecting timers, intervals or immediates.
794 * @param now The new 'now' in unix milliseconds
795 */
796 setSystemTime(now: number): void;
797 /**
798 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
799 * without affecting timers, intervals or immediates.
800 * @param now The new 'now' as a JavaScript Date
801 */
802 setSystemTime(date: Date): void;
803
804 countTimers(): number;
805 }
806
807 interface SinonFakeTimersConfig {
808 now: number | Date;
809 toFake: string[];
810 shouldAdvanceTime: boolean;
811 }
812
813 interface SinonFakeUploadProgress {
814 eventListeners: {
815 progress: any[];
816 load: any[];
817 abort: any[];
818 error: any[];
819 };
820
821 addEventListener(event: string, listener: (e: Event) => any): void;
822 removeEventListener(event: string, listener: (e: Event) => any): void;
823 dispatchEvent(event: Event): void;
824 }
825
826 interface SinonFakeXMLHttpRequest {
827 // Properties
828 /**
829 * The URL set on the request object.
830 */
831 url: string;
832 /**
833 * The request method as a string.
834 */
835 method: string;
836 /**
837 * An object of all request headers, i.e.:
838 */
839 requestHeaders: any;
840 /**
841 * The request body
842 */
843 requestBody: string;
844 /**
845 * The request’s status code.
846 * undefined if the request has not been handled (see respond below)
847 */
848 status: number;
849 /**
850 * Only populated if the respond method is called (see below).
851 */
852 statusText: string;
853 /**
854 * Whether or not the request is asynchronous.
855 */
856 async: boolean;
857 /**
858 * Username, if any.
859 */
860 username: string;
861 /**
862 * Password, if any.
863 */
864 password: string;
865 withCredentials: boolean;
866 upload: SinonFakeUploadProgress;
867 /**
868 * When using respond, this property is populated with a parsed document if response headers indicate as much (see the spec)
869 */
870 responseXML: Document;
871 /**
872 * The value of the given response header, if the request has been responded to (see respond).
873 * @param header
874 */
875 getResponseHeader(header: string): string;
876 /**
877 * All response headers as an object.
878 */
879 getAllResponseHeaders(): any;
880
881 // Methods
882 /**
883 * Sets response headers (e.g. { "Content-Type": "text/html", ... }, updates the readyState property and fires onreadystatechange.
884 * @param headers
885 */
886 setResponseHeaders(headers: any): void;
887 /**
888 * Sets the respond body, updates the readyState property and fires onreadystatechange.
889 * Additionally, populates responseXML with a parsed document if response headers indicate as much.
890 */
891 setResponseBody(body: string): void;
892 /**
893 * Calls the above three methods.
894 */
895 respond(status: number, headers: any, body: string): void;
896 autoRespond(ms: number): void;
897 /**
898 * Simulates a network error on the request. The onerror handler will be called and the status will be 0.
899 */
900 error(): void;
901 onerror(): void;
902 }
903
904 interface SinonFakeXMLHttpRequestStatic {
905 new(): SinonFakeXMLHttpRequest;
906 /**
907 * Default false.
908 * When set to true, Sinon will check added filters if certain requests should be “unfaked”
909 */
910 useFilters: boolean;
911 /**
912 * Add a filter that will decide whether or not to fake a request.
913 * The filter will be called when xhr.open is called, with the exact same arguments (method, url, async, username, password).
914 * If the filter returns true, the request will not be faked.
915 * @param filter
916 */
917 addFilter(filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean): void;
918 /**
919 * By assigning a function to the onCreate property of the returned object from useFakeXMLHttpRequest()
920 * you can subscribe to newly created FakeXMLHttpRequest objects. See below for the fake xhr object API.
921 * Using this observer means you can still reach objects created by e.g. jQuery.ajax (or other abstractions/frameworks).
922 * @param xhr
923 */
924 onCreate(xhr: SinonFakeXMLHttpRequest): void;
925 /**
926 * Restore original function(s).
927 */
928 restore(): void;
929 }
930
931 interface SinonFakeServer extends SinonFakeServerOptions {
932 // Properties
933 /**
934 * Used internally to determine the HTTP method used with the provided request.
935 * By default this method simply returns request.method.
936 * When server.fakeHTTPMethods is true, the method will return the value of the _method parameter if the method is “POST”.
937 * This method can be overridden to provide custom behavior.
938 * @param request
939 */
940 getHTTPMethod(request: SinonFakeXMLHttpRequest): string;
941 /**
942 * You can inspect the server.requests to verify request ordering, find unmatched requests or check that no requests has been done.
943 * server.requests is an array of all the FakeXMLHttpRequest objects that have been created.
944 */
945 requests: SinonFakeXMLHttpRequest[];
946
947 // Methods
948 /**
949 * Causes the server to respond to any request not matched by another response with the provided data. The default catch-all response is [404, {}, ""].
950 * A String representing the response body
951 * An Array with status, headers and response body, e.g. [200, { "Content-Type": "text/html", "Content-Length": 2 }, "OK"]
952 * A Function.
953 * Default status is 200 and default headers are none.
954 * When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.
955 * @param body A String representing the response body
956 */
957 respondWith(body: string): void;
958 /**
959 * Causes the server to respond to any request not matched by another response with the provided data. The default catch-all response is [404, {}, ""].
960 * Default status is 200 and default headers are none.
961 * When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.
962 * @param response An Array with status, headers and response body, e.g. [200, { "Content-Type": "text/html", "Content-Length": 2 }, "OK"]
963 */
964 respondWith(response: any[]): void;
965 /**
966 * Causes the server to respond to any request not matched by another response with the provided data. The default catch-all response is [404, {}, ""].
967 * Default status is 200 and default headers are none.
968 * When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.
969 * @param fn A Function.
970 */
971 respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
972 /**
973 * Responds to all requests to given URL, e.g. /posts/1.
974 */
975 respondWith(url: string, body: string): void;
976 /**
977 * Responds to all requests to given URL, e.g. /posts/1.
978 */
979 respondWith(url: string, response: any[]): void;
980 /**
981 * Responds to all requests to given URL, e.g. /posts/1.
982 */
983 respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
984 /**
985 * Responds to all method requests to the given URL with the given response.
986 * method is an HTTP verb.
987 */
988 respondWith(method: string, url: string, body: string): void;
989 /**
990 * Responds to all method requests to the given URL with the given response.
991 * method is an HTTP verb.
992 */
993 respondWith(method: string, url: string, response: any[]): void;
994 /**
995 * Responds to all method requests to the given URL with the given response.
996 * method is an HTTP verb.
997 */
998 respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
999 /**
1000 * URL may be a regular expression, e.g. /\\/post\\//\\d+
1001 * If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
1002 */
1003 respondWith(url: RegExp, body: string): void;
1004 /**
1005 * URL may be a regular expression, e.g. /\\/post\\//\\d+
1006 * If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
1007 */
1008 respondWith(url: RegExp, response: any[]): void;
1009 /**
1010 * URL may be a regular expression, e.g. /\\/post\\//\\d+
1011 * If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
1012 */
1013 respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
1014 /**
1015 * Responds to all method requests to URLs matching the regular expression.
1016 */
1017 respondWith(method: string, url: RegExp, body: string): void;
1018 /**
1019 * Responds to all method requests to URLs matching the regular expression.
1020 */
1021 respondWith(method: string, url: RegExp, response: any[]): void;
1022 /**
1023 * Responds to all method requests to URLs matching the regular expression.
1024 */
1025 respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
1026 /**
1027 * Causes all queued asynchronous requests to receive a response.
1028 * If none of the responses added through respondWith match, the default response is [404, {}, ""].
1029 * Synchronous requests are responded to immediately, so make sure to call respondWith upfront.
1030 * If called with arguments, respondWith will be called with those arguments before responding to requests.
1031 */
1032 respond(): void;
1033 restore(): void;
1034 }
1035
1036 interface SinonFakeServerOptions {
1037 /**
1038 * When set to true, causes the server to automatically respond to incoming requests after a timeout.
1039 * The default timeout is 10ms but you can control it through the autoRespondAfter property.
1040 * Note that this feature is intended to help during mockup development, and is not suitable for use in tests.
1041 */
1042 autoRespond: boolean;
1043 /**
1044 * When autoRespond is true, respond to requests after this number of milliseconds. Default is 10.
1045 */
1046 autoRespondAfter: number;
1047 /**
1048 * If set to true, server will find _method parameter in POST body and recognize that as the actual method.
1049 * Supports a pattern common to Ruby on Rails applications. For custom HTTP method faking, override server.getHTTPMethod(request).
1050 */
1051 fakeHTTPMethods: boolean;
1052 /**
1053 * If set, the server will respond to every request immediately and synchronously.
1054 * This is ideal for faking the server from within a test without having to call server.respond() after each request made in that test.
1055 * As this is synchronous and immediate, this is not suitable for simulating actual network latency in tests or mockups.
1056 * To simulate network latency with automatic responses, see server.autoRespond and server.autoRespondAfter.
1057 */
1058 respondImmediately: boolean;
1059 }
1060
1061 interface SinonFakeServerStatic {
1062 create(options?: Partial<SinonFakeServerOptions>): SinonFakeServer;
1063 }
1064
1065 interface SinonExposeOptions {
1066 prefix: string;
1067 includeFail: boolean;
1068 }
1069
1070 interface SinonAssert {
1071 // Properties
1072 /**
1073 * Defaults to AssertError.
1074 */
1075 failException: string;
1076 /**
1077 * Every assertion fails by calling this method.
1078 * By default it throws an error of type sinon.assert.failException.
1079 * If the test framework looks for assertion errors by checking for a specific exception, you can simply override the kind of exception thrown.
1080 * If that does not fit with your testing framework of choice, override the fail method to do the right thing.
1081 */
1082 fail(message?: string): void; // Overridable
1083 /**
1084 * Called every time assertion passes.
1085 * Default implementation does nothing.
1086 */
1087 pass(assertion: any): void; // Overridable
1088
1089 // Methods
1090 /**
1091 * Passes if spy was never called
1092 * @param spy
1093 */
1094 notCalled(spy: SinonSpy): void;
1095 /**
1096 * Passes if spy was called at least once.
1097 */
1098 called(spy: SinonSpy): void;
1099 /**
1100 * Passes if spy was called once and only once.
1101 */
1102 calledOnce(spy: SinonSpy): void;
1103 /**
1104 * Passes if spy was called exactly twice.
1105 */
1106 calledTwice(spy: SinonSpy): void;
1107 /**
1108 * Passes if spy was called exactly three times.
1109 */
1110 calledThrice(spy: SinonSpy): void;
1111 /**
1112 * Passes if spy was called exactly num times.
1113 */
1114 callCount(spy: SinonSpy, count: number): void;
1115 /**
1116 * Passes if provided spies were called in the specified order.
1117 * @param spies
1118 */
1119 callOrder(...spies: SinonSpy[]): void;
1120 /**
1121 * Passes if spy was ever called with obj as its this value.
1122 * It’s possible to assert on a dedicated spy call: sinon.assert.calledOn(spy.firstCall, arg1, arg2, ...);.
1123 */
1124 calledOn(spyOrSpyCall: SinonSpy | SinonSpyCall, obj: any): void;
1125 /**
1126 * Passes if spy was always called with obj as its this value.
1127 */
1128 alwaysCalledOn(spy: SinonSpy, obj: any): void;
1129 /**
1130 * Passes if spy was called with the provided arguments.
1131 * It’s possible to assert on a dedicated spy call: sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);.
1132 * @param spyOrSpyCall
1133 * @param args
1134 */
1135 calledWith(spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: any[]): void;
1136 /**
1137 * Passes if spy was always called with the provided arguments.
1138 * @param spy
1139 * @param args
1140 */
1141 alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;
1142 /**
1143 * Passes if spy was never called with the provided arguments.
1144 * @param spy
1145 * @param args
1146 */
1147 neverCalledWith(spy: SinonSpy, ...args: any[]): void;
1148 /**
1149 * Passes if spy was called with the provided arguments and no others.
1150 * It’s possible to assert on a dedicated spy call: sinon.assert.calledWithExactly(spy.getCall(1), arg1, arg2, ...);.
1151 * @param spyOrSpyCall
1152 * @param args
1153 */
1154 calledWithExactly(spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: any[]): void;
1155 /**
1156 * Passes if spy was always called with the provided arguments and no others.
1157 */
1158 alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;
1159 /**
1160 * Passes if spy was called with matching arguments.
1161 * This behaves the same way as sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
1162 * It’s possible to assert on a dedicated spy call: sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);.
1163 */
1164 calledWithMatch(spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: any[]): void;
1165 /**
1166 * Passes if spy was always called with matching arguments.
1167 * This behaves the same way as sinon.assert.alwaysCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
1168 */
1169 alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
1170 /**
1171 * Passes if spy was never called with matching arguments.
1172 * This behaves the same way as sinon.assert.neverCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
1173 * @param spy
1174 * @param args
1175 */
1176 neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
1177 /**
1178 * Passes if spy was called with the new operator.
1179 * It’s possible to assert on a dedicated spy call: sinon.assert.calledWithNew(spy.secondCall, arg1, arg2, ...);.
1180 * @param spyOrSpyCall
1181 */
1182 calledWithNew(spyOrSpyCall: SinonSpy | SinonSpyCall): void;
1183 /**
1184 * Passes if spy threw any exception.
1185 */
1186 threw(spyOrSpyCall: SinonSpy | SinonSpyCall): void;
1187 /**
1188 * Passes if spy threw the given exception.
1189 * The exception is an actual object.
1190 * It’s possible to assert on a dedicated spy call: sinon.assert.threw(spy.thirdCall, exception);.
1191 */
1192 threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: string): void;
1193 /**
1194 * Passes if spy threw the given exception.
1195 * The exception is a String denoting its type.
1196 * It’s possible to assert on a dedicated spy call: sinon.assert.threw(spy.thirdCall, exception);.
1197 */
1198 threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: any): void;
1199 /**
1200 * Like threw, only required for all calls to the spy.
1201 */
1202 alwaysThrew(spy: SinonSpy): void;
1203 /**
1204 * Like threw, only required for all calls to the spy.
1205 */
1206 alwaysThrew(spy: SinonSpy, exception: string): void;
1207 /**
1208 * Like threw, only required for all calls to the spy.
1209 */
1210 alwaysThrew(spy: SinonSpy, exception: any): void;
1211 /**
1212 * Uses sinon.match to test if the arguments can be considered a match.
1213 */
1214 match(actual: any, expected: any): void;
1215 /**
1216 * Exposes assertions into another object, to better integrate with the test framework.
1217 * For instance, JsTestDriver uses global assertions, and to make Sinon.JS assertions appear alongside them, you can do.
1218 * @example sinon.assert.expose(this);
1219 * This will give you assertCalled(spy),assertCallOrder(spy1, spy2, ...) and so on.
1220 * The method accepts an optional options object with two options.
1221 */
1222 expose(obj: any, options?: Partial<SinonExposeOptions>): void;
1223 }
1224
1225 interface SinonMatcher {
1226 /**
1227 * All matchers implement and and or. This allows to logically combine mutliple matchers.
1228 * The result is a new matchers that requires both (and) or one of the matchers (or) to return true.
1229 * @example var stringOrNumber = sinon.match.string.or(sinon.match.number);
1230 * var bookWithPages = sinon.match.instanceOf(Book).and(sinon.match.has("pages"));
1231 */
1232 and(expr: SinonMatcher): SinonMatcher;
1233 /**
1234 * All matchers implement and and or. This allows to logically combine mutliple matchers.
1235 * The result is a new matchers that requires both (and) or one of the matchers (or) to return true.
1236 * @example var stringOrNumber = sinon.match.string.or(sinon.match.number);
1237 * var bookWithPages = sinon.match.instanceOf(Book).and(sinon.match.has("pages"));
1238 */
1239 or(expr: SinonMatcher): SinonMatcher;
1240 test(val: any): boolean;
1241 }
1242
1243 interface SinonArrayMatcher extends SinonMatcher {
1244 /**
1245 * Requires an Array to be deep equal another one.
1246 */
1247 deepEquals(expected: any[]): SinonMatcher;
1248 /**
1249 * Requires an Array to start with the same values as another one.
1250 */
1251 startsWith(expected: any[]): SinonMatcher;
1252 /**
1253 * Requires an Array to end with the same values as another one.
1254 */
1255 endsWith(expected: any[]): SinonMatcher;
1256 /**
1257 * Requires an Array to contain each one of the values the given array has.
1258 */
1259 contains(expected: any[]): SinonMatcher;
1260 }
1261
1262 interface SimplifiedSet {
1263 has(el: any): boolean;
1264 }
1265
1266 interface SimplifiedMap extends SimplifiedSet {
1267 get(key: any): any;
1268 }
1269
1270 interface SinonMapMatcher extends SinonMatcher {
1271 /**
1272 * Requires a Map to be deep equal another one.
1273 */
1274 deepEquals(expected: SimplifiedMap): SinonMatcher;
1275 /**
1276 * Requires a Map to contain each one of the items the given map has.
1277 */
1278 contains(expected: SimplifiedMap): SinonMatcher;
1279 }
1280
1281 interface SinonSetMatcher extends SinonMatcher {
1282 /**
1283 * Requires a Set to be deep equal another one.
1284 */
1285 deepEquals(expected: SimplifiedSet): SinonMatcher;
1286 /**
1287 * Requires a Set to contain each one of the items the given set has.
1288 */
1289 contains(expected: SimplifiedSet): SinonMatcher;
1290 }
1291
1292 interface SinonMatch {
1293 /**
1294 * Requires the value to be == to the given number.
1295 */
1296 (value: number): SinonMatcher;
1297 /**
1298 * Requires the value to be a string and have the expectation as a substring.
1299 */
1300 (value: string): SinonMatcher;
1301 /**
1302 * Requires the value to be a string and match the given regular expression.
1303 */
1304 (expr: RegExp): SinonMatcher;
1305 /**
1306 * See custom matchers.
1307 */
1308 (callback: (value: any) => boolean, message?: string): SinonMatcher;
1309 /**
1310 * Requires the value to be not null or undefined and have at least the same properties as expectation.
1311 * This supports nested matchers.
1312 */
1313 (obj: object): SinonMatcher;
1314 /**
1315 * Matches anything.
1316 */
1317 any: SinonMatcher;
1318 /**
1319 * Requires the value to be defined.
1320 */
1321 defined: SinonMatcher;
1322 /**
1323 * Requires the value to be truthy.
1324 */
1325 truthy: SinonMatcher;
1326 /**
1327 * Requires the value to be falsy.
1328 */
1329 falsy: SinonMatcher;
1330 /**
1331 * Requires the value to be a Boolean
1332 */
1333 bool: SinonMatcher;
1334 /**
1335 * Requires the value to be a Number.
1336 */
1337 number: SinonMatcher;
1338 /**
1339 * Requires the value to be a String.
1340 */
1341 string: SinonMatcher;
1342 /**
1343 * Requires the value to be an Object.
1344 */
1345 object: SinonMatcher;
1346 /**
1347 * Requires the value to be a Function.
1348 */
1349 func: SinonMatcher;
1350 /**
1351 * Requires the value to be a Map.
1352 */
1353 map: SinonMapMatcher;
1354 /**
1355 * Requires the value to be a Set.
1356 */
1357 set: SinonSetMatcher;
1358 /**
1359 * Requires the value to be an Array.
1360 */
1361 array: SinonArrayMatcher;
1362 /**
1363 * Requires the value to be a regular expression.
1364 */
1365 regexp: SinonMatcher;
1366 /**
1367 * Requires the value to be a Date object.
1368 */
1369 date: SinonMatcher;
1370 /**
1371 * Requires the value to be a Symbol.
1372 */
1373 symbol: SinonMatcher;
1374 /**
1375 * Requires the value to be in the specified array.
1376 */
1377 in(allowed: any[]): SinonMatcher;
1378 /**
1379 * Requires the value to strictly equal ref.
1380 */
1381 same(obj: any): SinonMatcher;
1382 /**
1383 * Requires the value to be of the given type, where type can be one of "undefined", "null", "boolean", "number", "string", "object", "function", "array", "regexp", "date" or "symbol".
1384 */
1385 typeOf(type: string): SinonMatcher;
1386 /**
1387 * Requires the value to be an instance of the given type.
1388 */
1389 instanceOf(type: any): SinonMatcher;
1390 /**
1391 * Requires the value to define the given property.
1392 * The property might be inherited via the prototype chain.
1393 * If the optional expectation is given, the value of the property is deeply compared with the expectation.
1394 * The expectation can be another matcher.
1395 * @param property
1396 * @param expect
1397 */
1398 has(property: string, expect?: any): SinonMatcher;
1399 /**
1400 * Same as sinon.match.has but the property must be defined by the value itself. Inherited properties are ignored.
1401 * @param property
1402 * @param expect
1403 */
1404 hasOwn(property: string, expect?: any): SinonMatcher;
1405 /**
1406 * Requires the value to define the given propertyPath. Dot (prop.prop) and bracket (prop[0]) notations are supported as in Lodash.get.
1407 * The propertyPath might be inherited via the prototype chain.
1408 * If the optional expectation is given, the value at the propertyPath is deeply compared with the expectation.
1409 * The expectation can be another matcher.
1410 */
1411 hasNested(path: string, expect?: any): SinonMatcher;
1412 /**
1413 * Requires every element of an Array, Set or Map, or alternatively every value of an Object to match the given matcher.
1414 */
1415 every(matcher: SinonMatcher): SinonMatcher;
1416 /**
1417 * Requires any element of an Array, Set or Map, or alternatively any value of an Object to match the given matcher.
1418 */
1419 some(matcher: SinonMatcher): SinonMatcher;
1420 }
1421
1422 interface SinonSandboxConfig {
1423 /**
1424 * The sandbox’s methods can be injected into another object for convenience.
1425 * The injectInto configuration option can name an object to add properties to.
1426 */
1427 injectInto: object | null;
1428 /**
1429 * What properties to inject.
1430 * Note that simply naming “server” here is not sufficient to have a server property show up in the target object,
1431 * you also have to set useFakeServer to true.
1432 */
1433 properties: string[];
1434 /**
1435 * If set to true, the sandbox will have a clock property.
1436 * You can optionally pass in a configuration object that follows the specification for fake timers, such as { toFake: ["setTimeout", "setInterval"] }.
1437 */
1438 useFakeTimers: boolean | Partial<SinonFakeTimersConfig>;
1439 /**
1440 * If true, server and requests properties are added to the sandbox. Can also be an object to use for fake server.
1441 * The default one is sinon.fakeServer, but if you’re using jQuery 1.3.x or some other library that does not set the XHR’s onreadystatechange handler,
1442 * you might want to do:
1443 */
1444 useFakeServer: boolean | SinonFakeServer;
1445 }
1446
1447 /**
1448 * Stubbed type of an object with members replaced by stubs.
1449 *
1450 * @template TType Type being stubbed.
1451 */
1452 type StubbableType<TType> = Function & { prototype: TType };
1453
1454 /**
1455 * An instance of a stubbed object type with functions replaced by stubs.
1456 *
1457 * @template TType Object type being stubbed.
1458 */
1459 type SinonStubbedInstance<TType> = {
1460 [P in keyof TType]: SinonStubbedMember<TType[P]>;
1461 };
1462
1463 /**
1464 * Replaces a type with a Sinon stub if it's a function.
1465 */
1466 type SinonStubbedMember<T> = T extends Function ? SinonStub : T;
1467
1468 interface SinonFake {
1469 /**
1470 * Creates a basic fake, with no behavior
1471 */
1472 (): SinonSpy;
1473 /**
1474 * Wraps an existing Function to record all interactions, while leaving it up to the func to provide the behavior.
1475 * This is useful when complex behavior not covered by the sinon.fake.* methods is required or when wrapping an existing function or method.
1476 */
1477 (fn: Function): SinonSpy;
1478 /**
1479 * Creates a fake that returns the val argument
1480 * @param val Returned value
1481 */
1482 returns(val: any): SinonSpy;
1483 /**
1484 * Creates a fake that throws an Error with the provided value as the message property.
1485 * If an Error is passed as the val argument, then that will be the thrown value. If any other value is passed, then that will be used for the message property of the thrown Error.
1486 * @param val Returned value or throw value if an Error
1487 */
1488 throws(val: Error | string): SinonSpy;
1489 /**
1490 * Creates a fake that returns a resolved Promise for the passed value.
1491 * @param val Resolved promise
1492 */
1493 resolves(val: any): SinonSpy;
1494 /**
1495 * Creates a fake that returns a rejected Promise for the passed value.
1496 * If an Error is passed as the value argument, then that will be the value of the promise.
1497 * If any other value is passed, then that will be used for the message property of the Error returned by the promise.
1498 * @param val Rejected promise
1499 */
1500 rejects(val: any): SinonSpy;
1501 /**
1502 * fake expects the last argument to be a callback and will invoke it with the given arguments.
1503 */
1504 yields(...args: any[]): SinonSpy;
1505 /**
1506 * fake expects the last argument to be a callback and will invoke it asynchronously with the given arguments.
1507 */
1508 yieldsAsync(...args: any[]): SinonSpy;
1509 }
1510
1511 interface SinonSandbox {
1512 /**
1513 * A convenience reference for sinon.assert
1514 * Since sinon@2.0.0
1515 */
1516 assert: SinonAssert;
1517 clock: SinonFakeTimers;
1518 requests: SinonFakeXMLHttpRequest[];
1519 server: SinonFakeServer;
1520 /**
1521 * Works exactly like sinon.spy
1522 */
1523 spy: SinonSpyStatic;
1524 /**
1525 * Works exactly like sinon.stub.
1526 */
1527 stub: SinonStubStatic;
1528 /**
1529 * Works exactly like sinon.mock
1530 */
1531 mock: SinonMockStatic;
1532
1533 /**
1534 * * No param : Causes Sinon to replace the global setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, process.hrtime, performance.now(when available)
1535 * and Date with a custom implementation which is bound to the returned clock object.
1536 * Starts the clock at the UNIX epoch (timestamp of 0).
1537 * * Now : As above, but rather than starting the clock with a timestamp of 0, start at the provided timestamp now.
1538 * Since sinon@2.0.0
1539 * You can also pass in a Date object, and its getTime() will be used for the starting timestamp.
1540 * * Config : As above, but allows further configuration options, some of which are:
1541 * * config.now - Number/Date - installs lolex with the specified unix epoch (default: 0)
1542 * * config.toFake - String[ ] - an array with explicit function names to fake.
1543 * By default lolex will automatically fake all methods except process.nextTick. You could, however, still fake nextTick by providing it explicitly
1544 * * config.shouldAdvanceTime - Boolean - tells lolex to increment mocked time automatically based on the real system time shift (default: false)
1545 * * Please visit the lolex.install documentation for the full feature set.
1546 * * Important note: when faking nextTick, normal calls to process.nextTick() would not execute automatically as they would during normal event-loop phases.
1547 * You would have to call either clock.next(), clock.tick(), clock.runAll() or clock.runToLast() (see example below). Please refer to the lolex documentation for more information.
1548 * @param config
1549 */
1550 useFakeTimers(config?: number | Date | Partial<SinonFakeTimersConfig>): SinonFakeTimers;
1551 /**
1552 * Causes Sinon to replace the native XMLHttpRequest object in browsers that support it with a custom implementation which does not send actual requests.
1553 * In browsers that support ActiveXObject, this constructor is replaced, and fake objects are returned for XMLHTTP progIds.
1554 * Other progIds, such as XMLDOM are left untouched.
1555 * The native XMLHttpRequest object will be available at sinon.xhr.XMLHttpRequest
1556 */
1557 useFakeXMLHttpRequest(): SinonFakeXMLHttpRequestStatic;
1558 /**
1559 * Fakes XHR and binds a server object to the sandbox such that it too is restored when calling sandbox.restore().
1560 * Access requests through sandbox.requests and server through sandbox.server
1561 */
1562 useFakeServer(): SinonFakeServer;
1563 /**
1564 * Restores all fakes created through sandbox.
1565 */
1566 restore(): void;
1567 /**
1568 * Resets the internal state of all fakes created through sandbox.
1569 */
1570 reset(): void;
1571 /**
1572 * Resets the history of all stubs created through the sandbox.
1573 * Since sinon@2.0.0
1574 */
1575 resetHistory(): void;
1576 /**
1577 * Resets the behaviour of all stubs created through the sandbox.
1578 * Since sinon@2.0.0
1579 */
1580 resetBehavior(): void;
1581 /**
1582 * Causes all stubs created from the sandbox to return promises using a specific Promise library instead of the global one when using stub.rejects or stub.resolves.
1583 * Returns the stub to allow chaining.
1584 * Since sinon@2.0.0
1585 */
1586 usingPromise(promiseLibrary: any): SinonSandbox;
1587 /**
1588 * Verifies all mocks created through the sandbox.
1589 */
1590 verify(): void;
1591 /**
1592 * Verifies all mocks and restores all fakes created through the sandbox.
1593 */
1594 verifyAndRestore(): void;
1595
1596 /**
1597 * Replaces property on object with replacement argument. Attempts to replace an already replaced value cause an exception.
1598 * replacement can be any value, including spies, stubs and fakes.
1599 * This method only works on non-accessor properties, for replacing accessors, use sandbox.replaceGetter() and sandbox.replaceSetter().
1600 */
1601 replace<T, TKey extends keyof T>(
1602 obj: T,
1603 prop: TKey,
1604 replacement: T[TKey]): T[TKey];
1605 /**
1606 * Replaces getter for property on object with replacement argument. Attempts to replace an already replaced getter cause an exception.
1607 * replacement must be a Function, and can be instances of spies, stubs and fakes.
1608 * @param obj
1609 * @param prop
1610 * @param replacement
1611 */
1612 replaceGetter<T, TKey extends keyof T>(
1613 obj: T,
1614 prop: TKey,
1615 replacement: () => T[TKey]): () => T[TKey];
1616 /**
1617 * Replaces setter for property on object with replacement argument. Attempts to replace an already replaced setter cause an exception.
1618 * replacement must be a Function, and can be instances of spies, stubs and fakes.
1619 * @param obj
1620 * @param prop
1621 * @param replacement
1622 */
1623 replaceSetter<T, TKey extends keyof T>(
1624 obj: T,
1625 prop: TKey,
1626 replacement: (val: T[TKey]) => void): (val: T[TKey]) => void;
1627
1628 /**
1629 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
1630 *
1631 * @template TType Type being stubbed.
1632 * @param constructor Object or class to stub.
1633 * @param overrides An optional map overriding created stubs
1634 * @returns A stubbed version of the constructor.
1635 * @remarks The given constructor function is not invoked. See also the stub API.
1636 */
1637 createStubInstance<TType>(
1638 constructor: StubbableType<TType>,
1639 overrides?: { [K in keyof TType]?: any }
1640 ): SinonStubbedInstance<TType>;
1641 }
1642
1643 interface SinonApi {
1644 fake: SinonFake;
1645 match: SinonMatch;
1646 spyCall(...args: any[]): SinonSpyCall;
1647 expectation: SinonExpectationStatic;
1648
1649 clock: {
1650 create(now: number | Date): SinonFakeTimers;
1651 };
1652
1653 FakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
1654
1655 fakeServer: SinonFakeServerStatic;
1656 fakeServerWithClock: SinonFakeServerStatic;
1657
1658 /**
1659 * Creates a new sandbox object with spies, stubs, and mocks.
1660 * @param config
1661 */
1662 createSandbox(config?: Partial<SinonSandboxConfig>): SinonSandbox;
1663 defaultConfig: Partial<SinonSandboxConfig>;
1664 }
1665
1666 interface LegacySandbox {
1667 sandbox: {
1668 /**
1669 * @deprecated Since 5.0, use `sinon.createSandbox` instead
1670 */
1671 create(config?: Partial<SinonSandboxConfig>): SinonSandbox;
1672 };
1673 }
1674
1675 type SinonStatic = SinonSandbox & LegacySandbox & SinonApi;
1676}
1677
1678declare const Sinon: Sinon.SinonStatic;
1679
1680export = Sinon;
1681export as namespace sinon;