UNPKG

20.6 kBTypeScriptView Raw
1// Type definitions for Sinon 4.0
2// Project: http://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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 2.3
10
11// sinon uses DOM dependencies which are absent in browser-less environment like node.js
12// to avoid compiler errors this monkey patch is used
13// see more details in https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11351
14// tslint:disable no-empty-interface
15interface Event { }
16interface Document { }
17// tslint:enable no-empty-interface
18
19declare namespace Sinon {
20 interface SinonSpyCallApi {
21 // Properties
22 thisValue: any;
23 args: any[];
24 exception: any;
25 returnValue: any;
26
27 // Methods
28 calledOn(obj: any): boolean;
29 calledWith(...args: any[]): boolean;
30 calledWithExactly(...args: any[]): boolean;
31 calledWithMatch(...args: any[]): boolean;
32 notCalledWith(...args: any[]): boolean;
33 notCalledWithMatch(...args: any[]): boolean;
34 returned(value: any): boolean;
35 threw(): boolean;
36 threw(type: string): boolean;
37 threw(obj: any): boolean;
38 callArg(pos: number): void;
39 callArgOn(pos: number, obj: any, ...args: any[]): void;
40 callArgWith(pos: number, ...args: any[]): void;
41 callArgOnWith(pos: number, obj: any, ...args: any[]): void;
42 yield(...args: any[]): void;
43 yieldOn(obj: any, ...args: any[]): void;
44 yieldTo(property: string, ...args: any[]): void;
45 yieldToOn(property: string, obj: any, ...args: any[]): void;
46 }
47
48 interface SinonSpyCall extends SinonSpyCallApi {
49 calledBefore(call: SinonSpyCall): boolean;
50 calledAfter(call: SinonSpyCall): boolean;
51 calledWithNew(call: SinonSpyCall): boolean;
52 }
53
54 interface SinonSpy extends SinonSpyCallApi {
55 // Properties
56 callCount: number;
57 called: boolean;
58 notCalled: boolean;
59 calledOnce: boolean;
60 calledTwice: boolean;
61 calledThrice: boolean;
62 firstCall: SinonSpyCall;
63 secondCall: SinonSpyCall;
64 thirdCall: SinonSpyCall;
65 lastCall: SinonSpyCall;
66 thisValues: any[];
67 args: any[][];
68 exceptions: any[];
69 returnValues: any[];
70
71 // Methods
72 (...args: any[]): any;
73 calledBefore(anotherSpy: SinonSpy): boolean;
74 calledAfter(anotherSpy: SinonSpy): boolean;
75 calledImmediatelyBefore(anotherSpy: SinonSpy): boolean;
76 calledImmediatelyAfter(anotherSpy: SinonSpy): boolean;
77 calledWithNew(): boolean;
78 withArgs(...args: any[]): SinonSpy;
79 alwaysCalledOn(obj: any): boolean;
80 alwaysCalledWith(...args: any[]): boolean;
81 alwaysCalledWithExactly(...args: any[]): boolean;
82 alwaysCalledWithMatch(...args: any[]): boolean;
83 neverCalledWith(...args: any[]): boolean;
84 neverCalledWithMatch(...args: any[]): boolean;
85 alwaysThrew(): boolean;
86 alwaysThrew(type: string): boolean;
87 alwaysThrew(obj: any): boolean;
88 alwaysReturned(obj: any): boolean;
89 invokeCallback(...args: any[]): void;
90 getCall(n: number): SinonSpyCall;
91 getCalls(): SinonSpyCall[];
92 reset(): void;
93 printf(format: string, ...args: any[]): string;
94 restore(): void;
95 }
96
97 interface SinonSpyStatic {
98 (): SinonSpy;
99 (func: Function): SinonSpy;
100 <T>(obj: T, method: keyof T): SinonSpy;
101 }
102
103 interface SinonStatic {
104 spy: SinonSpyStatic;
105 }
106
107 interface SinonStub extends SinonSpy {
108 resetBehavior(): void;
109 resetHistory(): void;
110 usingPromise(promiseLibrary: any): SinonStub;
111
112 returns(obj: any): SinonStub;
113 returnsArg(index: number): SinonStub;
114 returnsThis(): SinonStub;
115 resolves(value?: any): SinonStub;
116 throws(type?: string): SinonStub;
117 throws(obj: any): SinonStub;
118 throwsArg(index: number): SinonStub;
119 throwsException(type?: string): SinonStub;
120 throwsException(obj: any): SinonStub;
121 rejects(): SinonStub;
122 rejects(errorType: string): SinonStub;
123 rejects(value: any): SinonStub;
124 callsArg(index: number): SinonStub;
125 callThrough(): SinonStub;
126 callsArgOn(index: number, context: any): SinonStub;
127 callsArgWith(index: number, ...args: any[]): SinonStub;
128 callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub;
129 callsArgAsync(index: number): SinonStub;
130 callsArgOnAsync(index: number, context: any): SinonStub;
131 callsArgWithAsync(index: number, ...args: any[]): SinonStub;
132 callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub;
133 callsFake(func: (...args: any[]) => void): SinonStub;
134 get(func: () => any): SinonStub;
135 set(func: (v: any) => void): SinonStub;
136 onCall(n: number): SinonStub;
137 onFirstCall(): SinonStub;
138 onSecondCall(): SinonStub;
139 onThirdCall(): SinonStub;
140 value(val: any): SinonStub;
141 yields(...args: any[]): SinonStub;
142 yieldsOn(context: any, ...args: any[]): SinonStub;
143 yieldsRight(...args: any[]): SinonStub;
144 yieldsTo(property: string, ...args: any[]): SinonStub;
145 yieldsToOn(property: string, context: any, ...args: any[]): SinonStub;
146 yieldsAsync(...args: any[]): SinonStub;
147 yieldsOnAsync(context: any, ...args: any[]): SinonStub;
148 yieldsToAsync(property: string, ...args: any[]): SinonStub;
149 yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub;
150 withArgs(...args: any[]): SinonStub;
151 }
152
153 interface SinonStubStatic {
154 (): SinonStub;
155 (obj: any): SinonStub;
156 <T>(obj: T, method: keyof T): SinonStub;
157 <T>(obj: T, method: keyof T, func: Function): SinonStub;
158 }
159
160 interface SinonStatic {
161 stub: SinonStubStatic;
162 }
163
164 interface SinonExpectation extends SinonStub {
165 atLeast(n: number): SinonExpectation;
166 atMost(n: number): SinonExpectation;
167 never(): SinonExpectation;
168 once(): SinonExpectation;
169 twice(): SinonExpectation;
170 thrice(): SinonExpectation;
171 exactly(n: number): SinonExpectation;
172 withArgs(...args: any[]): SinonExpectation;
173 withExactArgs(...args: any[]): SinonExpectation;
174 on(obj: any): SinonExpectation;
175 verify(): SinonExpectation;
176 restore(): void;
177 }
178
179 interface SinonExpectationStatic {
180 create(methodName?: string): SinonExpectation;
181 }
182
183 interface SinonMock {
184 expects(method: string): SinonExpectation;
185 restore(): void;
186 verify(): void;
187 }
188
189 interface SinonMockStatic {
190 (): SinonExpectation;
191 (obj: any): SinonMock;
192 }
193
194 interface SinonStatic {
195 expectation: SinonExpectationStatic;
196 mock: SinonMockStatic;
197 }
198
199 interface SinonFakeTimers {
200 now: number;
201 create(now: number): SinonFakeTimers;
202 setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;
203 clearTimeout(id: number): void;
204 setInterval(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;
205 clearInterval(id: number): void;
206 tick(ms: number): number;
207 next(): void;
208 runAll(): void;
209 runToLast(): void;
210 reset(): void;
211 Date(): Date;
212 Date(year: number): Date;
213 Date(year: number, month: number): Date;
214 Date(year: number, month: number, day: number): Date;
215 Date(year: number, month: number, day: number, hour: number): Date;
216 Date(year: number, month: number, day: number, hour: number, minute: number): Date;
217 Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date;
218 Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date;
219 restore(): void;
220
221 /**
222 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
223 * without affecting timers, intervals or immediates.
224 * @param now The new 'now' in unix milliseconds
225 */
226 setSystemTime(now: number): void;
227 /**
228 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
229 * without affecting timers, intervals or immediates.
230 * @param now The new 'now' as a JavaScript Date
231 */
232 setSystemTime(date: Date): void;
233 }
234
235 interface SinonFakeTimersStatic {
236 (): SinonFakeTimers;
237 (...timers: string[]): SinonFakeTimers;
238 (now: number, ...timers: string[]): SinonFakeTimers;
239 }
240
241 interface SinonStatic {
242 useFakeTimers: SinonFakeTimersStatic;
243 clock: SinonFakeTimers;
244 }
245
246 interface SinonFakeUploadProgress {
247 eventListeners: {
248 progress: any[];
249 load: any[];
250 abort: any[];
251 error: any[];
252 };
253
254 addEventListener(event: string, listener: (e: Event) => any): void;
255 removeEventListener(event: string, listener: (e: Event) => any): void;
256 dispatchEvent(event: Event): void;
257 }
258
259 interface SinonFakeXMLHttpRequest {
260 // Properties
261 onCreate(xhr: SinonFakeXMLHttpRequest): void;
262 url: string;
263 method: string;
264 requestHeaders: any;
265 requestBody: string;
266 status: number;
267 statusText: string;
268 async: boolean;
269 username: string;
270 password: string;
271 withCredentials: boolean;
272 upload: SinonFakeUploadProgress;
273 responseXML: Document;
274 getResponseHeader(header: string): string;
275 getAllResponseHeaders(): any;
276
277 // Methods
278 restore(): void;
279 useFilters: boolean;
280 addFilter(filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean): void;
281 setResponseHeaders(headers: any): void;
282 setResponseBody(body: string): void;
283 respond(status: number, headers: any, body: string): void;
284 autoRespond(ms: number): void;
285 error(): void;
286 onerror(): void;
287 }
288
289 type SinonFakeXMLHttpRequestStatic = () => SinonFakeXMLHttpRequest;
290
291 interface SinonStatic {
292 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
293 FakeXMLHttpRequest: SinonFakeXMLHttpRequest;
294 }
295
296 interface SinonFakeServer {
297 // Properties
298 autoRespond: boolean;
299 autoRespondAfter: number;
300 fakeHTTPMethods: boolean;
301 getHTTPMethod(request: SinonFakeXMLHttpRequest): string;
302 requests: SinonFakeXMLHttpRequest[];
303 respondImmediately: boolean;
304
305 // Methods
306 respondWith(body: string): void;
307 respondWith(response: any[]): void;
308 respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
309 respondWith(url: string, body: string): void;
310 respondWith(url: string, response: any[]): void;
311 respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
312 respondWith(method: string, url: string, body: string): void;
313 respondWith(method: string, url: string, response: any[]): void;
314 respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
315 respondWith(url: RegExp, body: string): void;
316 respondWith(url: RegExp, response: any[]): void;
317 respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
318 respondWith(method: string, url: RegExp, body: string): void;
319 respondWith(method: string, url: RegExp, response: any[]): void;
320 respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
321 respond(): void;
322 restore(): void;
323 }
324
325 interface SinonFakeServerStatic {
326 create(): SinonFakeServer;
327 }
328
329 interface SinonStatic {
330 fakeServer: SinonFakeServerStatic;
331 fakeServerWithClock: SinonFakeServerStatic;
332 }
333
334 interface SinonExposeOptions {
335 prefix?: string;
336 includeFail?: boolean;
337 }
338
339 interface SinonAssert {
340 // Properties
341 failException: string;
342 fail(message?: string): void; // Overridable
343 pass(assertion: any): void; // Overridable
344
345 // Methods
346 notCalled(spy: SinonSpy): void;
347 called(spy: SinonSpy): void;
348 calledOnce(spy: SinonSpy): void;
349 calledTwice(spy: SinonSpy): void;
350 calledThrice(spy: SinonSpy): void;
351 callCount(spy: SinonSpy, count: number): void;
352 callOrder(...spies: SinonSpy[]): void;
353 calledOn(spy: SinonSpy, obj: any): void;
354 alwaysCalledOn(spy: SinonSpy, obj: any): void;
355 calledWith(spy: SinonSpy, ...args: any[]): void;
356 alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;
357 neverCalledWith(spy: SinonSpy, ...args: any[]): void;
358 calledWithExactly(spy: SinonSpy, ...args: any[]): void;
359 alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;
360 calledWithMatch(spy: SinonSpy, ...args: any[]): void;
361 alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
362 neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
363 threw(spy: SinonSpy): void;
364 threw(spy: SinonSpy, exception: string): void;
365 threw(spy: SinonSpy, exception: any): void;
366 alwaysThrew(spy: SinonSpy): void;
367 alwaysThrew(spy: SinonSpy, exception: string): void;
368 alwaysThrew(spy: SinonSpy, exception: any): void;
369 match(actual: any, expected: any): void;
370 expose(obj: any, options?: SinonExposeOptions): void;
371 }
372
373 interface SinonStatic {
374 assert: SinonAssert;
375 }
376
377 interface SinonMatcher {
378 and(expr: SinonMatcher): SinonMatcher;
379 or(expr: SinonMatcher): SinonMatcher;
380 }
381
382 interface SinonArrayMatcher extends SinonMatcher {
383 /**
384 * Requires an Array to be deep equal another one.
385 */
386 deepEquals(expected: any[]): SinonMatcher;
387 /**
388 * Requires an Array to start with the same values as another one.
389 */
390 startsWith(expected: any[]): SinonMatcher;
391 /**
392 * Requires an Array to end with the same values as another one.
393 */
394 endsWith(expected: any[]): SinonMatcher;
395 /**
396 * Requires an Array to contain each one of the values the given array has.
397 */
398 contains(expected: any[]): SinonMatcher;
399 }
400
401 interface SimplifiedSet {
402 has(el: any): boolean;
403 }
404
405 interface SimplifiedMap extends SimplifiedSet {
406 get(key: any): any;
407 }
408
409 interface SinonMapMatcher extends SinonMatcher {
410 /**
411 * Requires a Map to be deep equal another one.
412 */
413 deepEquals(expected: SimplifiedMap): SinonMatcher;
414 /**
415 * Requires a Map to contain each one of the items the given map has.
416 */
417 contains(expected: SimplifiedMap): SinonMatcher;
418 }
419
420 interface SinonSetMatcher extends SinonMatcher {
421 /**
422 * Requires a Set to be deep equal another one.
423 */
424 deepEquals(expected: SimplifiedSet): SinonMatcher;
425 /**
426 * Requires a Set to contain each one of the items the given set has.
427 */
428 contains(expected: SimplifiedSet): SinonMatcher;
429 }
430
431 interface SinonMatch {
432 (value: number): SinonMatcher;
433 (value: string): SinonMatcher;
434 (expr: RegExp): SinonMatcher;
435 (obj: any): SinonMatcher;
436 (callback: (value: any) => boolean, message?: string): SinonMatcher;
437 any: SinonMatcher;
438 defined: SinonMatcher;
439 truthy: SinonMatcher;
440 falsy: SinonMatcher;
441 bool: SinonMatcher;
442 number: SinonMatcher;
443 string: SinonMatcher;
444 object: SinonMatcher;
445 func: SinonMatcher;
446 /**
447 * Requires the value to be a Map.
448 */
449 map: SinonMapMatcher;
450 /**
451 * Requires the value to be a Set.
452 */
453 set: SinonSetMatcher;
454 /**
455 * Requires the value to be an Array.
456 */
457 array: SinonArrayMatcher;
458 regexp: SinonMatcher;
459 date: SinonMatcher;
460 symbol: SinonMatcher;
461 same(obj: any): SinonMatcher;
462 typeOf(type: string): SinonMatcher;
463 instanceOf(type: any): SinonMatcher;
464 has(property: string, expect?: any): SinonMatcher;
465 hasOwn(property: string, expect?: any): SinonMatcher;
466 }
467
468 interface SinonStatic {
469 match: SinonMatch;
470 }
471
472 interface SinonSandboxConfig {
473 injectInto?: any;
474 properties?: string[];
475 useFakeTimers?: any;
476 useFakeServer?: any;
477 }
478
479 interface SinonSandbox {
480 assert: SinonAssert;
481 clock: SinonFakeTimers;
482 requests: SinonFakeXMLHttpRequest;
483 server: SinonFakeServer;
484 spy: SinonSpyStatic;
485 stub: SinonStubStatic;
486 mock: SinonMockStatic;
487 useFakeTimers: SinonFakeTimersStatic;
488 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
489 useFakeServer(): SinonFakeServer;
490 restore(): void;
491 reset(): void;
492 resetHistory(): void;
493 resetBehavior(): void;
494 usingPromise(promiseLibrary: any): SinonSandbox;
495 verify(): void;
496 verifyAndRestore(): void;
497 }
498
499 interface SinonSandboxStatic {
500 create(config?: SinonSandboxConfig): SinonSandbox;
501 }
502
503 interface SinonStatic {
504 createSandbox(config?: SinonSandboxConfig): SinonSandbox;
505 defaultConfig: SinonSandboxConfig;
506 sandbox: SinonSandboxStatic;
507 }
508
509 interface SinonTestConfig {
510 injectIntoThis?: boolean;
511 injectInto?: any;
512 properties?: string[];
513 useFakeTimers?: boolean;
514 useFakeServer?: boolean;
515 }
516
517 interface SinonTestWrapper extends SinonSandbox {
518 (...args: any[]): any;
519 }
520
521 // Utility overridables
522 interface SinonStatic {
523 /**
524 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
525 *
526 * @param constructor Object or class to stub.
527 * @returns A stubbed version of the constructor.
528 * @remarks The given constructor function is not invoked. See also the stub API.
529 */
530 createStubInstance(constructor: any): any;
531
532 /**
533 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
534 *
535 * @template TType Type being stubbed.
536 * @param constructor Object or class to stub.
537 * @returns A stubbed version of the constructor.
538 * @remarks The given constructor function is not invoked. See also the stub API.
539 */
540 createStubInstance<TType>(constructor: StubbableType<TType>): SinonStubbedInstance<TType>;
541
542 format(obj: any): string;
543 restore(object: any): void;
544 }
545
546 /**
547 * Stubbed type of an object with members replaced by stubs.
548 *
549 * @template TType Type being stubbed.
550 */
551 interface StubbableType<TType> {
552 new(...args: any[]): TType;
553 }
554
555 /**
556 * An instance of a stubbed object type with members replaced by stubs.
557 *
558 * @template TType Object type being stubbed.
559 */
560 type SinonStubbedInstance<TType> = {
561 [P in keyof TType]: SinonStub;
562 };
563}
564
565declare const Sinon: Sinon.SinonStatic;
566
567export = Sinon;
568export as namespace sinon;