forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
1770 lines (1701 loc) · 72 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// sinon uses DOM dependencies which are absent in browser-less environment like node.js
// to avoid compiler errors this monkey patch is used
// see more details in https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11351
interface Event {} // tslint:disable-line no-empty-interface
interface Document {} // tslint:disable-line no-empty-interface
declare namespace Sinon {
type MatchArguments<T> = {
[K in keyof T]: SinonMatcher
| (T[K] extends object ? MatchArguments<T[K]> : never)
| T[K];
};
interface SinonSpyCallApi<TArgs extends any[] = any[], TReturnValue = any> {
// Properties
/**
* Array of received arguments.
*/
args: TArgs;
// Methods
/**
* Returns true if the spy was called at least once with @param obj as this.
* calledOn also accepts a matcher spyCall.calledOn(sinon.match(fn)) (see matchers).
* @param obj
*/
calledOn(obj: any): boolean;
/**
* Returns true if spy was called at least once with the provided arguments.
* Can be used for partial matching, Sinon only checks the provided arguments against actual arguments,
* so a call that received the provided arguments (in the same spots) and possibly others as well will return true.
* @param args
*/
calledWith(...args: Partial<MatchArguments<TArgs>>): boolean;
/**
* Returns true if spy was called at least once with the provided arguments and no others.
*/
calledWithExactly(...args: MatchArguments<TArgs>): boolean;
/**
* Returns true if spy/stub was called the new operator.
* Beware that this is inferred based on the value of the this object and the spy function’s prototype,
* so it may give false positives if you actively return the right kind of object.
*/
calledWithNew(): boolean;
/**
* Returns true if spy was called at exactly once with the provided arguments.
* @param args
*/
calledOnceWith(...args: MatchArguments<TArgs>): boolean;
calledOnceWithExactly(...args: MatchArguments<TArgs>): boolean;
/**
* Returns true if spy was called with matching arguments (and possibly others).
* This behaves the same as spy.calledWith(sinon.match(arg1), sinon.match(arg2), ...).
* @param args
*/
calledWithMatch(...args: TArgs): boolean;
/**
* Returns true if call did not receive provided arguments.
* @param args
*/
notCalledWith(...args: MatchArguments<TArgs>): boolean;
/**
* Returns true if call did not receive matching arguments.
* This behaves the same as spyCall.notCalledWith(sinon.match(arg1), sinon.match(arg2), ...).
* @param args
*/
notCalledWithMatch(...args: TArgs): boolean;
/**
* Returns true if spy returned the provided value at least once.
* Uses deep comparison for objects and arrays. Use spy.returned(sinon.match.same(obj)) for strict comparison (see matchers).
* @param value
*/
returned(value: TReturnValue|SinonMatcher): boolean;
/**
* Returns true if spy threw an exception at least once.
*/
threw(): boolean;
/**
* Returns true if spy threw an exception of the provided type at least once.
*/
threw(type: string): boolean;
/**
* Returns true if spy threw the provided exception object at least once.
*/
threw(obj: any): boolean;
/**
* Like yield, but with an explicit argument number specifying which callback to call.
* Useful if a function is called with more than one callback, and simply calling the first callback is not desired.
* @param pos
*/
callArg(pos: number): void;
callArgOn(pos: number, obj: any, ...args: any[]): void;
/**
* Like callArg, but with arguments.
*/
callArgWith(pos: number, ...args: any[]): void;
callArgOnWith(pos: number, obj: any, ...args: any[]): void;
/**
* Invoke callbacks passed to the stub with the given arguments.
* If the stub was never called with a function argument, yield throws an error.
* Returns an Array with all callbacks return values in the order they were called, if no error is thrown.
* Also aliased as invokeCallback.
*/
yield(...args: any[]): void;
yieldOn(obj: any, ...args: any[]): void;
/**
* Invokes callbacks passed as a property of an object to the stub.
* Like yield, yieldTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.
*/
yieldTo(property: string, ...args: any[]): void;
yieldToOn(property: string, obj: any, ...args: any[]): void;
}
interface SinonSpyCall<TArgs extends any[] = any[], TReturnValue = any>
extends SinonSpyCallApi<TArgs, TReturnValue> {
/**
* The call’s this value.
*/
thisValue: any;
/**
* Exception thrown, if any.
*/
exception: any;
/**
* Return value.
*/
returnValue: TReturnValue;
/**
* This property is a convenience for a call’s callback.
* When the last argument in a call is a Function, then callback will reference that. Otherwise it will be undefined.
*/
callback: Function | undefined;
/**
* This property is a convenience for the last argument of the call.
*/
lastArg: any;
/**
* Returns true if the spy call occurred before another spy call.
* @param call
*
*/
calledBefore(call: SinonSpyCall<any>): boolean;
/**
* Returns true if the spy call occurred after another spy call.
* @param call
*/
calledAfter(call: SinonSpyCall<any>): boolean;
}
interface SinonSpy<TArgs extends any[] = any[], TReturnValue = any>
extends Pick<
SinonSpyCallApi<TArgs, TReturnValue>,
Exclude<keyof SinonSpyCallApi<TArgs, TReturnValue>, 'args'>
> {
// Properties
/**
* The number of recorded calls.
*/
callCount: number;
/**
* true if the spy was called at least once
*/
called: boolean;
/**
* true if the spy was not called
*/
notCalled: boolean;
/**
* true if spy was called exactly once
*/
calledOnce: boolean;
/**
* true if the spy was called exactly twice
*/
calledTwice: boolean;
/**
* true if the spy was called exactly thrice
*/
calledThrice: boolean;
/**
* The first call
*/
firstCall: SinonSpyCall<TArgs, TReturnValue>;
/**
* The second call
*/
secondCall: SinonSpyCall<TArgs, TReturnValue>;
/**
* The third call
*/
thirdCall: SinonSpyCall<TArgs, TReturnValue>;
/**
* The last call
*/
lastCall: SinonSpyCall<TArgs, TReturnValue>;
/**
* Array of this objects, spy.thisValues[0] is the this object for the first call.
*/
thisValues: any[];
/**
* Array of arguments received, spy.args[0] is an array of arguments received in the first call.
*/
args: TArgs[];
/**
* Array of exception objects thrown, spy.exceptions[0] is the exception thrown by the first call.
* If the call did not throw an error, the value at the call’s location in .exceptions will be undefined.
*/
exceptions: any[];
/**
* Array of return values, spy.returnValues[0] is the return value of the first call.
* If the call did not explicitly return a value, the value at the call’s location in .returnValues will be undefined.
*/
returnValues: TReturnValue[];
// Methods
(...args: TArgs): TReturnValue;
/**
* Returns true if the spy was called before @param anotherSpy
* @param anotherSpy
*/
calledBefore(anotherSpy: SinonSpy<any>): boolean;
/**
* Returns true if the spy was called after @param anotherSpy
* @param anotherSpy
*/
calledAfter(anotherSpy: SinonSpy<any>): boolean;
/**
* Returns true if spy was called before @param anotherSpy, and no spy calls occurred between spy and @param anotherSpy.
* @param anotherSpy
*/
calledImmediatelyBefore(anotherSpy: SinonSpy<any>): boolean;
/**
* Returns true if spy was called after @param anotherSpy, and no spy calls occurred between @param anotherSpy and spy.
* @param anotherSpy
*/
calledImmediatelyAfter(anotherSpy: SinonSpy<any>): boolean;
/**
* Creates a spy that only records calls when the received arguments match those passed to withArgs.
* This is useful to be more expressive in your assertions, where you can access the spy with the same call.
* @param args Expected args
*/
withArgs(...args: MatchArguments<TArgs>): SinonSpy<TArgs, TReturnValue>;
/**
* Returns true if the spy was always called with @param obj as this.
* @param obj
*/
alwaysCalledOn(obj: any): boolean;
/**
* Returns true if spy was always called with the provided arguments (and possibly others).
*/
alwaysCalledWith(...args: MatchArguments<TArgs>): boolean;
/**
* Returns true if spy was always called with the exact provided arguments.
* @param args
*/
alwaysCalledWithExactly(...args: MatchArguments<TArgs>): boolean;
/**
* Returns true if spy was always called with matching arguments (and possibly others).
* This behaves the same as spy.alwaysCalledWith(sinon.match(arg1), sinon.match(arg2), ...).
* @param args
*/
alwaysCalledWithMatch(...args: TArgs): boolean;
/**
* Returns true if the spy/stub was never called with the provided arguments.
* @param args
*/
neverCalledWith(...args: MatchArguments<TArgs>): boolean;
/**
* Returns true if the spy/stub was never called with matching arguments.
* This behaves the same as spy.neverCalledWith(sinon.match(arg1), sinon.match(arg2), ...).
* @param args
*/
neverCalledWithMatch(...args: TArgs): boolean;
/**
* Returns true if spy always threw an exception.
*/
alwaysThrew(): boolean;
/**
* Returns true if spy always threw an exception of the provided type.
*/
alwaysThrew(type: string): boolean;
/**
* Returns true if spy always threw the provided exception object.
*/
alwaysThrew(obj: any): boolean;
/**
* Returns true if spy always returned the provided value.
* @param obj
*/
alwaysReturned(obj: any): boolean;
/**
* Invoke callbacks passed to the stub with the given arguments.
* If the stub was never called with a function argument, yield throws an error.
* Returns an Array with all callbacks return values in the order they were called, if no error is thrown.
*/
invokeCallback(...args: TArgs): void;
/**
* Set the displayName of the spy or stub.
* @param name
*/
named(name: string): SinonSpy<TArgs, TReturnValue>;
/**
* Returns the nth call.
* Accessing individual calls helps with more detailed behavior verification when the spy is called more than once.
* @param n
*/
getCall(n: number): SinonSpyCall<TArgs, TReturnValue>;
/**
* Returns an Array of all calls recorded by the spy.
*/
getCalls(): Array<SinonSpyCall<TArgs, TReturnValue>>;
/**
* Resets the state of a spy.
*/
resetHistory(): void;
/**
* Returns the passed format string with the following replacements performed:
* * %n - the name of the spy "spy" by default)
* * %c - the number of times the spy was called, in words ("once", "twice", etc.)
* * %C - a list of string representations of the calls to the spy, with each call prefixed by a newline and four spaces
* * %t - a comma-delimited list of this values the spy was called on
* * %n - the formatted value of the nth argument passed to printf
* * %* - a comma-delimited list of the (non-format string) arguments passed to printf
* * %D - a multi-line list of the arguments received by all calls to the spy
* @param format
* @param args
*/
printf(format: string, ...args: any[]): string;
/**
* Replaces the spy with the original method. Only available if the spy replaced an existing method.
*/
restore(): void;
}
interface SinonSpyStatic {
/**
* Creates an anonymous function that records arguments, this value, exceptions and return values for all calls.
*/
(): SinonSpy;
/**
* Spies on the provided function
*/
<F extends (...args: any[]) => any>(func: F): SinonSpy<Parameters<F>, ReturnType<F>>;
/**
* Creates a spy for object.method and replaces the original method with the spy.
* An exception is thrown if the property is not already a function.
* The spy acts exactly like the original method in all cases.
* The original method can be restored by calling object.method.restore().
* The returned spy is the function object which replaced the original method. spy === object.method.
*/
<T, K extends keyof T>(obj: T, method: K, types?: string[]): T[K] extends (
...args: infer TArgs
) => infer TReturnValue
? SinonSpy<TArgs, TReturnValue>
: SinonSpy;
}
interface SinonStub<TArgs extends any[] = any[], TReturnValue = any>
extends SinonSpy<TArgs, TReturnValue> {
/**
* Resets the stub’s behaviour to the default behaviour
* You can reset behaviour of all stubs using sinon.resetBehavior()
*/
resetBehavior(): void;
/**
* Resets both behaviour and history of the stub.
* This is equivalent to calling both stub.resetBehavior() and stub.resetHistory()
* Updated in sinon@2.0.0
* Since sinon@5.0.0
* As a convenience, you can apply stub.reset() to all stubs using sinon.reset()
*/
reset(): void;
/**
* Causes the stub to return promises using a specific Promise library instead of the global one when using stub.rejects or stub.resolves.
* Returns the stub to allow chaining.
*/
usingPromise(promiseLibrary: any): SinonStub<TArgs, TReturnValue>;
/**
* Makes the stub return the provided @param obj value.
* @param obj
*/
returns(obj: TReturnValue): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return the argument at the provided @param index.
* stub.returnsArg(0); causes the stub to return the first argument.
* If the argument at the provided index is not available, prior to sinon@6.1.2, an undefined value will be returned;
* starting from sinon@6.1.2, a TypeError will be thrown.
* @param index
*/
returnsArg(index: number): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return its this value.
* Useful for stubbing jQuery-style fluent APIs.
*/
returnsThis(): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which resolves to the provided value.
* When constructing the Promise, sinon uses the Promise.resolve method.
* You are responsible for providing a polyfill in environments which do not provide Promise.
* The Promise library can be overwritten using the usingPromise method.
* Since sinon@2.0.0
*/
resolves(value?: TReturnValue extends PromiseLike<infer TResolveValue> ? TResolveValue : never): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which resolves to the argument at the provided index.
* stub.resolvesArg(0); causes the stub to return a Promise which resolves to the first argument.
* If the argument at the provided index is not available, a TypeError will be thrown.
*/
resolvesArg(index: number): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which resolves to its this value.
*/
resolvesThis(): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to throw an exception (Error).
* @param type
*/
throws(type?: string): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to throw the provided exception object.
*/
throws(obj: any): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to throw the argument at the provided index.
* stub.throwsArg(0); causes the stub to throw the first argument as the exception.
* If the argument at the provided index is not available, a TypeError will be thrown.
* Since sinon@2.3.0
* @param index
*/
throwsArg(index: number): SinonStub<TArgs, TReturnValue>;
throwsException(type?: string): SinonStub<TArgs, TReturnValue>;
throwsException(obj: any): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which rejects with an exception (Error).
* When constructing the Promise, sinon uses the Promise.reject method.
* You are responsible for providing a polyfill in environments which do not provide Promise.
* The Promise library can be overwritten using the usingPromise method.
* Since sinon@2.0.0
*/
rejects(): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which rejects with an exception of the provided type.
* Since sinon@2.0.0
*/
rejects(errorType: string): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which rejects with the provided exception object.
* Since sinon@2.0.0
*/
rejects(value: any): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to call the argument at the provided index as a callback function.
* stub.callsArg(0); causes the stub to call the first argument as a callback.
* If the argument at the provided index is not available or is not a function, a TypeError will be thrown.
*/
callsArg(index: number): SinonStub<TArgs, TReturnValue>;
/**
* Causes the original method wrapped into the stub to be called when none of the conditional stubs are matched.
*/
callThrough(): SinonStub<TArgs, TReturnValue>;
/**
* Like stub.callsArg(index); but with an additional parameter to pass the this context.
* @param index
* @param context
*/
callsArgOn(index: number, context: any): SinonStub<TArgs, TReturnValue>;
/**
* Like callsArg, but with arguments to pass to the callback.
* @param index
* @param args
*/
callsArgWith(index: number, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Like above but with an additional parameter to pass the this context.
* @param index
* @param context
* @param args
*/
callsArgOnWith(
index: number,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
* @param index
*/
callsArgAsync(index: number): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
* @param index
* @param context
*/
callsArgOnAsync(index: number, context: any): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
*/
callsArgWithAsync(index: number, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
*/
callsArgOnWithAsync(
index: number,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
/**
* Makes the stub call the provided @param func when invoked.
* @param func
*/
callsFake(func: (...args: TArgs) => TReturnValue): SinonStub<TArgs, TReturnValue>;
/**
* Replaces a new getter for this stub.
*/
get(func: () => any): SinonStub<TArgs, TReturnValue>;
/**
* Defines a new setter for this stub.
* @param func
*/
set(func: (v: any) => void): SinonStub<TArgs, TReturnValue>;
/**
* Defines the behavior of the stub on the @param n call. Useful for testing sequential interactions.
* There are methods onFirstCall, onSecondCall,onThirdCall to make stub definitions read more naturally.
* onCall can be combined with all of the behavior defining methods in this section. In particular, it can be used together with withArgs.
* @param n
*/
onCall(n: number): SinonStub<TArgs, TReturnValue>;
/**
* Alias for stub.onCall(0);
*/
onFirstCall(): SinonStub<TArgs, TReturnValue>;
/**
* Alias for stub.onCall(1);
*/
onSecondCall(): SinonStub<TArgs, TReturnValue>;
/**
* Alias for stub.onCall(2);
*/
onThirdCall(): SinonStub<TArgs, TReturnValue>;
/**
* Defines a new value for this stub.
* @param val
*/
value(val: any): SinonStub<TArgs, TReturnValue>;
/**
* Set the displayName of the spy or stub.
* @param name
*/
named(name: string): SinonStub<TArgs, TReturnValue>;
/**
* Similar to callsArg.
* Causes the stub to call the first callback it receives with the provided arguments (if any).
* If a method accepts more than one callback, you need to use callsArg to have the stub invoke other callbacks than the first one.
*/
yields(...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Like above but with an additional parameter to pass the this context.
*/
yieldsOn(context: any, ...args: any[]): SinonStub<TArgs, TReturnValue>;
yieldsRight(...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Causes the spy to invoke a callback passed as a property of an object to the spy.
* Like yields, yieldsTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.
* @param property
* @param args
*/
yieldsTo(property: string, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Like above but with an additional parameter to pass the this context.
*/
yieldsToOn(
property: string,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
* @param args
*/
yieldsAsync(...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
* @param context
* @param args
*/
yieldsOnAsync(context: any, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
* @param property
* @param args
*/
yieldsToAsync(property: string, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
* @param property
* @param context
* @param args
*/
yieldsToOnAsync(
property: string,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
/**
* Stubs the method only for the provided arguments.
* This is useful to be more expressive in your assertions, where you can access the spy with the same call.
* It is also useful to create a stub that can act differently in response to different arguments.
* @param args
*/
withArgs(...args: MatchArguments<TArgs>): SinonStub<TArgs, TReturnValue>;
}
interface SinonStubStatic {
/* tslint:disable:no-unnecessary-generics */
/**
* Creates an anonymous stub function
*/
<TArgs extends any[] = any[], R = any>(): SinonStub<TArgs, R>;
/* tslint:enable:no-unnecessary-generics */
/**
* Stubs all the object’s methods.
* 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).
* Stubbing individual methods tests intent more precisely and is less susceptible to unexpected behavior as the object’s code evolves.
* If you want to create a stub object of MyConstructor, but don’t want the constructor to be invoked, use this utility function.
*/
<T>(obj: T): SinonStubbedInstance<T>;
/**
* Replaces obj.method with a stub function.
* An exception is thrown if the property is not already a function.
* The original function can be restored by calling object.method.restore(); (or stub.restore();).
*/
<T, K extends keyof T>(obj: T, method: K): T[K] extends (
...args: infer TArgs
) => infer TReturnValue
? SinonStub<TArgs, TReturnValue>
: SinonStub;
}
interface SinonExpectation extends SinonStub {
/**
* Specify the minimum amount of calls expected.
*/
atLeast(n: number): SinonExpectation;
/**
* Specify the maximum amount of calls expected.
* @param n
*/
atMost(n: number): SinonExpectation;
/**
* Expect the method to never be called.
*/
never(): SinonExpectation;
/**
* Expect the method to be called exactly once.
*/
once(): SinonExpectation;
/**
* Expect the method to be called exactly twice.
*/
twice(): SinonExpectation;
/**
* Expect the method to be called exactly thrice.
*/
thrice(): SinonExpectation;
/**
* Expect the method to be called exactly @param n times.
*/
exactly(n: number): SinonExpectation;
/**
* Expect the method to be called with the provided arguments and possibly others.
* An expectation instance only holds onto a single set of arguments specified with withArgs.
* Subsequent calls will overwrite the previously-specified set of arguments (even if they are different),
* so it is generally not intended that this method be invoked more than once per test case.
* @param args
*/
withArgs(...args: any[]): SinonExpectation;
/**
* Expect the method to be called with the provided arguments and no others.
* An expectation instance only holds onto a single set of arguments specified with withExactArgs.
* Subsequent calls will overwrite the previously-specified set of arguments (even if they are different),
* so it is generally not intended that this method be invoked more than once per test case.
* @param args
*/
withExactArgs(...args: any[]): SinonExpectation;
on(obj: any): SinonExpectation;
/**
* Verifies all expectations on the mock.
* If any expectation is not satisfied, an exception is thrown.
* Also restores the mocked methods.
*/
verify(): SinonExpectation;
/**
* Restores all mocked methods.
*/
restore(): void;
}
interface SinonExpectationStatic {
/**
* Creates an expectation without a mock object, basically an anonymous mock function.
* Method name is optional and is used in exception messages to make them more readable.
* @param methodName
*/
create(methodName?: string): SinonExpectation;
}
interface SinonMock {
/**
* Overrides obj.method with a mock function and returns it.
*/
expects(method: string): SinonExpectation;
/**
* Restores all mocked methods.
*/
restore(): void;
/**
* Verifies all expectations on the mock.
* If any expectation is not satisfied, an exception is thrown.
* Also restores the mocked methods.
*/
verify(): void;
}
interface SinonMockStatic {
(): SinonExpectation;
/**
* Creates a mock for the provided object.
* Does not change the object, but returns a mock object to set expectations on the object’s methods.
*/
(obj: any): SinonMock;
}
type SinonTimerId = number | { id: number };
interface SinonFakeTimers {
now: number;
setTimeout(
callback: (...args: any[]) => void,
timeout: number,
...args: any[]
): SinonTimerId;
clearTimeout(id: SinonTimerId): void;
setInterval(
callback: (...args: any[]) => void,
timeout: number,
...args: any[]
): SinonTimerId;
clearInterval(id: SinonTimerId): void;
setImmediate(
callback: (...args: any[]) => void,
...args: any[]
): SinonTimerId;
clearImmediate(id: SinonTimerId): void;
requestAnimationFrame(callback: (...args: any[]) => void): number;
cancelAnimationFrame(id: number): void;
nextTick(callback: () => void): void;
/**
* Tick the clock ahead time milliseconds.
* Causes all timers scheduled within the affected time range to be called.
* time may be the number of milliseconds to advance the clock by or a human-readable string.
* 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.
* time may be negative, which causes the clock to change but won’t fire any callbacks.
* @param ms
*/
tick(ms: number | string): void;
/**
* Advances the clock to the the moment of the first scheduled timer, firing it.
*/
next(): void;
/**
* 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.
* 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.
*/
runAll(): void;
runToLast(): void;
reset(): void;
runMicrotasks(): void;
runToFrame(): void;
Date(): Date;
Date(year: number): Date;
Date(year: number, month: number): Date;
Date(year: number, month: number, day: number): Date;
Date(year: number, month: number, day: number, hour: number): Date;
Date(
year: number,
month: number,
day: number,
hour: number,
minute: number
): Date;
Date(
year: number,
month: number,
day: number,
hour: number,
minute: number,
second: number
): Date;
Date(
year: number,
month: number,
day: number,
hour: number,
minute: number,
second: number,
ms: number
): Date;
/**
* Restore the faked methods.
* Call in e.g. tearDown.
*/
restore(): void;
uninstall(): void;
/**
* Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
* without affecting timers, intervals or immediates.
* @param now The new 'now' in unix milliseconds
*/
setSystemTime(now: number): void;
/**
* Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
* without affecting timers, intervals or immediates.
* @param now The new 'now' as a JavaScript Date
*/
setSystemTime(date: Date): void;
}
interface SinonFakeTimersConfig {
now: number | Date;
toFake: string[];
shouldAdvanceTime: boolean;
}
interface SinonFakeUploadProgress {
eventListeners: {
progress: any[];
load: any[];
abort: any[];
error: any[];
};
addEventListener(event: string, listener: (e: Event) => any): void;
removeEventListener(event: string, listener: (e: Event) => any): void;
dispatchEvent(event: Event): void;
}
interface SinonFakeXMLHttpRequest {
// Properties
/**
* The URL set on the request object.
*/
url: string;
/**
* The request method as a string.
*/
method: string;
/**
* An object of all request headers, i.e.:
*/
requestHeaders: any;
/**
* The request body
*/
requestBody: string;
/**
* The request’s status code.
* undefined if the request has not been handled (see respond below)
*/
status: number;
/**
* Only populated if the respond method is called (see below).
*/
statusText: string;
/**
* Whether or not the request is asynchronous.
*/
async: boolean;
/**
* Username, if any.
*/
username: string;
/**
* Password, if any.
*/
password: string;
withCredentials: boolean;
upload: SinonFakeUploadProgress;
/**
* When using respond, this property is populated with a parsed document if response headers indicate as much (see the spec)
*/
responseXML: Document;
/**
* The value of the given response header, if the request has been responded to (see respond).
* @param header
*/
getResponseHeader(header: string): string;
/**
* All response headers as an object.
*/
getAllResponseHeaders(): any;
// Methods
/**
* Sets response headers (e.g. { "Content-Type": "text/html", ... }, updates the readyState property and fires onreadystatechange.
* @param headers
*/
setResponseHeaders(headers: any): void;
/**
* Sets the respond body, updates the readyState property and fires onreadystatechange.
* Additionally, populates responseXML with a parsed document if response headers indicate as much.
*/
setResponseBody(body: string): void;
/**
* Calls the above three methods.
*/
respond(status: number, headers: any, body: string): void;
autoRespond(ms: number): void;
/**
* Simulates a network error on the request. The onerror handler will be called and the status will be 0.
*/
error(): void;
onerror(): void;
}
interface SinonFakeXMLHttpRequestStatic {
new (): SinonFakeXMLHttpRequest;
/**
* Default false.
* When set to true, Sinon will check added filters if certain requests should be “unfaked”
*/
useFilters: boolean;
/**
* Add a filter that will decide whether or not to fake a request.
* The filter will be called when xhr.open is called, with the exact same arguments (method, url, async, username, password).
* If the filter returns true, the request will not be faked.
* @param filter
*/
addFilter(
filter: (
method: string,
url: string,
async: boolean,
username: string,
password: string
) => boolean
): void;
/**
* By assigning a function to the onCreate property of the returned object from useFakeXMLHttpRequest()
* you can subscribe to newly created FakeXMLHttpRequest objects. See below for the fake xhr object API.
* Using this observer means you can still reach objects created by e.g. jQuery.ajax (or other abstractions/frameworks).
* @param xhr
*/
onCreate(xhr: SinonFakeXMLHttpRequest): void;
/**
* Restore original function(s).
*/
restore(): void;
}
interface SinonFakeServer extends SinonFakeServerOptions {
// Properties
/**
* Used internally to determine the HTTP method used with the provided request.
* By default this method simply returns request.method.
* When server.fakeHTTPMethods is true, the method will return the value of the _method parameter if the method is “POST”.
* This method can be overridden to provide custom behavior.
* @param request
*/
getHTTPMethod(request: SinonFakeXMLHttpRequest): string;
/**
* You can inspect the server.requests to verify request ordering, find unmatched requests or check that no requests has been done.
* server.requests is an array of all the FakeXMLHttpRequest objects that have been created.