UNPKG

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