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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
|
// 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];
};
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: 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): boolean;
/**
* Returns true if the spy call occurred after another spy call.
* @param call
*/
calledAfter(call: SinonSpyCall): 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: any[]): any;
/**
* Returns true if the spy was called before @param anotherSpy
* @param anotherSpy
*/
calledBefore(anotherSpy: SinonSpy): boolean;
/**
* Returns true if the spy was called after @param anotherSpy
* @param anotherSpy
*/
calledAfter(anotherSpy: SinonSpy): 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): 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): 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
*/
(func: Function): SinonSpy;
/**
* 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 Promise<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 {
// Disable rule so assignment to typed stub works (see examples in tests).
/**
* Creates an anonymous stub function
*/
// tslint:disable-next-line no-unnecessary-generics
<TArgs extends any[]= any[], R = any>(): SinonStub<TArgs, R>;
/**
* 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.
*/
requests: SinonFakeXMLHttpRequest[];
// Methods
/**
* Causes the server to respond to any request not matched by another response with the provided data. The default catch-all response is [404, {}, ""].
* A String representing the response body
* An Array with status, headers and response body, e.g. [200, { "Content-Type": "text/html", "Content-Length": 2 }, "OK"]
* A Function.
* Default status is 200 and default headers are none.
* When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.
* @param body A String representing the response body
*/
respondWith(body: string): void;
/**
* Causes the server to respond to any request not matched by another response with the provided data. The default catch-all response is [404, {}, ""].
* Default status is 200 and default headers are none.
* When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.
* @param response An Array with status, headers and response body, e.g. [200, { "Content-Type": "text/html", "Content-Length": 2 }, "OK"]
*/
respondWith(response: any[]): void;
/**
* Causes the server to respond to any request not matched by another response with the provided data. The default catch-all response is [404, {}, ""].
* Default status is 200 and default headers are none.
* When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.
* @param fn A Function.
*/
respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
/**
* Responds to all requests to given URL, e.g. /posts/1.
*/
respondWith(url: string, body: string): void;
/**
* Responds to all requests to given URL, e.g. /posts/1.
*/
respondWith(url: string, response: any[]): void;
/**
* Responds to all requests to given URL, e.g. /posts/1.
*/
respondWith(
url: string,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
/**
* Responds to all method requests to the given URL with the given response.
* method is an HTTP verb.
*/
respondWith(method: string, url: string, body: string): void;
/**
* Responds to all method requests to the given URL with the given response.
* method is an HTTP verb.
*/
respondWith(method: string, url: string, response: any[]): void;
/**
* Responds to all method requests to the given URL with the given response.
* method is an HTTP verb.
*/
respondWith(
method: string,
url: string,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
/**
* URL may be a regular expression, e.g. /\\/post\\//\\d+
* If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
*/
respondWith(url: RegExp, body: string): void;
/**
* URL may be a regular expression, e.g. /\\/post\\//\\d+
* If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
*/
respondWith(url: RegExp, response: any[]): void;
/**
* URL may be a regular expression, e.g. /\\/post\\//\\d+
* If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
*/
respondWith(
url: RegExp,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
/**
* Responds to all method requests to URLs matching the regular expression.
*/
respondWith(method: string, url: RegExp, body: string): void;
/**
* Responds to all method requests to URLs matching the regular expression.
*/
respondWith(method: string, url: RegExp, response: any[]): void;
/**
* Responds to all method requests to URLs matching the regular expression.
*/
respondWith(
method: string,
url: RegExp,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
/**
* Causes all queued asynchronous requests to receive a response.
* If none of the responses added through respondWith match, the default response is [404, {}, ""].
* Synchronous requests are responded to immediately, so make sure to call respondWith upfront.
* If called with arguments, respondWith will be called with those arguments before responding to requests.
*/
respond(): void;
restore(): void;
}
interface SinonFakeServerOptions {
/**
* When set to true, causes the server to automatically respond to incoming requests after a timeout.
* The default timeout is 10ms but you can control it through the autoRespondAfter property.
* Note that this feature is intended to help during mockup development, and is not suitable for use in tests.
*/
autoRespond: boolean;
/**
* When autoRespond is true, respond to requests after this number of milliseconds. Default is 10.
*/
autoRespondAfter: number;
/**
* If set to true, server will find _method parameter in POST body and recognize that as the actual method.
* Supports a pattern common to Ruby on Rails applications. For custom HTTP method faking, override server.getHTTPMethod(request).
*/
fakeHTTPMethods: boolean;
/**
* If set, the server will respond to every request immediately and synchronously.
* This is ideal for faking the server from within a test without having to call server.respond() after each request made in that test.
* As this is synchronous and immediate, this is not suitable for simulating actual network latency in tests or mockups.
* To simulate network latency with automatic responses, see server.autoRespond and server.autoRespondAfter.
*/
respondImmediately: boolean;
}
interface SinonFakeServerStatic {
create(options?: Partial<SinonFakeServerOptions>): SinonFakeServer;
}
interface SinonExposeOptions {
prefix: string;
includeFail: boolean;
}
interface SinonAssert {
// Properties
/**
* Defaults to AssertError.
*/
failException: string;
/**
* Every assertion fails by calling this method.
* By default it throws an error of type sinon.assert.failException.
* If the test framework looks for assertion errors by checking for a specific exception, you can simply override the kind of exception thrown.
* If that does not fit with your testing framework of choice, override the fail method to do the right thing.
*/
fail(message?: string): void; // Overridable
/**
* Called every time assertion passes.
* Default implementation does nothing.
*/
pass(assertion: any): void; // Overridable
// Methods
/**
* Passes if spy was never called
* @param spy
*/
notCalled(spy: SinonSpy): void;
/**
* Passes if spy was called at least once.
*/
called(spy: SinonSpy): void;
/**
* Passes if spy was called once and only once.
*/
calledOnce(spy: SinonSpy): void;
/**
* Passes if spy was called exactly twice.
*/
calledTwice(spy: SinonSpy): void;
/**
* Passes if spy was called exactly three times.
*/
calledThrice(spy: SinonSpy): void;
/**
* Passes if spy was called exactly num times.
*/
callCount(spy: SinonSpy, count: number): void;
/**
* Passes if provided spies were called in the specified order.
* @param spies
*/
callOrder(...spies: SinonSpy[]): void;
/**
* Passes if spy was ever called with obj as its this value.
* It’s possible to assert on a dedicated spy call: sinon.assert.calledOn(spy.firstCall, arg1, arg2, ...);.
*/
calledOn(spyOrSpyCall: SinonSpy | SinonSpyCall, obj: any): void;
/**
* Passes if spy was always called with obj as its this value.
*/
alwaysCalledOn(spy: SinonSpy, obj: any): void;
/**
* Passes if spy was called with the provided arguments.
* It’s possible to assert on a dedicated spy call: sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);.
* @param spyOrSpyCall
* @param args
*/
calledWith(spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: any[]): void;
/**
* Passes if spy was always called with the provided arguments.
* @param spy
* @param args
*/
alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;
/**
* Passes if spy was never called with the provided arguments.
* @param spy
* @param args
*/
neverCalledWith(spy: SinonSpy, ...args: any[]): void;
/**
* Passes if spy was called with the provided arguments and no others.
* It’s possible to assert on a dedicated spy call: sinon.assert.calledWithExactly(spy.getCall(1), arg1, arg2, ...);.
* @param spyOrSpyCall
* @param args
*/
calledWithExactly(
spyOrSpyCall: SinonSpy | SinonSpyCall,
...args: any[]
): void;
/**
* Passes if spy was always called with the provided arguments and no others.
*/
alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;
/**
* Passes if spy was called with matching arguments.
* This behaves the same way as sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
* It’s possible to assert on a dedicated spy call: sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);.
*/
calledWithMatch(
spyOrSpyCall: SinonSpy | SinonSpyCall,
...args: any[]
): void;
/**
* Passes if spy was always called with matching arguments.
* This behaves the same way as sinon.assert.alwaysCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
*/
alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
/**
* Passes if spy was never called with matching arguments.
* This behaves the same way as sinon.assert.neverCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
* @param spy
* @param args
*/
neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
/**
* Passes if spy was called with the new operator.
* It’s possible to assert on a dedicated spy call: sinon.assert.calledWithNew(spy.secondCall, arg1, arg2, ...);.
* @param spyOrSpyCall
*/
calledWithNew(spyOrSpyCall: SinonSpy | SinonSpyCall): void;
/**
* Passes if spy threw any exception.
*/
threw(spyOrSpyCall: SinonSpy | SinonSpyCall): void;
/**
* Passes if spy threw the given exception.
* The exception is an actual object.
* It’s possible to assert on a dedicated spy call: sinon.assert.threw(spy.thirdCall, exception);.
*/
threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: string): void;
/**
* Passes if spy threw the given exception.
* The exception is a String denoting its type.
* It’s possible to assert on a dedicated spy call: sinon.assert.threw(spy.thirdCall, exception);.
*/
threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: any): void;
/**
* Like threw, only required for all calls to the spy.
*/
alwaysThrew(spy: SinonSpy): void;
/**
* Like threw, only required for all calls to the spy.
*/
alwaysThrew(spy: SinonSpy, exception: string): void;
/**
* Like threw, only required for all calls to the spy.
*/
alwaysThrew(spy: SinonSpy, exception: any): void;
/**
* Uses sinon.match to test if the arguments can be considered a match.
*/
match(actual: any, expected: any): void;
/**
* Exposes assertions into another object, to better integrate with the test framework.
* For instance, JsTestDriver uses global assertions, and to make Sinon.JS assertions appear alongside them, you can do.
* @example sinon.assert.expose(this);
* This will give you assertCalled(spy),assertCallOrder(spy1, spy2, ...) and so on.
* The method accepts an optional options object with two options.
*/
expose(obj: any, options?: Partial<SinonExposeOptions>): void;
}
interface SinonMatcher {
/**
* All matchers implement and and or. This allows to logically combine mutliple matchers.
* The result is a new matchers that requires both (and) or one of the matchers (or) to return true.
* @example var stringOrNumber = sinon.match.string.or(sinon.match.number);
* var bookWithPages = sinon.match.instanceOf(Book).and(sinon.match.has("pages"));
*/
and(expr: SinonMatcher): SinonMatcher;
/**
* All matchers implement and and or. This allows to logically combine mutliple matchers.
* The result is a new matchers that requires both (and) or one of the matchers (or) to return true.
* @example var stringOrNumber = sinon.match.string.or(sinon.match.number);
* var bookWithPages = sinon.match.instanceOf(Book).and(sinon.match.has("pages"));
*/
or(expr: SinonMatcher): SinonMatcher;
test(val: any): boolean;
}
interface SinonArrayMatcher extends SinonMatcher {
/**
* Requires an Array to be deep equal another one.
*/
deepEquals(expected: any[]): SinonMatcher;
/**
* Requires an Array to start with the same values as another one.
*/
startsWith(expected: any[]): SinonMatcher;
/**
* Requires an Array to end with the same values as another one.
*/
endsWith(expected: any[]): SinonMatcher;
/**
* Requires an Array to contain each one of the values the given array has.
*/
contains(expected: any[]): SinonMatcher;
}
interface SimplifiedSet {
has(el: any): boolean;
}
interface SimplifiedMap extends SimplifiedSet {
get(key: any): any;
}
interface SinonMapMatcher extends SinonMatcher {
/**
* Requires a Map to be deep equal another one.
*/
deepEquals(expected: SimplifiedMap): SinonMatcher;
/**
* Requires a Map to contain each one of the items the given map has.
*/
contains(expected: SimplifiedMap): SinonMatcher;
}
interface SinonSetMatcher extends SinonMatcher {
/**
* Requires a Set to be deep equal another one.
*/
deepEquals(expected: SimplifiedSet): SinonMatcher;
/**
* Requires a Set to contain each one of the items the given set has.
*/
contains(expected: SimplifiedSet): SinonMatcher;
}
interface SinonMatch {
/**
* Requires the value to be == to the given number.
*/
(value: number): SinonMatcher;
/**
* Requires the value to be a string and have the expectation as a substring.
*/
(value: string): SinonMatcher;
/**
* Requires the value to be a string and match the given regular expression.
*/
(expr: RegExp): SinonMatcher;
/**
* See custom matchers.
*/
(callback: (value: any) => boolean, message?: string): SinonMatcher;
/**
* Requires the value to be not null or undefined and have at least the same properties as expectation.
* This supports nested matchers.
*/
(obj: object): SinonMatcher;
/**
* Matches anything.
*/
any: SinonMatcher;
/**
* Requires the value to be defined.
*/
defined: SinonMatcher;
/**
* Requires the value to be truthy.
*/
truthy: SinonMatcher;
/**
* Requires the value to be falsy.
*/
falsy: SinonMatcher;
/**
* Requires the value to be a Boolean
*/
bool: SinonMatcher;
/**
* Requires the value to be a Number.
*/
number: SinonMatcher;
/**
* Requires the value to be a String.
*/
string: SinonMatcher;
/**
* Requires the value to be an Object.
*/
object: SinonMatcher;
/**
* Requires the value to be a Function.
*/
func: SinonMatcher;
/**
* Requires the value to be a Map.
*/
map: SinonMapMatcher;
/**
* Requires the value to be a Set.
*/
set: SinonSetMatcher;
/**
* Requires the value to be an Array.
*/
array: SinonArrayMatcher;
/**
* Requires the value to be a regular expression.
*/
regexp: SinonMatcher;
/**
* Requires the value to be a Date object.
*/
date: SinonMatcher;
/**
* Requires the value to be a Symbol.
*/
symbol: SinonMatcher;
/**
* Requires the value to be in the specified array.
*/
in(allowed: any[]): SinonMatcher;
/**
* Requires the value to strictly equal ref.
*/
same(obj: any): SinonMatcher;
/**
* Requires the value to be of the given type, where type can be one of "undefined", "null", "boolean", "number", "string", "object", "function", "array", "regexp", "date" or "symbol".
*/
typeOf(type: string): SinonMatcher;
/**
* Requires the value to be an instance of the given type.
*/
instanceOf(type: any): SinonMatcher;
/**
* Requires the value to define the given property.
* The property might be inherited via the prototype chain.
* If the optional expectation is given, the value of the property is deeply compared with the expectation.
* The expectation can be another matcher.
* @param property
* @param expect
*/
has(property: string, expect?: any): SinonMatcher;
/**
* Same as sinon.match.has but the property must be defined by the value itself. Inherited properties are ignored.
* @param property
* @param expect
*/
hasOwn(property: string, expect?: any): SinonMatcher;
/**
* Requires the value to define the given propertyPath. Dot (prop.prop) and bracket (prop[0]) notations are supported as in Lodash.get.
* The propertyPath might be inherited via the prototype chain.
* If the optional expectation is given, the value at the propertyPath is deeply compared with the expectation.
* The expectation can be another matcher.
*/
hasNested(path: string, expect?: any): SinonMatcher;
/**
* Requires every element of an Array, Set or Map, or alternatively every value of an Object to match the given matcher.
*/
every(matcher: SinonMatcher): SinonMatcher;
/**
* Requires any element of an Array, Set or Map, or alternatively any value of an Object to match the given matcher.
*/
some(matcher: SinonMatcher): SinonMatcher;
}
interface SinonSandboxConfig {
/**
* The sandbox’s methods can be injected into another object for convenience.
* The injectInto configuration option can name an object to add properties to.
*/
injectInto: object | null;
/**
* What properties to inject.
* Note that simply naming “server” here is not sufficient to have a server property show up in the target object,
* you also have to set useFakeServer to true.
*/
properties: string[];
/**
* If set to true, the sandbox will have a clock property.
* You can optionally pass in a configuration object that follows the specification for fake timers, such as { toFake: ["setTimeout", "setInterval"] }.
*/
useFakeTimers: boolean | Partial<SinonFakeTimersConfig>;
/**
* If true, server and requests properties are added to the sandbox. Can also be an object to use for fake server.
* The default one is sinon.fakeServer, but if you’re using jQuery 1.3.x or some other library that does not set the XHR’s onreadystatechange handler,
* you might want to do:
*/
useFakeServer: boolean | SinonFakeServer;
}
/**
* Stubbed type of an object with members replaced by stubs.
*
* @template TType Type being stubbed.
*/
type StubbableType<TType> = Function & { prototype: TType };
/**
* An instance of a stubbed object type with functions replaced by stubs.
*
* @template TType Object type being stubbed.
*/
type SinonStubbedInstance<TType> = {
[P in keyof TType]: SinonStubbedMember<TType[P]>
};
/**
* Replaces a type with a Sinon stub if it's a function.
*/
type SinonStubbedMember<T> = T extends (
...args: infer TArgs
) => infer TReturnValue ? SinonStub<TArgs, TReturnValue> : T;
interface SinonFake {
/**
* Creates a basic fake, with no behavior
*/
(): SinonSpy;
/**
* Wraps an existing Function to record all interactions, while leaving it up to the func to provide the behavior.
* This is useful when complex behavior not covered by the sinon.fake.* methods is required or when wrapping an existing function or method.
*/
(fn: Function): SinonSpy;
/**
* Creates a fake that returns the val argument
* @param val Returned value
*/
returns(val: any): SinonSpy;
/**
* Creates a fake that throws an Error with the provided value as the message property.
* If an Error is passed as the val argument, then that will be the thrown value. If any other value is passed, then that will be used for the message property of the thrown Error.
* @param val Returned value or throw value if an Error
*/
throws(val: Error | string): SinonSpy;
/**
* Creates a fake that returns a resolved Promise for the passed value.
* @param val Resolved promise
*/
resolves(val: any): SinonSpy;
/**
* Creates a fake that returns a rejected Promise for the passed value.
* If an Error is passed as the value argument, then that will be the value of the promise.
* If any other value is passed, then that will be used for the message property of the Error returned by the promise.
* @param val Rejected promise
*/
rejects(val: any): SinonSpy;
/**
* fake expects the last argument to be a callback and will invoke it with the given arguments.
*/
yields(...args: any[]): SinonSpy;
/**
* fake expects the last argument to be a callback and will invoke it asynchronously with the given arguments.
*/
yieldsAsync(...args: any[]): SinonSpy;
}
interface SinonSandbox {
/**
* A convenience reference for sinon.assert
* Since sinon@2.0.0
*/
assert: SinonAssert;
clock: SinonFakeTimers;
requests: SinonFakeXMLHttpRequest[];
server: SinonFakeServer;
/**
* Works exactly like sinon.spy
*/
spy: SinonSpyStatic;
/**
* Works exactly like sinon.stub.
*/
stub: SinonStubStatic;
/**
* Works exactly like sinon.mock
*/
mock: SinonMockStatic;
/**
* * No param : Causes Sinon to replace the global setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, process.hrtime, performance.now(when available)
* and Date with a custom implementation which is bound to the returned clock object.
* Starts the clock at the UNIX epoch (timestamp of 0).
* * Now : As above, but rather than starting the clock with a timestamp of 0, start at the provided timestamp now.
* Since sinon@2.0.0
* You can also pass in a Date object, and its getTime() will be used for the starting timestamp.
* * Config : As above, but allows further configuration options, some of which are:
* * config.now - Number/Date - installs lolex with the specified unix epoch (default: 0)
* * config.toFake - String[ ] - an array with explicit function names to fake.
* By default lolex will automatically fake all methods except process.nextTick. You could, however, still fake nextTick by providing it explicitly
* * config.shouldAdvanceTime - Boolean - tells lolex to increment mocked time automatically based on the real system time shift (default: false)
* * Please visit the lolex.install documentation for the full feature set.
* * Important note: when faking nextTick, normal calls to process.nextTick() would not execute automatically as they would during normal event-loop phases.
* You would have to call either clock.next(), clock.tick(), clock.runAll() or clock.runToLast() (see example below). Please refer to the lolex documentation for more information.
* @param config
*/
useFakeTimers(
config?: number | Date | Partial<SinonFakeTimersConfig>
): SinonFakeTimers;
/**
* Causes Sinon to replace the native XMLHttpRequest object in browsers that support it with a custom implementation which does not send actual requests.
* In browsers that support ActiveXObject, this constructor is replaced, and fake objects are returned for XMLHTTP progIds.
* Other progIds, such as XMLDOM are left untouched.
* The native XMLHttpRequest object will be available at sinon.xhr.XMLHttpRequest
*/
useFakeXMLHttpRequest(): SinonFakeXMLHttpRequestStatic;
/**
* Fakes XHR and binds a server object to the sandbox such that it too is restored when calling sandbox.restore().
* Access requests through sandbox.requests and server through sandbox.server
*/
useFakeServer(): SinonFakeServer;
/**
* Restores all fakes created through sandbox.
*/
restore(): void;
/**
* Resets the internal state of all fakes created through sandbox.
*/
reset(): void;
/**
* Resets the history of all stubs created through the sandbox.
* Since sinon@2.0.0
*/
resetHistory(): void;
/**
* Resets the behaviour of all stubs created through the sandbox.
* Since sinon@2.0.0
*/
resetBehavior(): void;
/**
* Causes all stubs created from the sandbox 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.
* Since sinon@2.0.0
*/
usingPromise(promiseLibrary: any): SinonSandbox;
/**
* Verifies all mocks created through the sandbox.
*/
verify(): void;
/**
* Verifies all mocks and restores all fakes created through the sandbox.
*/
verifyAndRestore(): void;
/**
* Replaces property on object with replacement argument. Attempts to replace an already replaced value cause an exception.
* replacement can be any value, including spies, stubs and fakes.
* This method only works on non-accessor properties, for replacing accessors, use sandbox.replaceGetter() and sandbox.replaceSetter().
*/
replace<T, TKey extends keyof T>(
obj: T,
prop: TKey,
replacement: T[TKey]
): T[TKey];
/**
* Replaces getter for property on object with replacement argument. Attempts to replace an already replaced getter cause an exception.
* replacement must be a Function, and can be instances of spies, stubs and fakes.
* @param obj
* @param prop
* @param replacement
*/
replaceGetter<T, TKey extends keyof T>(
obj: T,
prop: TKey,
replacement: () => T[TKey]
): () => T[TKey];
/**
* Replaces setter for property on object with replacement argument. Attempts to replace an already replaced setter cause an exception.
* replacement must be a Function, and can be instances of spies, stubs and fakes.
* @param obj
* @param prop
* @param replacement
*/
replaceSetter<T, TKey extends keyof T>(
obj: T,
prop: TKey,
replacement: (val: T[TKey]) => void
): (val: T[TKey]) => void;
/**
* Creates a new object with the given functions as the prototype and stubs all implemented functions.
*
* @template TType Type being stubbed.
* @param constructor Object or class to stub.
* @returns A stubbed version of the constructor.
* @remarks The given constructor function is not invoked. See also the stub API.
*/
createStubInstance<TType>(
constructor: StubbableType<TType>
): SinonStubbedInstance<TType>;
}
interface SinonApi {
fake: SinonFake;
match: SinonMatch;
spyCall(...args: any[]): SinonSpyCall;
expectation: SinonExpectationStatic;
clock: {
create(now: number | Date): SinonFakeTimers;
};
FakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
fakeServer: SinonFakeServerStatic;
fakeServerWithClock: SinonFakeServerStatic;
/**
* Creates a new sandbox object with spies, stubs, and mocks.
* @param config
*/
createSandbox(config?: Partial<SinonSandboxConfig>): SinonSandbox;
defaultConfig: Partial<SinonSandboxConfig>;
}
interface LegacySandbox {
sandbox: {
/**
* @deprecated Since 5.0, use `sinon.createSandbox` instead
*/
create(config?: Partial<SinonSandboxConfig>): SinonSandbox;
};
}
type SinonStatic = SinonSandbox & LegacySandbox & SinonApi;
}
declare const Sinon: Sinon.SinonStatic;
export = Sinon;
export as namespace sinon;
|