UNPKG

20.6 kBTypeScriptView Raw
1// Type definitions for Sinon 4.1
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
14interface Event { } // tslint:disable-line no-empty-interface
15interface Document { } // tslint:disable-line no-empty-interface
16
17declare namespace Sinon {
18 interface SinonSpyCallApi {
19 // Properties
20 thisValue: any;
21 args: any[];
22 exception: any;
23 returnValue: any;
24
25 // Methods
26 calledOn(obj: any): boolean;
27 calledWith(...args: any[]): boolean;
28 calledWithExactly(...args: any[]): boolean;
29 calledWithMatch(...args: any[]): boolean;
30 notCalledWith(...args: any[]): boolean;
31 notCalledWithMatch(...args: any[]): boolean;
32 returned(value: any): boolean;
33 threw(): boolean;
34 threw(type: string): boolean;
35 threw(obj: any): boolean;
36 callArg(pos: number): void;
37 callArgOn(pos: number, obj: any, ...args: any[]): void;
38 callArgWith(pos: number, ...args: any[]): void;
39 callArgOnWith(pos: number, obj: any, ...args: any[]): void;
40 yield(...args: any[]): void;
41 yieldOn(obj: any, ...args: any[]): void;
42 yieldTo(property: string, ...args: any[]): void;
43 yieldToOn(property: string, obj: any, ...args: any[]): void;
44 }
45
46 interface SinonSpyCall extends SinonSpyCallApi {
47 calledBefore(call: SinonSpyCall): boolean;
48 calledAfter(call: SinonSpyCall): boolean;
49 calledWithNew(call: SinonSpyCall): boolean;
50 }
51
52 interface SinonSpy extends SinonSpyCallApi {
53 // Properties
54 callCount: number;
55 called: boolean;
56 notCalled: boolean;
57 calledOnce: boolean;
58 calledTwice: boolean;
59 calledThrice: boolean;
60 firstCall: SinonSpyCall;
61 secondCall: SinonSpyCall;
62 thirdCall: SinonSpyCall;
63 lastCall: SinonSpyCall;
64 thisValues: any[];
65 args: any[][];
66 exceptions: any[];
67 returnValues: any[];
68
69 // Methods
70 (...args: any[]): any;
71 calledBefore(anotherSpy: SinonSpy): boolean;
72 calledAfter(anotherSpy: SinonSpy): boolean;
73 calledImmediatelyBefore(anotherSpy: SinonSpy): boolean;
74 calledImmediatelyAfter(anotherSpy: SinonSpy): boolean;
75 calledWithNew(): boolean;
76 withArgs(...args: any[]): SinonSpy;
77 alwaysCalledOn(obj: any): boolean;
78 alwaysCalledWith(...args: any[]): boolean;
79 alwaysCalledWithExactly(...args: any[]): boolean;
80 alwaysCalledWithMatch(...args: any[]): boolean;
81 neverCalledWith(...args: any[]): boolean;
82 neverCalledWithMatch(...args: any[]): boolean;
83 alwaysThrew(): boolean;
84 alwaysThrew(type: string): boolean;
85 alwaysThrew(obj: any): boolean;
86 alwaysReturned(obj: any): boolean;
87 invokeCallback(...args: any[]): void;
88 getCall(n: number): SinonSpyCall;
89 getCalls(): SinonSpyCall[];
90 /// @deprecated Use resetHistory() instead
91 reset(): void;
92 resetHistory(): 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 <T>(obj: T): SinonStubbedInstance<T>;
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 extends SinonFakeServerOptions {
297 // Properties
298 getHTTPMethod(request: SinonFakeXMLHttpRequest): string;
299 requests: SinonFakeXMLHttpRequest[];
300
301 // Methods
302 respondWith(body: string): void;
303 respondWith(response: any[]): void;
304 respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
305 respondWith(url: string, body: string): void;
306 respondWith(url: string, response: any[]): void;
307 respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
308 respondWith(method: string, url: string, body: string): void;
309 respondWith(method: string, url: string, response: any[]): void;
310 respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
311 respondWith(url: RegExp, body: string): void;
312 respondWith(url: RegExp, response: any[]): void;
313 respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
314 respondWith(method: string, url: RegExp, body: string): void;
315 respondWith(method: string, url: RegExp, response: any[]): void;
316 respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
317 respond(): void;
318 restore(): void;
319 }
320
321 interface SinonFakeServerOptions {
322 autoRespond?: boolean;
323 autoRespondAfter?: number;
324 fakeHTTPMethods?: boolean;
325 respondImmediately?: boolean;
326 }
327
328 interface SinonFakeServerStatic {
329 create(options?: SinonFakeServerOptions): SinonFakeServer;
330 }
331
332 interface SinonStatic {
333 fakeServer: SinonFakeServerStatic;
334 fakeServerWithClock: SinonFakeServerStatic;
335 }
336
337 interface SinonExposeOptions {
338 prefix?: string;
339 includeFail?: boolean;
340 }
341
342 interface SinonAssert {
343 // Properties
344 failException: string;
345 fail(message?: string): void; // Overridable
346 pass(assertion: any): void; // Overridable
347
348 // Methods
349 notCalled(spy: SinonSpy): void;
350 called(spy: SinonSpy): void;
351 calledOnce(spy: SinonSpy): void;
352 calledTwice(spy: SinonSpy): void;
353 calledThrice(spy: SinonSpy): void;
354 callCount(spy: SinonSpy, count: number): void;
355 callOrder(...spies: SinonSpy[]): void;
356 calledOn(spy: SinonSpy, obj: any): void;
357 alwaysCalledOn(spy: SinonSpy, obj: any): void;
358 calledWith(spy: SinonSpy, ...args: any[]): void;
359 alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;
360 neverCalledWith(spy: SinonSpy, ...args: any[]): void;
361 calledWithExactly(spy: SinonSpy, ...args: any[]): void;
362 alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;
363 calledWithMatch(spy: SinonSpy, ...args: any[]): void;
364 alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
365 neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
366 threw(spy: SinonSpy): void;
367 threw(spy: SinonSpy, exception: string): void;
368 threw(spy: SinonSpy, exception: any): void;
369 alwaysThrew(spy: SinonSpy): void;
370 alwaysThrew(spy: SinonSpy, exception: string): void;
371 alwaysThrew(spy: SinonSpy, exception: any): void;
372 match(actual: any, expected: any): void;
373 expose(obj: any, options?: SinonExposeOptions): void;
374 }
375
376 interface SinonStatic {
377 assert: SinonAssert;
378 }
379
380 interface SinonMatcher {
381 and(expr: SinonMatcher): SinonMatcher;
382 or(expr: SinonMatcher): SinonMatcher;
383 }
384
385 interface SinonArrayMatcher extends SinonMatcher {
386 /**
387 * Requires an Array to be deep equal another one.
388 */
389 deepEquals(expected: any[]): SinonMatcher;
390 /**
391 * Requires an Array to start with the same values as another one.
392 */
393 startsWith(expected: any[]): SinonMatcher;
394 /**
395 * Requires an Array to end with the same values as another one.
396 */
397 endsWith(expected: any[]): SinonMatcher;
398 /**
399 * Requires an Array to contain each one of the values the given array has.
400 */
401 contains(expected: any[]): SinonMatcher;
402 }
403
404 interface SimplifiedSet {
405 has(el: any): boolean;
406 }
407
408 interface SimplifiedMap extends SimplifiedSet {
409 get(key: any): any;
410 }
411
412 interface SinonMapMatcher extends SinonMatcher {
413 /**
414 * Requires a Map to be deep equal another one.
415 */
416 deepEquals(expected: SimplifiedMap): SinonMatcher;
417 /**
418 * Requires a Map to contain each one of the items the given map has.
419 */
420 contains(expected: SimplifiedMap): SinonMatcher;
421 }
422
423 interface SinonSetMatcher extends SinonMatcher {
424 /**
425 * Requires a Set to be deep equal another one.
426 */
427 deepEquals(expected: SimplifiedSet): SinonMatcher;
428 /**
429 * Requires a Set to contain each one of the items the given set has.
430 */
431 contains(expected: SimplifiedSet): SinonMatcher;
432 }
433
434 interface SinonMatch {
435 (value: number): SinonMatcher;
436 (value: string): SinonMatcher;
437 (expr: RegExp): SinonMatcher;
438 (obj: any): SinonMatcher;
439 (callback: (value: any) => boolean, message?: string): SinonMatcher;
440 any: SinonMatcher;
441 defined: SinonMatcher;
442 truthy: SinonMatcher;
443 falsy: SinonMatcher;
444 bool: SinonMatcher;
445 number: SinonMatcher;
446 string: SinonMatcher;
447 object: SinonMatcher;
448 func: SinonMatcher;
449 /**
450 * Requires the value to be a Map.
451 */
452 map: SinonMapMatcher;
453 /**
454 * Requires the value to be a Set.
455 */
456 set: SinonSetMatcher;
457 /**
458 * Requires the value to be an Array.
459 */
460 array: SinonArrayMatcher;
461 regexp: SinonMatcher;
462 date: SinonMatcher;
463 symbol: SinonMatcher;
464 same(obj: any): SinonMatcher;
465 typeOf(type: string): SinonMatcher;
466 instanceOf(type: any): SinonMatcher;
467 has(property: string, expect?: any): SinonMatcher;
468 hasOwn(property: string, expect?: any): SinonMatcher;
469 }
470
471 interface SinonStatic {
472 match: SinonMatch;
473 }
474
475 interface SinonSandboxConfig {
476 injectInto?: any;
477 properties?: string[];
478 useFakeTimers?: any;
479 useFakeServer?: any;
480 }
481
482 interface SinonSandbox {
483 assert: SinonAssert;
484 clock: SinonFakeTimers;
485 requests: SinonFakeXMLHttpRequest;
486 server: SinonFakeServer;
487 spy: SinonSpyStatic;
488 stub: SinonStubStatic;
489 mock: SinonMockStatic;
490 useFakeTimers: SinonFakeTimersStatic;
491 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
492 useFakeServer(): SinonFakeServer;
493 restore(): void;
494 reset(): void;
495 resetHistory(): void;
496 resetBehavior(): void;
497 usingPromise(promiseLibrary: any): SinonSandbox;
498 verify(): void;
499 verifyAndRestore(): void;
500 createStubInstance(constructor: any): any;
501 createStubInstance<TType>(constructor: StubbableType<TType>): SinonStubbedInstance<TType>;
502 }
503
504 interface SinonSandboxStatic {
505 create(config?: SinonSandboxConfig): SinonSandbox;
506 }
507
508 interface SinonStatic {
509 createSandbox(config?: SinonSandboxConfig): SinonSandbox;
510 defaultConfig: SinonSandboxConfig;
511 sandbox: SinonSandboxStatic;
512 }
513
514 interface SinonTestConfig {
515 injectIntoThis?: boolean;
516 injectInto?: any;
517 properties?: string[];
518 useFakeTimers?: boolean;
519 useFakeServer?: boolean;
520 }
521
522 interface SinonTestWrapper extends SinonSandbox {
523 (...args: any[]): any;
524 }
525
526 // Utility overridables
527 interface SinonStatic {
528 /**
529 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
530 *
531 * @param constructor Object or class to stub.
532 * @returns A stubbed version of the constructor.
533 * @remarks The given constructor function is not invoked. See also the stub API.
534 */
535 createStubInstance(constructor: any): any;
536
537 /**
538 * Creates a new object with the given functions as the prototype and stubs all implemented functions.
539 *
540 * @template TType Type being stubbed.
541 * @param constructor Object or class to stub.
542 * @returns A stubbed version of the constructor.
543 * @remarks The given constructor function is not invoked. See also the stub API.
544 */
545 createStubInstance<TType>(constructor: StubbableType<TType>): SinonStubbedInstance<TType>;
546
547 format(obj: any): string;
548 restore(object: any): void;
549 }
550
551 /**
552 * Stubbed type of an object with members replaced by stubs.
553 *
554 * @template TType Type being stubbed.
555 */
556 interface StubbableType<TType> {
557 new(...args: any[]): TType;
558 }
559
560 /**
561 * An instance of a stubbed object type with members replaced by stubs.
562 *
563 * @template TType Object type being stubbed.
564 */
565 type SinonStubbedInstance<TType> = {
566 // TODO: this should really only replace functions on TType with SinonStubs, not all properties
567 // Likely infeasible without mapped conditional types, per https://github.com/Microsoft/TypeScript/issues/12424
568 [P in keyof TType]: SinonStub;
569 };
570}
571
572declare const Sinon: Sinon.SinonStatic;
573
574export = Sinon;
575export as namespace sinon;