UNPKG

20.5 kBTypeScriptView Raw
1// Type definitions for Sinon 2.3
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 reset(): void;
208 Date(): Date;
209 Date(year: number): Date;
210 Date(year: number, month: number): Date;
211 Date(year: number, month: number, day: number): Date;
212 Date(year: number, month: number, day: number, hour: number): Date;
213 Date(year: number, month: number, day: number, hour: number, minute: number): Date;
214 Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date;
215 Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date;
216 restore(): void;
217
218 /**
219 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
220 * without affecting timers, intervals or immediates.
221 * @param now The new 'now' in unix milliseconds
222 */
223 setSystemTime(now: number): void;
224 /**
225 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
226 * without affecting timers, intervals or immediates.
227 * @param now The new 'now' as a JavaScript Date
228 */
229 setSystemTime(date: Date): void;
230 }
231
232 interface SinonFakeTimersStatic {
233 (): SinonFakeTimers;
234 (...timers: string[]): SinonFakeTimers;
235 (now: number, ...timers: string[]): SinonFakeTimers;
236 }
237
238 interface SinonStatic {
239 useFakeTimers: SinonFakeTimersStatic;
240 clock: SinonFakeTimers;
241 }
242
243 interface SinonFakeUploadProgress {
244 eventListeners: {
245 progress: any[];
246 load: any[];
247 abort: any[];
248 error: any[];
249 };
250
251 addEventListener(event: string, listener: (e: Event) => any): void;
252 removeEventListener(event: string, listener: (e: Event) => any): void;
253 dispatchEvent(event: Event): void;
254 }
255
256 interface SinonFakeXMLHttpRequest {
257 // Properties
258 onCreate(xhr: SinonFakeXMLHttpRequest): void;
259 url: string;
260 method: string;
261 requestHeaders: any;
262 requestBody: string;
263 status: number;
264 statusText: string;
265 async: boolean;
266 username: string;
267 password: string;
268 withCredentials: boolean;
269 upload: SinonFakeUploadProgress;
270 responseXML: Document;
271 getResponseHeader(header: string): string;
272 getAllResponseHeaders(): any;
273
274 // Methods
275 restore(): void;
276 useFilters: boolean;
277 addFilter(filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean): void;
278 setResponseHeaders(headers: any): void;
279 setResponseBody(body: string): void;
280 respond(status: number, headers: any, body: string): void;
281 autoRespond(ms: number): void;
282 error(): void;
283 onerror(): void;
284 }
285
286 type SinonFakeXMLHttpRequestStatic = () => SinonFakeXMLHttpRequest;
287
288 interface SinonStatic {
289 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
290 FakeXMLHttpRequest: SinonFakeXMLHttpRequest;
291 }
292
293 interface SinonFakeServer {
294 // Properties
295 autoRespond: boolean;
296 autoRespondAfter: number;
297 fakeHTTPMethods: boolean;
298 getHTTPMethod(request: SinonFakeXMLHttpRequest): string;
299 requests: SinonFakeXMLHttpRequest[];
300 respondImmediately: boolean;
301
302 // Methods
303 respondWith(body: string): void;
304 respondWith(response: any[]): void;
305 respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
306 respondWith(url: string, body: string): void;
307 respondWith(url: string, response: any[]): void;
308 respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
309 respondWith(method: string, url: string, body: string): void;
310 respondWith(method: string, url: string, response: any[]): void;
311 respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
312 respondWith(url: RegExp, body: string): void;
313 respondWith(url: RegExp, response: any[]): void;
314 respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
315 respondWith(method: string, url: RegExp, body: string): void;
316 respondWith(method: string, url: RegExp, response: any[]): void;
317 respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
318 respond(): void;
319 restore(): void;
320 }
321
322 interface SinonFakeServerStatic {
323 create(): SinonFakeServer;
324 }
325
326 interface SinonStatic {
327 fakeServer: SinonFakeServerStatic;
328 fakeServerWithClock: SinonFakeServerStatic;
329 }
330
331 interface SinonExposeOptions {
332 prefix?: string;
333 includeFail?: boolean;
334 }
335
336 interface SinonAssert {
337 // Properties
338 failException: string;
339 fail(message?: string): void; // Overridable
340 pass(assertion: any): void; // Overridable
341
342 // Methods
343 notCalled(spy: SinonSpy): void;
344 called(spy: SinonSpy): void;
345 calledOnce(spy: SinonSpy): void;
346 calledTwice(spy: SinonSpy): void;
347 calledThrice(spy: SinonSpy): void;
348 callCount(spy: SinonSpy, count: number): void;
349 callOrder(...spies: SinonSpy[]): void;
350 calledOn(spy: SinonSpy, obj: any): void;
351 alwaysCalledOn(spy: SinonSpy, obj: any): void;
352 calledWith(spy: SinonSpy, ...args: any[]): void;
353 alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;
354 neverCalledWith(spy: SinonSpy, ...args: any[]): void;
355 calledWithExactly(spy: SinonSpy, ...args: any[]): void;
356 alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;
357 calledWithMatch(spy: SinonSpy, ...args: any[]): void;
358 alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
359 neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
360 threw(spy: SinonSpy): void;
361 threw(spy: SinonSpy, exception: string): void;
362 threw(spy: SinonSpy, exception: any): void;
363 alwaysThrew(spy: SinonSpy): void;
364 alwaysThrew(spy: SinonSpy, exception: string): void;
365 alwaysThrew(spy: SinonSpy, exception: any): void;
366 match(actual: any, expected: any): void;
367 expose(obj: any, options?: SinonExposeOptions): void;
368 }
369
370 interface SinonStatic {
371 assert: SinonAssert;
372 }
373
374 interface SinonMatcher {
375 and(expr: SinonMatcher): SinonMatcher;
376 or(expr: SinonMatcher): SinonMatcher;
377 }
378
379 interface SinonArrayMatcher extends SinonMatcher {
380 /**
381 * Requires an Array to be deep equal another one.
382 */
383 deepEquals(expected: any[]): SinonMatcher;
384 /**
385 * Requires an Array to start with the same values as another one.
386 */
387 startsWith(expected: any[]): SinonMatcher;
388 /**
389 * Requires an Array to end with the same values as another one.
390 */
391 endsWith(expected: any[]): SinonMatcher;
392 /**
393 * Requires an Array to contain each one of the values the given array has.
394 */
395 contains(expected: any[]): SinonMatcher;
396 }
397
398 interface SimplifiedSet {
399 has(el: any): boolean;
400 }
401
402 interface SimplifiedMap extends SimplifiedSet {
403 get(key: any): any;
404 }
405
406 interface SinonMapMatcher extends SinonMatcher {
407 /**
408 * Requires a Map to be deep equal another one.
409 */
410 deepEquals(expected: SimplifiedMap): SinonMatcher;
411 /**
412 * Requires a Map to contain each one of the items the given map has.
413 */
414 contains(expected: SimplifiedMap): SinonMatcher;
415 }
416
417 interface SinonSetMatcher extends SinonMatcher {
418 /**
419 * Requires a Set to be deep equal another one.
420 */
421 deepEquals(expected: SimplifiedSet): SinonMatcher;
422 /**
423 * Requires a Set to contain each one of the items the given set has.
424 */
425 contains(expected: SimplifiedSet): SinonMatcher;
426 }
427
428 interface SinonMatch {
429 (value: number): SinonMatcher;
430 (value: string): SinonMatcher;
431 (expr: RegExp): SinonMatcher;
432 (obj: any): SinonMatcher;
433 (callback: (value: any) => boolean, message?: string): SinonMatcher;
434 any: SinonMatcher;
435 defined: SinonMatcher;
436 truthy: SinonMatcher;
437 falsy: SinonMatcher;
438 bool: SinonMatcher;
439 number: SinonMatcher;
440 string: SinonMatcher;
441 object: SinonMatcher;
442 func: SinonMatcher;
443 /**
444 * Requires the value to be a Map.
445 */
446 map: SinonMapMatcher;
447 /**
448 * Requires the value to be a Set.
449 */
450 set: SinonSetMatcher;
451 /**
452 * Requires the value to be an Array.
453 */
454 array: SinonArrayMatcher;
455 regexp: SinonMatcher;
456 date: SinonMatcher;
457 symbol: SinonMatcher;
458 same(obj: any): SinonMatcher;
459 typeOf(type: string): SinonMatcher;
460 instanceOf(type: any): SinonMatcher;
461 has(property: string, expect?: any): SinonMatcher;
462 hasOwn(property: string, expect?: any): SinonMatcher;
463 }
464
465 interface SinonStatic {
466 match: SinonMatch;
467 }
468
469 interface SinonSandboxConfig {
470 injectInto?: any;
471 properties?: string[];
472 useFakeTimers?: any;
473 useFakeServer?: any;
474 }
475
476 interface SinonSandbox {
477 assert: SinonAssert;
478 clock: SinonFakeTimers;
479 requests: SinonFakeXMLHttpRequest;
480 server: SinonFakeServer;
481 spy: SinonSpyStatic;
482 stub: SinonStubStatic;
483 mock: SinonMockStatic;
484 useFakeTimers: SinonFakeTimersStatic;
485 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
486 useFakeServer(): SinonFakeServer;
487 restore(): void;
488 reset(): void;
489 resetHistory(): void;
490 resetBehavior(): void;
491 usingPromise(promiseLibrary: any): SinonSandbox;
492 verify(): void;
493 verifyAndRestore(): void;
494 }
495
496 interface SinonSandboxStatic {
497 create(config?: SinonSandboxConfig): SinonSandbox;
498 }
499
500 interface SinonStatic {
501 createSandbox(config?: SinonSandboxConfig): SinonSandbox;
502 defaultConfig: SinonSandboxConfig;
503 sandbox: SinonSandboxStatic;
504 }
505
506 interface SinonTestConfig {
507 injectIntoThis?: boolean;
508 injectInto?: any;
509 properties?: string[];
510 useFakeTimers?: boolean;
511 useFakeServer?: boolean;
512 }
513
514 interface SinonTestWrapper extends SinonSandbox {
515 (...args: any[]): any;
516 }
517
518 // Utility overridables
519 interface SinonStatic {
520 /**
521 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
522 *
523 * @param constructor Object or class to stub.
524 * @returns A stubbed version of the constructor.
525 * @remarks The given constructor function is not invoked. See also the stub API.
526 */
527 createStubInstance(constructor: any): any;
528
529 /**
530 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
531 *
532 * @template TType Type being stubbed.
533 * @param constructor Object or class to stub.
534 * @returns A stubbed version of the constructor.
535 * @remarks The given constructor function is not invoked. See also the stub API.
536 */
537 createStubInstance<TType>(constructor: StubbableType<TType>): SinonStubbedInstance<TType>;
538
539 format(obj: any): string;
540 restore(object: any): void;
541 }
542
543 /**
544 * Stubbed type of an object with members replaced by stubs.
545 *
546 * @template TType Type being stubbed.
547 */
548 interface StubbableType<TType> {
549 new(...args: any[]): TType;
550 }
551
552 /**
553 * An instance of a stubbed object type with members replaced by stubs.
554 *
555 * @template TType Object type being stubbed.
556 */
557 type SinonStubbedInstance<TType> = {
558 [P in keyof TType]: SinonStub;
559 };
560}
561
562declare const Sinon: Sinon.SinonStatic;
563
564export = Sinon;
565export as namespace sinon;