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 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="nodejs.org:node-version" content="v22.14.0">
<title>Test runner | Node.js v22.14.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/test.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
<script>
const storedTheme = localStorage.getItem('theme');
// Follow operating system theme preference
if (storedTheme === null && window.matchMedia) {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}
</script>
<style>@media(max-width:494px){.with-34-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:534px){.with-39-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:574px){.with-44-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:1064px){.with-71-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:686px){.with-58-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:622px){.with-50-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:670px){.with-56-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}</style>
</head>
<body class="alt apidoc" id="api-section-test">
<a href="#apicontent" class="skip-to-content">Skip to content</a>
<div id="content" class="clearfix">
<div role="navigation" id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test active">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul>
</div>
<div id="column1" data-id="test" class="interior">
<header class="header">
<div class="header-container">
<h1>Node.js v22.14.0 documentation</h1>
<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
<svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
<path fill="none" d="M0 0h24v24H0z" />
<path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
<path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
</svg>
</button>
</div>
<div id="gtoc">
<ul>
<li class="pinned-header">Node.js v22.14.0</li>
<li class="picker-header">
<a href="#toc-picker" aria-controls="toc-picker">
<span class="picker-arrow"></span>
Table of contents
</a>
<div class="picker" tabindex="-1"><div class="toc"><ul id="toc-picker">
<li><span class="stability_2"><a href="#test-runner">Test runner</a></span>
<ul>
<li><a href="#subtests">Subtests</a></li>
<li><a href="#skipping-tests">Skipping tests</a></li>
<li><a href="#todo-tests">TODO tests</a></li>
<li><a href="#describe-and-it-aliases"><code>describe()</code> and <code>it()</code> aliases</a></li>
<li><a href="#only-tests"><code>only</code> tests</a></li>
<li><a href="#filtering-tests-by-name">Filtering tests by name</a></li>
<li><a href="#extraneous-asynchronous-activity">Extraneous asynchronous activity</a></li>
<li><span class="stability_1"><a href="#watch-mode">Watch mode</a></span></li>
<li><a href="#running-tests-from-the-command-line">Running tests from the command line</a>
<ul>
<li><a href="#test-runner-execution-model">Test runner execution model</a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#collecting-code-coverage">Collecting code coverage</a></span>
<ul>
<li><a href="#coverage-reporters">Coverage reporters</a></li>
</ul>
</li>
<li><a href="#mocking">Mocking</a>
<ul>
<li><a href="#timers">Timers</a></li>
<li><a href="#dates">Dates</a></li>
</ul>
</li>
<li><a href="#snapshot-testing">Snapshot testing</a></li>
<li><a href="#test-reporters">Test reporters</a>
<ul>
<li><a href="#custom-reporters">Custom reporters</a></li>
<li><a href="#multiple-reporters">Multiple reporters</a></li>
</ul>
</li>
<li><a href="#runoptions"><code>run([options])</code></a></li>
<li><a href="#suitename-options-fn"><code>suite([name][, options][, fn])</code></a></li>
<li><a href="#suiteskipname-options-fn"><code>suite.skip([name][, options][, fn])</code></a></li>
<li><a href="#suitetodoname-options-fn"><code>suite.todo([name][, options][, fn])</code></a></li>
<li><a href="#suiteonlyname-options-fn"><code>suite.only([name][, options][, fn])</code></a></li>
<li><a href="#testname-options-fn"><code>test([name][, options][, fn])</code></a></li>
<li><a href="#testskipname-options-fn"><code>test.skip([name][, options][, fn])</code></a></li>
<li><a href="#testtodoname-options-fn"><code>test.todo([name][, options][, fn])</code></a></li>
<li><a href="#testonlyname-options-fn"><code>test.only([name][, options][, fn])</code></a></li>
<li><a href="#describename-options-fn"><code>describe([name][, options][, fn])</code></a></li>
<li><a href="#describeskipname-options-fn"><code>describe.skip([name][, options][, fn])</code></a></li>
<li><a href="#describetodoname-options-fn"><code>describe.todo([name][, options][, fn])</code></a></li>
<li><a href="#describeonlyname-options-fn"><code>describe.only([name][, options][, fn])</code></a></li>
<li><a href="#itname-options-fn"><code>it([name][, options][, fn])</code></a></li>
<li><a href="#itskipname-options-fn"><code>it.skip([name][, options][, fn])</code></a></li>
<li><a href="#ittodoname-options-fn"><code>it.todo([name][, options][, fn])</code></a></li>
<li><a href="#itonlyname-options-fn"><code>it.only([name][, options][, fn])</code></a></li>
<li><a href="#beforefn-options"><code>before([fn][, options])</code></a></li>
<li><a href="#afterfn-options"><code>after([fn][, options])</code></a></li>
<li><a href="#beforeeachfn-options"><code>beforeEach([fn][, options])</code></a></li>
<li><a href="#aftereachfn-options"><code>afterEach([fn][, options])</code></a></li>
<li><a href="#assert"><code>assert</code></a>
<ul>
<li><a href="#assertregistername-fn"><code>assert.register(name, fn)</code></a></li>
</ul>
</li>
<li><a href="#snapshot"><code>snapshot</code></a>
<ul>
<li><a href="#snapshotsetdefaultsnapshotserializersserializers"><code>snapshot.setDefaultSnapshotSerializers(serializers)</code></a></li>
<li><a href="#snapshotsetresolvesnapshotpathfn"><code>snapshot.setResolveSnapshotPath(fn)</code></a></li>
</ul>
</li>
<li><a href="#class-mockfunctioncontext">Class: <code>MockFunctionContext</code></a>
<ul>
<li><a href="#ctxcalls"><code>ctx.calls</code></a></li>
<li><a href="#ctxcallcount"><code>ctx.callCount()</code></a></li>
<li><a href="#ctxmockimplementationimplementation"><code>ctx.mockImplementation(implementation)</code></a></li>
<li><a href="#ctxmockimplementationonceimplementation-oncall"><code>ctx.mockImplementationOnce(implementation[, onCall])</code></a></li>
<li><a href="#ctxresetcalls"><code>ctx.resetCalls()</code></a></li>
<li><a href="#ctxrestore"><code>ctx.restore()</code></a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#class-mockmodulecontext">Class: <code>MockModuleContext</code></a></span>
<ul>
<li><a href="#ctxrestore_1"><code>ctx.restore()</code></a></li>
</ul>
</li>
<li><a href="#class-mocktracker">Class: <code>MockTracker</code></a>
<ul>
<li><a href="#mockfnoriginal-implementation-options"><code>mock.fn([original[, implementation]][, options])</code></a></li>
<li><a href="#mockgetterobject-methodname-implementation-options"><code>mock.getter(object, methodName[, implementation][, options])</code></a></li>
<li><a href="#mockmethodobject-methodname-implementation-options"><code>mock.method(object, methodName[, implementation][, options])</code></a></li>
<li><span class="stability_1"><a href="#mockmodulespecifier-options"><code>mock.module(specifier[, options])</code></a></span></li>
<li><a href="#mockreset"><code>mock.reset()</code></a></li>
<li><a href="#mockrestoreall"><code>mock.restoreAll()</code></a></li>
<li><a href="#mocksetterobject-methodname-implementation-options"><code>mock.setter(object, methodName[, implementation][, options])</code></a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#class-mocktimers">Class: <code>MockTimers</code></a></span>
<ul>
<li><a href="#timersenableenableoptions"><code>timers.enable([enableOptions])</code></a></li>
<li><a href="#timersreset"><code>timers.reset()</code></a></li>
<li><a href="#timerssymboldispose"><code>timers[Symbol.dispose]()</code></a></li>
<li><a href="#timerstickmilliseconds"><code>timers.tick([milliseconds])</code></a>
<ul>
<li><a href="#using-clear-functions">Using clear functions</a></li>
<li><a href="#working-with-nodejs-timers-modules">Working with Node.js timers modules</a></li>
</ul>
</li>
<li><a href="#timersrunall"><code>timers.runAll()</code></a></li>
<li><a href="#timerssettimemilliseconds"><code>timers.setTime(milliseconds)</code></a>
<ul>
<li><a href="#dates-and-timers-working-together">Dates and Timers working together</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#class-testsstream">Class: <code>TestsStream</code></a>
<ul>
<li><a href="#event-testcoverage">Event: <code>'test:coverage'</code></a></li>
<li><a href="#event-testcomplete">Event: <code>'test:complete'</code></a></li>
<li><a href="#event-testdequeue">Event: <code>'test:dequeue'</code></a></li>
<li><a href="#event-testdiagnostic">Event: <code>'test:diagnostic'</code></a></li>
<li><a href="#event-testenqueue">Event: <code>'test:enqueue'</code></a></li>
<li><a href="#event-testfail">Event: <code>'test:fail'</code></a></li>
<li><a href="#event-testpass">Event: <code>'test:pass'</code></a></li>
<li><a href="#event-testplan">Event: <code>'test:plan'</code></a></li>
<li><a href="#event-teststart">Event: <code>'test:start'</code></a></li>
<li><a href="#event-teststderr">Event: <code>'test:stderr'</code></a></li>
<li><a href="#event-teststdout">Event: <code>'test:stdout'</code></a></li>
<li><a href="#event-testsummary">Event: <code>'test:summary'</code></a></li>
<li><a href="#event-testwatchdrained">Event: <code>'test:watch:drained'</code></a></li>
</ul>
</li>
<li><a href="#class-testcontext">Class: <code>TestContext</code></a>
<ul>
<li><a href="#contextbeforefn-options"><code>context.before([fn][, options])</code></a></li>
<li><a href="#contextbeforeeachfn-options"><code>context.beforeEach([fn][, options])</code></a></li>
<li><a href="#contextafterfn-options"><code>context.after([fn][, options])</code></a></li>
<li><a href="#contextaftereachfn-options"><code>context.afterEach([fn][, options])</code></a></li>
<li><a href="#contextassert"><code>context.assert</code></a>
<ul>
<li><a href="#contextassertfilesnapshotvalue-path-options"><code>context.assert.fileSnapshot(value, path[, options])</code></a></li>
<li><a href="#contextassertsnapshotvalue-options"><code>context.assert.snapshot(value[, options])</code></a></li>
</ul>
</li>
<li><a href="#contextdiagnosticmessage"><code>context.diagnostic(message)</code></a></li>
<li><a href="#contextfilepath"><code>context.filePath</code></a></li>
<li><a href="#contextfullname"><code>context.fullName</code></a></li>
<li><a href="#contextname"><code>context.name</code></a></li>
<li><a href="#contextplancount"><code>context.plan(count)</code></a></li>
<li><a href="#contextrunonlyshouldrunonlytests"><code>context.runOnly(shouldRunOnlyTests)</code></a></li>
<li><a href="#contextsignal"><code>context.signal</code></a></li>
<li><a href="#contextskipmessage"><code>context.skip([message])</code></a></li>
<li><a href="#contexttodomessage"><code>context.todo([message])</code></a></li>
<li><a href="#contexttestname-options-fn"><code>context.test([name][, options][, fn])</code></a></li>
<li><a href="#contextwaitforcondition-options"><code>context.waitFor(condition[, options])</code></a></li>
</ul>
</li>
<li><a href="#class-suitecontext">Class: <code>SuiteContext</code></a>
<ul>
<li><a href="#contextfilepath_1"><code>context.filePath</code></a></li>
<li><a href="#contextname_1"><code>context.name</code></a></li>
<li><a href="#contextsignal_1"><code>context.signal</code></a></li>
</ul>
</li>
</ul>
</li>
</ul></div></div>
</li>
<li class="picker-header">
<a href="#gtoc-picker" aria-controls="gtoc-picker">
<span class="picker-arrow"></span>
Index
</a>
<div class="picker" tabindex="-1" id="gtoc-picker"><ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
<li>
<a href="index.html">Index</a>
</li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test active">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul></div>
</li>
<li class="picker-header">
<a href="#alt-docs" aria-controls="alt-docs">
<span class="picker-arrow"></span>
Other versions
</a>
<div class="picker" tabindex="-1"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v23.x/api/test.html">23.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v22.x/api/test.html">22.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v21.x/api/test.html">21.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v20.x/api/test.html">20.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v19.x/api/test.html">19.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v18.x/api/test.html">18.x <b>LTS</b></a></li></ol></div>
</li>
<li class="picker-header">
<a href="#options-picker" aria-controls="options-picker">
<span class="picker-arrow"></span>
Options
</a>
<div class="picker" tabindex="-1">
<ul id="options-picker">
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="test.json">View as JSON</a>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/test.md">Edit on GitHub</a></li>
</ul>
</div>
</li>
</ul>
</div>
<hr>
</header>
<details role="navigation" id="toc" open><summary>Table of contents</summary><ul>
<li><span class="stability_2"><a href="#test-runner">Test runner</a></span>
<ul>
<li><a href="#subtests">Subtests</a></li>
<li><a href="#skipping-tests">Skipping tests</a></li>
<li><a href="#todo-tests">TODO tests</a></li>
<li><a href="#describe-and-it-aliases"><code>describe()</code> and <code>it()</code> aliases</a></li>
<li><a href="#only-tests"><code>only</code> tests</a></li>
<li><a href="#filtering-tests-by-name">Filtering tests by name</a></li>
<li><a href="#extraneous-asynchronous-activity">Extraneous asynchronous activity</a></li>
<li><span class="stability_1"><a href="#watch-mode">Watch mode</a></span></li>
<li><a href="#running-tests-from-the-command-line">Running tests from the command line</a>
<ul>
<li><a href="#test-runner-execution-model">Test runner execution model</a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#collecting-code-coverage">Collecting code coverage</a></span>
<ul>
<li><a href="#coverage-reporters">Coverage reporters</a></li>
</ul>
</li>
<li><a href="#mocking">Mocking</a>
<ul>
<li><a href="#timers">Timers</a></li>
<li><a href="#dates">Dates</a></li>
</ul>
</li>
<li><a href="#snapshot-testing">Snapshot testing</a></li>
<li><a href="#test-reporters">Test reporters</a>
<ul>
<li><a href="#custom-reporters">Custom reporters</a></li>
<li><a href="#multiple-reporters">Multiple reporters</a></li>
</ul>
</li>
<li><a href="#runoptions"><code>run([options])</code></a></li>
<li><a href="#suitename-options-fn"><code>suite([name][, options][, fn])</code></a></li>
<li><a href="#suiteskipname-options-fn"><code>suite.skip([name][, options][, fn])</code></a></li>
<li><a href="#suitetodoname-options-fn"><code>suite.todo([name][, options][, fn])</code></a></li>
<li><a href="#suiteonlyname-options-fn"><code>suite.only([name][, options][, fn])</code></a></li>
<li><a href="#testname-options-fn"><code>test([name][, options][, fn])</code></a></li>
<li><a href="#testskipname-options-fn"><code>test.skip([name][, options][, fn])</code></a></li>
<li><a href="#testtodoname-options-fn"><code>test.todo([name][, options][, fn])</code></a></li>
<li><a href="#testonlyname-options-fn"><code>test.only([name][, options][, fn])</code></a></li>
<li><a href="#describename-options-fn"><code>describe([name][, options][, fn])</code></a></li>
<li><a href="#describeskipname-options-fn"><code>describe.skip([name][, options][, fn])</code></a></li>
<li><a href="#describetodoname-options-fn"><code>describe.todo([name][, options][, fn])</code></a></li>
<li><a href="#describeonlyname-options-fn"><code>describe.only([name][, options][, fn])</code></a></li>
<li><a href="#itname-options-fn"><code>it([name][, options][, fn])</code></a></li>
<li><a href="#itskipname-options-fn"><code>it.skip([name][, options][, fn])</code></a></li>
<li><a href="#ittodoname-options-fn"><code>it.todo([name][, options][, fn])</code></a></li>
<li><a href="#itonlyname-options-fn"><code>it.only([name][, options][, fn])</code></a></li>
<li><a href="#beforefn-options"><code>before([fn][, options])</code></a></li>
<li><a href="#afterfn-options"><code>after([fn][, options])</code></a></li>
<li><a href="#beforeeachfn-options"><code>beforeEach([fn][, options])</code></a></li>
<li><a href="#aftereachfn-options"><code>afterEach([fn][, options])</code></a></li>
<li><a href="#assert"><code>assert</code></a>
<ul>
<li><a href="#assertregistername-fn"><code>assert.register(name, fn)</code></a></li>
</ul>
</li>
<li><a href="#snapshot"><code>snapshot</code></a>
<ul>
<li><a href="#snapshotsetdefaultsnapshotserializersserializers"><code>snapshot.setDefaultSnapshotSerializers(serializers)</code></a></li>
<li><a href="#snapshotsetresolvesnapshotpathfn"><code>snapshot.setResolveSnapshotPath(fn)</code></a></li>
</ul>
</li>
<li><a href="#class-mockfunctioncontext">Class: <code>MockFunctionContext</code></a>
<ul>
<li><a href="#ctxcalls"><code>ctx.calls</code></a></li>
<li><a href="#ctxcallcount"><code>ctx.callCount()</code></a></li>
<li><a href="#ctxmockimplementationimplementation"><code>ctx.mockImplementation(implementation)</code></a></li>
<li><a href="#ctxmockimplementationonceimplementation-oncall"><code>ctx.mockImplementationOnce(implementation[, onCall])</code></a></li>
<li><a href="#ctxresetcalls"><code>ctx.resetCalls()</code></a></li>
<li><a href="#ctxrestore"><code>ctx.restore()</code></a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#class-mockmodulecontext">Class: <code>MockModuleContext</code></a></span>
<ul>
<li><a href="#ctxrestore_1"><code>ctx.restore()</code></a></li>
</ul>
</li>
<li><a href="#class-mocktracker">Class: <code>MockTracker</code></a>
<ul>
<li><a href="#mockfnoriginal-implementation-options"><code>mock.fn([original[, implementation]][, options])</code></a></li>
<li><a href="#mockgetterobject-methodname-implementation-options"><code>mock.getter(object, methodName[, implementation][, options])</code></a></li>
<li><a href="#mockmethodobject-methodname-implementation-options"><code>mock.method(object, methodName[, implementation][, options])</code></a></li>
<li><span class="stability_1"><a href="#mockmodulespecifier-options"><code>mock.module(specifier[, options])</code></a></span></li>
<li><a href="#mockreset"><code>mock.reset()</code></a></li>
<li><a href="#mockrestoreall"><code>mock.restoreAll()</code></a></li>
<li><a href="#mocksetterobject-methodname-implementation-options"><code>mock.setter(object, methodName[, implementation][, options])</code></a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#class-mocktimers">Class: <code>MockTimers</code></a></span>
<ul>
<li><a href="#timersenableenableoptions"><code>timers.enable([enableOptions])</code></a></li>
<li><a href="#timersreset"><code>timers.reset()</code></a></li>
<li><a href="#timerssymboldispose"><code>timers[Symbol.dispose]()</code></a></li>
<li><a href="#timerstickmilliseconds"><code>timers.tick([milliseconds])</code></a>
<ul>
<li><a href="#using-clear-functions">Using clear functions</a></li>
<li><a href="#working-with-nodejs-timers-modules">Working with Node.js timers modules</a></li>
</ul>
</li>
<li><a href="#timersrunall"><code>timers.runAll()</code></a></li>
<li><a href="#timerssettimemilliseconds"><code>timers.setTime(milliseconds)</code></a>
<ul>
<li><a href="#dates-and-timers-working-together">Dates and Timers working together</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#class-testsstream">Class: <code>TestsStream</code></a>
<ul>
<li><a href="#event-testcoverage">Event: <code>'test:coverage'</code></a></li>
<li><a href="#event-testcomplete">Event: <code>'test:complete'</code></a></li>
<li><a href="#event-testdequeue">Event: <code>'test:dequeue'</code></a></li>
<li><a href="#event-testdiagnostic">Event: <code>'test:diagnostic'</code></a></li>
<li><a href="#event-testenqueue">Event: <code>'test:enqueue'</code></a></li>
<li><a href="#event-testfail">Event: <code>'test:fail'</code></a></li>
<li><a href="#event-testpass">Event: <code>'test:pass'</code></a></li>
<li><a href="#event-testplan">Event: <code>'test:plan'</code></a></li>
<li><a href="#event-teststart">Event: <code>'test:start'</code></a></li>
<li><a href="#event-teststderr">Event: <code>'test:stderr'</code></a></li>
<li><a href="#event-teststdout">Event: <code>'test:stdout'</code></a></li>
<li><a href="#event-testsummary">Event: <code>'test:summary'</code></a></li>
<li><a href="#event-testwatchdrained">Event: <code>'test:watch:drained'</code></a></li>
</ul>
</li>
<li><a href="#class-testcontext">Class: <code>TestContext</code></a>
<ul>
<li><a href="#contextbeforefn-options"><code>context.before([fn][, options])</code></a></li>
<li><a href="#contextbeforeeachfn-options"><code>context.beforeEach([fn][, options])</code></a></li>
<li><a href="#contextafterfn-options"><code>context.after([fn][, options])</code></a></li>
<li><a href="#contextaftereachfn-options"><code>context.afterEach([fn][, options])</code></a></li>
<li><a href="#contextassert"><code>context.assert</code></a>
<ul>
<li><a href="#contextassertfilesnapshotvalue-path-options"><code>context.assert.fileSnapshot(value, path[, options])</code></a></li>
<li><a href="#contextassertsnapshotvalue-options"><code>context.assert.snapshot(value[, options])</code></a></li>
</ul>
</li>
<li><a href="#contextdiagnosticmessage"><code>context.diagnostic(message)</code></a></li>
<li><a href="#contextfilepath"><code>context.filePath</code></a></li>
<li><a href="#contextfullname"><code>context.fullName</code></a></li>
<li><a href="#contextname"><code>context.name</code></a></li>
<li><a href="#contextplancount"><code>context.plan(count)</code></a></li>
<li><a href="#contextrunonlyshouldrunonlytests"><code>context.runOnly(shouldRunOnlyTests)</code></a></li>
<li><a href="#contextsignal"><code>context.signal</code></a></li>
<li><a href="#contextskipmessage"><code>context.skip([message])</code></a></li>
<li><a href="#contexttodomessage"><code>context.todo([message])</code></a></li>
<li><a href="#contexttestname-options-fn"><code>context.test([name][, options][, fn])</code></a></li>
<li><a href="#contextwaitforcondition-options"><code>context.waitFor(condition[, options])</code></a></li>
</ul>
</li>
<li><a href="#class-suitecontext">Class: <code>SuiteContext</code></a>
<ul>
<li><a href="#contextfilepath_1"><code>context.filePath</code></a></li>
<li><a href="#contextname_1"><code>context.name</code></a></li>
<li><a href="#contextsignal_1"><code>context.signal</code></a></li>
</ul>
</li>
</ul>
</li>
</ul></details>
<div role="main" id="apicontent">
<h2>Test runner<span><a class="mark" href="#test-runner" id="test-runner">#</a></span><a aria-hidden="true" class="legacy" id="test_test_runner"></a></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.0.0</td>
<td><p>The test runner is now stable.</p></td></tr>
<tr><td>v18.0.0, v16.17.0</td>
<td><p><span>Added in: v18.0.0, v16.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v22.14.0/lib/test.js">lib/test.js</a></p>
<p>The <code>node:test</code> module facilitates the creation of JavaScript tests.
To access it:</p>
<pre class="with-34-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> test <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> test = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);</code><button class="copy-button">copy</button></pre>
<p>This module is only available under the <code>node:</code> scheme.</p>
<p>Tests created via the <code>test</code> module consist of a single function that is
processed in one of three ways:</p>
<ol>
<li>A synchronous function that is considered failing if it throws an exception,
and is considered passing otherwise.</li>
<li>A function that returns a <code>Promise</code> that is considered failing if the
<code>Promise</code> rejects, and is considered passing if the <code>Promise</code> fulfills.</li>
<li>A function that receives a callback function. If the callback receives any
truthy value as its first argument, the test is considered failing. If a
falsy value is passed as the first argument to the callback, the test is
considered passing. If the test function receives a callback function and
also returns a <code>Promise</code>, the test will fail.</li>
</ol>
<p>The following example illustrates how tests are written using the
<code>test</code> module.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'synchronous passing test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This test passes because it does not throw an exception.</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'synchronous failing test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This test fails because it throws an exception.</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'asynchronous passing test'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-comment">// This test passes because the Promise returned by the async</span>
<span class="hljs-comment">// function is settled and not rejected.</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'asynchronous failing test'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-comment">// This test fails because the Promise returned by the async</span>
<span class="hljs-comment">// function is rejected.</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'failing test using Promises'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// Promises can be used directly as well.</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> {
<span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
<span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'this will cause the test to fail'</span>));
});
});
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'callback passing test'</span>, <span class="hljs-function">(<span class="hljs-params">t, done</span>) =></span> {
<span class="hljs-comment">// done() is the callback function. When the setImmediate() runs, it invokes</span>
<span class="hljs-comment">// done() with no arguments.</span>
<span class="hljs-title function_">setImmediate</span>(done);
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'callback failing test'</span>, <span class="hljs-function">(<span class="hljs-params">t, done</span>) =></span> {
<span class="hljs-comment">// When the setImmediate() runs, done() is invoked with an Error object and</span>
<span class="hljs-comment">// the test fails.</span>
<span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
<span class="hljs-title function_">done</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'callback failure'</span>));
});
});</code> <button class="copy-button">copy</button></pre>
<p>If any tests fail, the process exit code is set to <code>1</code>.</p>
<section><h3>Subtests<span><a class="mark" href="#subtests" id="subtests">#</a></span><a aria-hidden="true" class="legacy" id="test_subtests"></a></h3>
<p>The test context's <code>test()</code> method allows subtests to be created.
It allows you to structure your tests in a hierarchical manner,
where you can create nested tests within a larger test.
This method behaves identically to the top level <code>test()</code> function.
The following example demonstrates the creation of a
top level test with two subtests.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'subtest 1'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
});
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'subtest 2'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>);
});
});</code> <button class="copy-button">copy</button></pre>
<blockquote>
<p><strong>Note:</strong> <code>beforeEach</code> and <code>afterEach</code> hooks are triggered
between each subtest execution.</p>
</blockquote>
<p>In this example, <code>await</code> is used to ensure that both subtests have completed.
This is necessary because tests do not wait for their subtests to
complete, unlike tests created within suites.
Any subtests that are still outstanding when their parent finishes
are cancelled and treated as failures. Any subtest failures cause the parent
test to fail.</p>
</section><section><h3>Skipping tests<span><a class="mark" href="#skipping-tests" id="skipping-tests">#</a></span><a aria-hidden="true" class="legacy" id="test_skipping_tests"></a></h3>
<p>Individual tests can be skipped by passing the <code>skip</code> option to the test, or by
calling the test context's <code>skip()</code> method as shown in the
following example.</p>
<pre><code class="language-js"><span class="hljs-comment">// The skip option is used, but no message is provided.</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'skip option'</span>, { <span class="hljs-attr">skip</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This code is never executed.</span>
});
<span class="hljs-comment">// The skip option is used, and a message is provided.</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'skip option with message'</span>, { <span class="hljs-attr">skip</span>: <span class="hljs-string">'this is skipped'</span> }, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This code is never executed.</span>
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'skip() method'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span>
t.<span class="hljs-title function_">skip</span>();
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'skip() method with message'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span>
t.<span class="hljs-title function_">skip</span>(<span class="hljs-string">'this is skipped'</span>);
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3>TODO tests<span><a class="mark" href="#todo-tests" id="todo-tests">#</a></span><a aria-hidden="true" class="legacy" id="test_todo_tests"></a></h3>
<p>Individual tests can be marked as flaky or incomplete by passing the <code>todo</code>
option to the test, or by calling the test context's <code>todo()</code> method, as shown
in the following example. These tests represent a pending implementation or bug
that needs to be fixed. TODO tests are executed, but are not treated as test
failures, and therefore do not affect the process exit code. If a test is marked
as both TODO and skipped, the TODO option is ignored.</p>
<pre><code class="language-js"><span class="hljs-comment">// The todo option is used, but no message is provided.</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'todo option'</span>, { <span class="hljs-attr">todo</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This code is executed, but not treated as a failure.</span>
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'this does not fail the test'</span>);
});
<span class="hljs-comment">// The todo option is used, and a message is provided.</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'todo option with message'</span>, { <span class="hljs-attr">todo</span>: <span class="hljs-string">'this is a todo test'</span> }, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This code is executed.</span>
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'todo() method'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-title function_">todo</span>();
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'todo() method with message'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-title function_">todo</span>(<span class="hljs-string">'this is a todo test and is not treated as a failure'</span>);
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'this does not fail the test'</span>);
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3><code>describe()</code> and <code>it()</code> aliases<span><a class="mark" href="#describe-and-it-aliases" id="describe-and-it-aliases">#</a></span><a aria-hidden="true" class="legacy" id="test_describe_and_it_aliases"></a></h3>
<p>Suites and tests can also be written using the <code>describe()</code> and <code>it()</code>
functions. <a href="#describename-options-fn"><code>describe()</code></a> is an alias for <a href="#suitename-options-fn"><code>suite()</code></a>, and <a href="#itname-options-fn"><code>it()</code></a> is an
alias for <a href="#testname-options-fn"><code>test()</code></a>.</p>
<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'A thing'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-title function_">it</span>(<span class="hljs-string">'should work'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
});
<span class="hljs-title function_">it</span>(<span class="hljs-string">'should be ok'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>);
});
<span class="hljs-title function_">describe</span>(<span class="hljs-string">'a nested thing'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-title function_">it</span>(<span class="hljs-string">'should work'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">3</span>, <span class="hljs-number">3</span>);
});
});
});</code> <button class="copy-button">copy</button></pre>
<p><code>describe()</code> and <code>it()</code> are imported from the <code>node:test</code> module.</p>
<pre class="with-46-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { describe, it } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { describe, it } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);</code><button class="copy-button">copy</button></pre>
</section><section><h3><code>only</code> tests<span><a class="mark" href="#only-tests" id="only-tests">#</a></span><a aria-hidden="true" class="legacy" id="test_only_tests"></a></h3>
<p>If Node.js is started with the <a href="cli.html#--test-only"><code>--test-only</code></a> command-line option, or test
isolation is disabled, it is possible to skip all tests except for a selected
subset by passing the <code>only</code> option to the tests that should run. When a test
with the <code>only</code> option is set, all subtests are also run.
If a suite has the <code>only</code> option set, all tests within the suite are run,
unless it has descendants with the <code>only</code> option set, in which case only those
tests are run.</p>
<p>When using <a href="#subtests">subtests</a> within a <code>test()</code>/<code>it()</code>, it is required to mark
all ancestor tests with the <code>only</code> option to run only a
selected subset of tests.</p>
<p>The test context's <code>runOnly()</code>
method can be used to implement the same behavior at the subtest level. Tests
that are not executed are omitted from the test runner output.</p>
<pre><code class="language-js"><span class="hljs-comment">// Assume Node.js is run with the --test-only command-line option.</span>
<span class="hljs-comment">// The suite's 'only' option is set, so these tests are run.</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'this test is run'</span>, { <span class="hljs-attr">only</span>: <span class="hljs-literal">true</span> }, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-comment">// Within this test, all subtests are run by default.</span>
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'running subtest'</span>);
<span class="hljs-comment">// The test context can be updated to run subtests with the 'only' option.</span>
t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">true</span>);
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now skipped'</span>);
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is run'</span>, { <span class="hljs-attr">only</span>: <span class="hljs-literal">true</span> });
<span class="hljs-comment">// Switch the context back to execute all tests.</span>
t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">false</span>);
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now run'</span>);
<span class="hljs-comment">// Explicitly do not run these tests.</span>
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'skipped subtest 3'</span>, { <span class="hljs-attr">only</span>: <span class="hljs-literal">false</span> });
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'skipped subtest 4'</span>, { <span class="hljs-attr">skip</span>: <span class="hljs-literal">true</span> });
});
<span class="hljs-comment">// The 'only' option is not set, so this test is skipped.</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'this test is not run'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// This code is not run.</span>
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'fail'</span>);
});
<span class="hljs-title function_">describe</span>(<span class="hljs-string">'a suite'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// The 'only' option is set, so this test is run.</span>
<span class="hljs-title function_">it</span>(<span class="hljs-string">'this test is run'</span>, { <span class="hljs-attr">only</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// This code is run.</span>
});
<span class="hljs-title function_">it</span>(<span class="hljs-string">'this test is not run'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// This code is not run.</span>
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'fail'</span>);
});
});
describe.<span class="hljs-title function_">only</span>(<span class="hljs-string">'a suite'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// The 'only' option is set, so this test is run.</span>
<span class="hljs-title function_">it</span>(<span class="hljs-string">'this test is run'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// This code is run.</span>
});
<span class="hljs-title function_">it</span>(<span class="hljs-string">'this test is run'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// This code is run.</span>
});
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3>Filtering tests by name<span><a class="mark" href="#filtering-tests-by-name" id="filtering-tests-by-name">#</a></span><a aria-hidden="true" class="legacy" id="test_filtering_tests_by_name"></a></h3>
<p>The <a href="cli.html#--test-name-pattern"><code>--test-name-pattern</code></a> command-line option can be used to only run
tests whose name matches the provided pattern, and the
<a href="cli.html#--test-skip-pattern"><code>--test-skip-pattern</code></a> option can be used to skip tests whose name
matches the provided pattern. Test name patterns are interpreted as
JavaScript regular expressions. The <code>--test-name-pattern</code> and
<code>--test-skip-pattern</code> options can be specified multiple times in order to run
nested tests. For each test that is executed, any corresponding test hooks,
such as <code>beforeEach()</code>, are also run. Tests that are not executed are omitted
from the test runner output.</p>
<p>Given the following test file, starting Node.js with the
<code>--test-name-pattern="test [1-3]"</code> option would cause the test runner to execute
<code>test 1</code>, <code>test 2</code>, and <code>test 3</code>. If <code>test 1</code> did not match the test name
pattern, then its subtests would not execute, despite matching the pattern. The
same set of tests could also be executed by passing <code>--test-name-pattern</code>
multiple times (e.g. <code>--test-name-pattern="test 1"</code>,
<code>--test-name-pattern="test 2"</code>, etc.).</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'test 1'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 2'</span>);
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 3'</span>);
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'Test 4'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'Test 5'</span>);
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 6'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>Test name patterns can also be specified using regular expression literals. This
allows regular expression flags to be used. In the previous example, starting
Node.js with <code>--test-name-pattern="/test [4-5]/i"</code> (or <code>--test-skip-pattern="/test [4-5]/i"</code>)
would match <code>Test 4</code> and <code>Test 5</code> because the pattern is case-insensitive.</p>
<p>To match a single test with a pattern, you can prefix it with all its ancestor
test names separated by space, to ensure it is unique.
For example, given the following test file:</p>
<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'test 1'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-title function_">it</span>(<span class="hljs-string">'some test'</span>);
});
<span class="hljs-title function_">describe</span>(<span class="hljs-string">'test 2'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-title function_">it</span>(<span class="hljs-string">'some test'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>Starting Node.js with <code>--test-name-pattern="test 1 some test"</code> would match
only <code>some test</code> in <code>test 1</code>.</p>
<p>Test name patterns do not change the set of files that the test runner executes.</p>
<p>If both <code>--test-name-pattern</code> and <code>--test-skip-pattern</code> are supplied,
tests must satisfy <strong>both</strong> requirements in order to be executed.</p>
</section><section><h3>Extraneous asynchronous activity<span><a class="mark" href="#extraneous-asynchronous-activity" id="extraneous-asynchronous-activity">#</a></span><a aria-hidden="true" class="legacy" id="test_extraneous_asynchronous_activity"></a></h3>
<p>Once a test function finishes executing, the results are reported as quickly
as possible while maintaining the order of the tests. However, it is possible
for the test function to generate asynchronous activity that outlives the test
itself. The test runner handles this type of activity, but does not delay the
reporting of test results in order to accommodate it.</p>
<p>In the following example, a test completes with two <code>setImmediate()</code>
operations still outstanding. The first <code>setImmediate()</code> attempts to create a
new subtest. Because the parent test has already finished and output its
results, the new subtest is immediately marked as failed, and reported later
to the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p>
<p>The second <code>setImmediate()</code> creates an <code>uncaughtException</code> event.
<code>uncaughtException</code> and <code>unhandledRejection</code> events originating from a completed
test are marked as failed by the <code>test</code> module and reported as diagnostic
warnings at the top level by the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'a test that creates asynchronous activity'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'subtest that is created too late'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'error1'</span>);
});
});
<span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'error2'</span>);
});
<span class="hljs-comment">// The test finishes after this line.</span>
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3>Watch mode<span><a class="mark" href="#watch-mode" id="watch-mode">#</a></span><a aria-hidden="true" class="legacy" id="test_watch_mode"></a></h3>
<div class="api_metadata">
<span>Added in: v19.2.0, v18.13.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<p>The Node.js test runner supports running in watch mode by passing the <code>--watch</code> flag:</p>
<pre><code class="language-bash">node --<span class="hljs-built_in">test</span> --watch</code> <button class="copy-button">copy</button></pre>
<p>In watch mode, the test runner will watch for changes to test files and
their dependencies. When a change is detected, the test runner will
rerun the tests affected by the change.
The test runner will continue to run until the process is terminated.</p>
</section><section><h3>Running tests from the command line<span><a class="mark" href="#running-tests-from-the-command-line" id="running-tests-from-the-command-line">#</a></span><a aria-hidden="true" class="legacy" id="test_running_tests_from_the_command_line"></a></h3>
<p>The Node.js test runner can be invoked from the command line by passing the
<a href="cli.html#--test"><code>--test</code></a> flag:</p>
<pre><code class="language-bash">node --<span class="hljs-built_in">test</span></code> <button class="copy-button">copy</button></pre>
<p>By default, Node.js will run all files matching these patterns:</p>
<ul>
<li><code>**/*.test.{cjs,mjs,js}</code></li>
<li><code>**/*-test.{cjs,mjs,js}</code></li>
<li><code>**/*_test.{cjs,mjs,js}</code></li>
<li><code>**/test-*.{cjs,mjs,js}</code></li>
<li><code>**/test.{cjs,mjs,js}</code></li>
<li><code>**/test/**/*.{cjs,mjs,js}</code></li>
</ul>
<p>When <a href="cli.html#--experimental-strip-types"><code>--experimental-strip-types</code></a> is supplied, the following
additional patterns are matched:</p>
<ul>
<li><code>**/*.test.{cts,mts,ts}</code></li>
<li><code>**/*-test.{cts,mts,ts}</code></li>
<li><code>**/*_test.{cts,mts,ts}</code></li>
<li><code>**/test-*.{cts,mts,ts}</code></li>
<li><code>**/test.{cts,mts,ts}</code></li>
<li><code>**/test/**/*.{cts,mts,ts}</code></li>
</ul>
<p>Alternatively, one or more glob patterns can be provided as the
final argument(s) to the Node.js command, as shown below.
Glob patterns follow the behavior of <a href="https://man7.org/linux/man-pages/man7/glob.7.html"><code>glob(7)</code></a>.
The glob patterns should be enclosed in double quotes on the command line to
prevent shell expansion, which can reduce portability across systems.</p>
<pre><code class="language-bash">node --<span class="hljs-built_in">test</span> <span class="hljs-string">"**/*.test.js"</span> <span class="hljs-string">"**/*.spec.js"</span></code> <button class="copy-button">copy</button></pre>
<p>Matching files are executed as test files.
More information on the test file execution can be found
in the <a href="#test-runner-execution-model">test runner execution model</a> section.</p>
<h4>Test runner execution model<span><a class="mark" href="#test-runner-execution-model" id="test-runner-execution-model">#</a></span><a aria-hidden="true" class="legacy" id="test_test_runner_execution_model"></a></h4>
<p>When process-level test isolation is enabled, each matching test file is
executed in a separate child process. The maximum number of child processes
running at any time is controlled by the <a href="cli.html#--test-concurrency"><code>--test-concurrency</code></a> flag. If the
child process finishes with an exit code of 0, the test is considered passing.
Otherwise, the test is considered to be a failure. Test files must be executable
by Node.js, but are not required to use the <code>node:test</code> module internally.</p>
<p>Each test file is executed as if it was a regular script. That is, if the test
file itself uses <code>node:test</code> to define tests, all of those tests will be
executed within a single application thread, regardless of the value of the
<code>concurrency</code> option of <a href="#testname-options-fn"><code>test()</code></a>.</p>
<p>When process-level test isolation is disabled, each matching test file is
imported into the test runner process. Once all test files have been loaded, the
top level tests are executed with a concurrency of one. Because the test files
are all run within the same context, it is possible for tests to interact with
each other in ways that are not possible when isolation is enabled. For example,
if a test relies on global state, it is possible for that state to be modified
by a test originating from another file.</p>
</section><section><h3>Collecting code coverage<span><a class="mark" href="#collecting-code-coverage" id="collecting-code-coverage">#</a></span><a aria-hidden="true" class="legacy" id="test_collecting_code_coverage"></a></h3>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<p>When Node.js is started with the <a href="cli.html#--experimental-test-coverage"><code>--experimental-test-coverage</code></a>
command-line flag, code coverage is collected and statistics are reported once
all tests have completed. If the <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a> environment variable is
used to specify a code coverage directory, the generated V8 coverage files are
written to that directory. Node.js core modules and files within
<code>node_modules/</code> directories are, by default, not included in the coverage report.
However, they can be explicitly included via the <a href="cli.html#--test-coverage-include"><code>--test-coverage-include</code></a> flag. If
coverage is enabled, the coverage report is sent to any <a href="#test-reporters">test reporters</a> via
the <code>'test:coverage'</code> event.</p>
<p>Coverage can be disabled on a series of lines using the following
comment syntax:</p>
<pre><code class="language-js"><span class="hljs-comment">/* node:coverage disable */</span>
<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) {
<span class="hljs-comment">// Code in this branch will never be executed, but the lines are ignored for</span>
<span class="hljs-comment">// coverage purposes. All lines following the 'disable' comment are ignored</span>
<span class="hljs-comment">// until a corresponding 'enable' comment is encountered.</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>);
}
<span class="hljs-comment">/* node:coverage enable */</span></code> <button class="copy-button">copy</button></pre>
<p>Coverage can also be disabled for a specified number of lines. After the
specified number of lines, coverage will be automatically reenabled. If the
number of lines is not explicitly provided, a single line is ignored.</p>
<pre><code class="language-js"><span class="hljs-comment">/* node:coverage ignore next */</span>
<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>); }
<span class="hljs-comment">/* node:coverage ignore next 3 */</span>
<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>);
}</code> <button class="copy-button">copy</button></pre>
<h4>Coverage reporters<span><a class="mark" href="#coverage-reporters" id="coverage-reporters">#</a></span><a aria-hidden="true" class="legacy" id="test_coverage_reporters"></a></h4>
<p>The tap and spec reporters will print a summary of the coverage statistics.
There is also an lcov reporter that will generate an lcov file which can be
used as an in depth coverage report.</p>
<pre><code class="language-bash">node --<span class="hljs-built_in">test</span> --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info</code> <button class="copy-button">copy</button></pre>
<ul>
<li>No test results are reported by this reporter.</li>
<li>This reporter should ideally be used alongside another reporter.</li>
</ul>
</section><section><h3>Mocking<span><a class="mark" href="#mocking" id="mocking">#</a></span><a aria-hidden="true" class="legacy" id="test_mocking"></a></h3>
<p>The <code>node:test</code> module supports mocking during testing via a top-level <code>mock</code>
object. The following example creates a spy on a function that adds two numbers
together. The spy is then used to assert that the function was called as
expected.</p>
<pre class="with-39-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { mock, test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on a function'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">const</span> sum = mock.<span class="hljs-title function_">fn</span>(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> {
<span class="hljs-keyword">return</span> a + b;
});
assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">sum</span>(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>), <span class="hljs-number">7</span>);
assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-keyword">const</span> call = sum.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>[<span class="hljs-number">0</span>];
assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">7</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>);
<span class="hljs-comment">// Reset the globally tracked mocks.</span>
mock.<span class="hljs-title function_">reset</span>();
});</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>;
<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { mock, test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on a function'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">const</span> sum = mock.<span class="hljs-title function_">fn</span>(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> {
<span class="hljs-keyword">return</span> a + b;
});
assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">sum</span>(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>), <span class="hljs-number">7</span>);
assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-keyword">const</span> call = sum.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>[<span class="hljs-number">0</span>];
assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">7</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>);
<span class="hljs-comment">// Reset the globally tracked mocks.</span>
mock.<span class="hljs-title function_">reset</span>();
});</code><button class="copy-button">copy</button></pre>
<p>The same mocking functionality is also exposed on the <a href="#class-testcontext"><code>TestContext</code></a> object
of each test. The following example creates a spy on an object method using the
API exposed on the <code>TestContext</code>. The benefit of mocking via the test context is
that the test runner will automatically restore all mocked functionality once
the test finishes.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on an object method'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-keyword">const</span> number = {
<span class="hljs-attr">value</span>: <span class="hljs-number">5</span>,
<span class="hljs-title function_">add</span>(<span class="hljs-params">a</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> + a;
},
};
t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">method</span>(number, <span class="hljs-string">'add'</span>);
assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">add</span>.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-title function_">add</span>(<span class="hljs-number">3</span>), <span class="hljs-number">8</span>);
assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">add</span>.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-keyword">const</span> call = number.<span class="hljs-property">add</span>.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>[<span class="hljs-number">0</span>];
assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>]);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">8</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">target</span>, <span class="hljs-literal">undefined</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">this</span>, number);
});</code> <button class="copy-button">copy</button></pre>
<h4>Timers<span><a class="mark" href="#timers" id="timers">#</a></span><a aria-hidden="true" class="legacy" id="test_timers"></a></h4>
<p>Mocking timers is a technique commonly used in software testing to simulate and
control the behavior of timers, such as <code>setInterval</code> and <code>setTimeout</code>,
without actually waiting for the specified time intervals.</p>
<p>Refer to the <a href="#class-mocktimers"><code>MockTimers</code></a> class for a full list of methods and features.</p>
<p>This allows developers to write more reliable and
predictable tests for time-dependent functionality.</p>
<p>The example below shows how to mock <code>setTimeout</code>.
Using <code>.enable({ apis: ['setTimeout'] });</code>
it will mock the <code>setTimeout</code> functions in the <a href="./timers.html">node:timers</a> and
<a href="./timers.html#timers-promises-api">node:timers/promises</a> modules,
as well as from the Node.js global context.</p>
<p><strong>Note:</strong> Destructuring functions such as
<code>import { setTimeout } from 'node:timers'</code>
is currently not supported by this API.</p>
<pre class="with-44-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { mock, test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">const</span> fn = mock.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-comment">// Reset the globally tracked mocks.</span>
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>();
<span class="hljs-comment">// If you call reset mock instance, it will also reset timers instance</span>
mock.<span class="hljs-title function_">reset</span>();
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { mock, test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">const</span> fn = mock.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-comment">// Reset the globally tracked mocks.</span>
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>();
<span class="hljs-comment">// If you call reset mock instance, it will also reset timers instance</span>
mock.<span class="hljs-title function_">reset</span>();
});</code><button class="copy-button">copy</button></pre>
<p>The same mocking functionality is also exposed in the mock property on the <a href="#class-testcontext"><code>TestContext</code></a> object
of each test. The benefit of mocking via the test context is
that the test runner will automatically restore all mocked timers
functionality once the test finishes.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><button class="copy-button">copy</button></pre>
<h4>Dates<span><a class="mark" href="#dates" id="dates">#</a></span><a aria-hidden="true" class="legacy" id="test_dates"></a></h4>
<p>The mock timers API also allows the mocking of the <code>Date</code> object. This is a
useful feature for testing time-dependent functionality, or to simulate
internal calendar functions such as <code>Date.now()</code>.</p>
<p>The dates implementation is also part of the <a href="#class-mocktimers"><code>MockTimers</code></a> class. Refer to it
for a full list of methods and features.</p>
<p><strong>Note:</strong> Dates and timers are dependent when mocked together. This means that
if you have both the <code>Date</code> and <code>setTimeout</code> mocked, advancing the time will
also advance the mocked date as they simulate a single internal clock.</p>
<p>The example below show how to mock the <code>Date</code> object and obtain the current
<code>Date.now()</code> value.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks the Date object'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>] });
<span class="hljs-comment">// If not specified, the initial date will be based on 0 in the UNIX epoch</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time will also advance the date</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">9999</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks the Date object'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>] });
<span class="hljs-comment">// If not specified, the initial date will be based on 0 in the UNIX epoch</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time will also advance the date</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">9999</span>);
});</code><button class="copy-button">copy</button></pre>
<p>If there is no initial epoch set, the initial date will be based on 0 in the
Unix epoch. This is January 1st, 1970, 00:00:00 UTC. You can set an initial date
by passing a <code>now</code> property to the <code>.enable()</code> method. This value will be used
as the initial date for the mocked <code>Date</code> object. It can either be a positive
integer, or another Date object.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks the Date object with initial time'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-number">100</span> });
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">100</span>);
<span class="hljs-comment">// Advance in time will also advance the date</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">200</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">300</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks the Date object with initial time'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-number">100</span> });
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">100</span>);
<span class="hljs-comment">// Advance in time will also advance the date</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">200</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">300</span>);
});</code><button class="copy-button">copy</button></pre>
<p>You can use the <code>.setTime()</code> method to manually move the mocked date to another
time. This method only accepts a positive integer.</p>
<p><strong>Note:</strong> This method will execute any mocked timers that are in the past
from the new time.</p>
<p>In the below example we are setting a new time for the mocked date.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'sets the time of a date object'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-number">100</span> });
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">100</span>);
<span class="hljs-comment">// Advance in time will also advance the date</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">1000</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">200</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">1200</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'sets the time of a date object'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-number">100</span> });
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">100</span>);
<span class="hljs-comment">// Advance in time will also advance the date</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">1000</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">200</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">1200</span>);
});</code><button class="copy-button">copy</button></pre>
<p>If you have any timer that's set to run in the past, it will be executed as if
the <code>.tick()</code> method has been called. This is useful if you want to test
time-dependent functionality that's already in the past.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runs timers as setTime passes ticks'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">1000</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">800</span>);
<span class="hljs-comment">// Timer is not executed as the time is not yet reached</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">800</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">1200</span>);
<span class="hljs-comment">// Timer is executed as the time is now reached</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">1200</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runs timers as setTime passes ticks'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">1000</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">800</span>);
<span class="hljs-comment">// Timer is not executed as the time is not yet reached</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">800</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">1200</span>);
<span class="hljs-comment">// Timer is executed as the time is now reached</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">1200</span>);
});</code><button class="copy-button">copy</button></pre>
<p>Using <code>.runAll()</code> will execute all timers that are currently in the queue. This
will also advance the mocked date to the time of the last timer that was
executed as if the time has passed.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runs timers as setTime passes ticks'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">1000</span>);
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">2000</span>);
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">3000</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>();
<span class="hljs-comment">// All timers are executed as the time is now reached</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">3</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">3000</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runs timers as setTime passes ticks'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">1000</span>);
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">2000</span>);
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">3000</span>);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>();
<span class="hljs-comment">// All timers are executed as the time is now reached</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">3</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">3000</span>);
});</code><button class="copy-button">copy</button></pre>
</section><section><h3>Snapshot testing<span><a class="mark" href="#snapshot-testing" id="snapshot-testing">#</a></span><a aria-hidden="true" class="legacy" id="test_snapshot_testing"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v23.4.0</td>
<td><p>Snapsnot testing is no longer experimental.</p></td></tr>
<tr><td>v22.3.0</td>
<td><p><span>Added in: v22.3.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>Snapshot tests allow arbitrary values to be serialized into string values and
compared against a set of known good values. The known good values are known as
snapshots, and are stored in a snapshot file. Snapshot files are managed by the
test runner, but are designed to be human readable to aid in debugging. Best
practice is for snapshot files to be checked into source control along with your
test files.</p>
<p>Snapshot files are generated by starting Node.js with the
<a href="cli.html#--test-update-snapshots"><code>--test-update-snapshots</code></a> command-line flag. A separate snapshot file is
generated for each test file. By default, the snapshot file has the same name
as the test file with a <code>.snapshot</code> file extension. This behavior can be
configured using the <code>snapshot.setResolveSnapshotPath()</code> function. Each
snapshot assertion corresponds to an export in the snapshot file.</p>
<p>An example snapshot test is shown below. The first time this test is executed,
it will fail because the corresponding snapshot file does not exist.</p>
<pre><code class="language-js"><span class="hljs-comment">// test.js</span>
<span class="hljs-title function_">suite</span>(<span class="hljs-string">'suite of snapshot tests'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-title function_">test</span>(<span class="hljs-string">'snapshot test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">snapshot</span>({ <span class="hljs-attr">value1</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">value2</span>: <span class="hljs-number">2</span> });
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">snapshot</span>(<span class="hljs-number">5</span>);
});
});</code> <button class="copy-button">copy</button></pre>
<p>Generate the snapshot file by running the test file with
<code>--test-update-snapshots</code>. The test should pass, and a file named
<code>test.js.snapshot</code> is created in the same directory as the test file. The
contents of the snapshot file are shown below. Each snapshot is identified by
the full name of test and a counter to differentiate between snapshots in the
same test.</p>
<pre><code class="language-js"><span class="hljs-built_in">exports</span>[<span class="hljs-string">`suite of snapshot tests > snapshot test 1`</span>] = <span class="hljs-string">`
{
"value1": 1,
"value2": 2
}
`</span>;
<span class="hljs-built_in">exports</span>[<span class="hljs-string">`suite of snapshot tests > snapshot test 2`</span>] = <span class="hljs-string">`
5
`</span>;</code> <button class="copy-button">copy</button></pre>
<p>Once the snapshot file is created, run the tests again without the
<code>--test-update-snapshots</code> flag. The tests should pass now.</p>
</section><section><h3>Test reporters<span><a class="mark" href="#test-reporters" id="test-reporters">#</a></span><a aria-hidden="true" class="legacy" id="test_test_reporters"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.9.0, v18.17.0</td>
<td><p>Reporters are now exposed at <code>node:test/reporters</code>.</p></td></tr>
<tr><td>v19.6.0, v18.15.0</td>
<td><p><span>Added in: v19.6.0, v18.15.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>The <code>node:test</code> module supports passing <a href="cli.html#--test-reporter"><code>--test-reporter</code></a>
flags for the test runner to use a specific reporter.</p>
<p>The following built-reporters are supported:</p>
<ul>
<li>
<p><code>tap</code>
The <code>tap</code> reporter outputs the test results in the <a href="https://testanything.org/">TAP</a> format.</p>
</li>
<li>
<p><code>spec</code>
The <code>spec</code> reporter outputs the test results in a human-readable format.</p>
</li>
<li>
<p><code>dot</code>
The <code>dot</code> reporter outputs the test results in a compact format,
where each passing test is represented by a <code>.</code>,
and each failing test is represented by a <code>X</code>.</p>
</li>
<li>
<p><code>junit</code>
The junit reporter outputs test results in a jUnit XML format</p>
</li>
<li>
<p><code>lcov</code>
The <code>lcov</code> reporter outputs test coverage when used with the
<a href="cli.html#--experimental-test-coverage"><code>--experimental-test-coverage</code></a> flag.</p>
</li>
</ul>
<p>When <code>stdout</code> is a <a href="tty.html">TTY</a>, the <code>spec</code> reporter is used by default.
Otherwise, the <code>tap</code> reporter is used by default.</p>
<p>The exact output of these reporters is subject to change between versions of
Node.js, and should not be relied on programmatically. If programmatic access
to the test runner's output is required, use the events emitted by the
<a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p>
<p>The reporters are available via the <code>node:test/reporters</code> module:</p>
<pre class="with-71-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { tap, spec, dot, junit, lcov } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test/reporters'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { tap, spec, dot, junit, lcov } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test/reporters'</span>);</code><button class="copy-button">copy</button></pre>
<h4>Custom reporters<span><a class="mark" href="#custom-reporters" id="custom-reporters">#</a></span><a aria-hidden="true" class="legacy" id="test_custom_reporters"></a></h4>
<p><a href="cli.html#--test-reporter"><code>--test-reporter</code></a> can be used to specify a path to custom reporter.
A custom reporter is a module that exports a value
accepted by <a href="stream.html#streamcomposestreams">stream.compose</a>.
Reporters should transform events emitted by a <a href="test.html#class-testsstream" class="type"><TestsStream></a></p>
<p>Example of a custom reporter using <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a>:</p>
<pre class="with-45-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Transform</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>;
<span class="hljs-keyword">const</span> customReporter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Transform</span>({
<span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>,
<span class="hljs-title function_">transform</span>(<span class="hljs-params">event, encoding, callback</span>) {
<span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) {
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test watch queue drained'</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test plan'</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, event.<span class="hljs-property">data</span>.<span class="hljs-property">message</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: {
<span class="hljs-keyword">const</span> { totalLineCount } = event.<span class="hljs-property">data</span>.<span class="hljs-property">summary</span>.<span class="hljs-property">totals</span>;
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>);
<span class="hljs-keyword">break</span>;
}
}
},
});
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> customReporter;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>);
<span class="hljs-keyword">const</span> customReporter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Transform</span>({
<span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>,
<span class="hljs-title function_">transform</span>(<span class="hljs-params">event, encoding, callback</span>) {
<span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) {
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test watch queue drained'</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed`</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test plan'</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>:
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, event.<span class="hljs-property">data</span>.<span class="hljs-property">message</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: {
<span class="hljs-keyword">const</span> { totalLineCount } = event.<span class="hljs-property">data</span>.<span class="hljs-property">summary</span>.<span class="hljs-property">totals</span>;
<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>);
<span class="hljs-keyword">break</span>;
}
}
},
});
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = customReporter;</code><button class="copy-button">copy</button></pre>
<p>Example of a custom reporter using a generator function:</p>
<pre class="with-58-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-title function_">customReporter</span>(<span class="hljs-params">source</span>) {
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> source) {
<span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) {
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">'test watch queue drained\n'</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">'test plan\n'</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`<span class="hljs-subst">${event.data.message}</span>\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: {
<span class="hljs-keyword">const</span> { totalLineCount } = event.<span class="hljs-property">data</span>.<span class="hljs-property">summary</span>.<span class="hljs-property">totals</span>;
<span class="hljs-keyword">yield</span> <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>;
<span class="hljs-keyword">break</span>;
}
}
}
}</code><code class="language-js cjs"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-title function_">customReporter</span>(<span class="hljs-params">source</span>) {
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> source) {
<span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) {
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">'test watch queue drained\n'</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">'test plan\n'</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>:
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>:
<span class="hljs-keyword">yield</span> <span class="hljs-string">`<span class="hljs-subst">${event.data.message}</span>\n`</span>;
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: {
<span class="hljs-keyword">const</span> { totalLineCount } = event.<span class="hljs-property">data</span>.<span class="hljs-property">summary</span>.<span class="hljs-property">totals</span>;
<span class="hljs-keyword">yield</span> <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>;
<span class="hljs-keyword">break</span>;
}
}
}
};</code><button class="copy-button">copy</button></pre>
<p>The value provided to <code>--test-reporter</code> should be a string like one used in an
<code>import()</code> in JavaScript code, or a value provided for <a href="cli.html#--importmodule"><code>--import</code></a>.</p>
<h4>Multiple reporters<span><a class="mark" href="#multiple-reporters" id="multiple-reporters">#</a></span><a aria-hidden="true" class="legacy" id="test_multiple_reporters"></a></h4>
<p>The <a href="cli.html#--test-reporter"><code>--test-reporter</code></a> flag can be specified multiple times to report test
results in several formats. In this situation
it is required to specify a destination for each reporter
using <a href="cli.html#--test-reporter-destination"><code>--test-reporter-destination</code></a>.
Destination can be <code>stdout</code>, <code>stderr</code>, or a file path.
Reporters and destinations are paired according
to the order they were specified.</p>
<p>In the following example, the <code>spec</code> reporter will output to <code>stdout</code>,
and the <code>dot</code> reporter will output to <code>file.txt</code>:</p>
<pre><code class="language-bash">node --test-reporter=spec --test-reporter=dot --test-reporter-destination=stdout --test-reporter-destination=file.txt</code> <button class="copy-button">copy</button></pre>
<p>When a single reporter is specified, the destination will default to <code>stdout</code>,
unless a destination is explicitly provided.</p>
</section><section><h3><code>run([options])</code><span><a class="mark" href="#runoptions" id="runoptions">#</a></span><a aria-hidden="true" class="legacy" id="test_run_options"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.10.0</td>
<td><p>Added coverage options.</p></td></tr>
<tr><td>v22.8.0</td>
<td><p>Added the <code>isolation</code> option.</p></td></tr>
<tr><td>v22.6.0</td>
<td><p>Added the <code>globPatterns</code> option.</p></td></tr>
<tr><td>v22.0.0</td>
<td><p>Added the <code>forceExit</code> option.</p></td></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Add a testNamePatterns option.</p></td></tr>
<tr><td>v18.9.0, v16.19.0</td>
<td><p><span>Added in: v18.9.0, v16.19.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for running tests. The following
properties are supported:
<ul>
<li><code>concurrency</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If a number is provided,
then that many test processes would run in parallel, where each process
corresponds to one test file.
If <code>true</code>, it would run <code>os.availableParallelism() - 1</code> test files in
parallel.
If <code>false</code>, it would only run one test file at a time.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>files</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array containing the list of files to run.
<strong>Default:</strong> matching files from <a href="#test-runner-execution-model">test runner execution model</a>.</li>
<li><code>forceExit</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Configures the test runner to exit the process once
all known tests have finished executing even if the event loop would
otherwise remain active. <strong>Default:</strong> <code>false</code>.</li>
<li><code>globPatterns</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array containing the list of glob patterns to
match test files. This option cannot be used together with <code>files</code>.
<strong>Default:</strong> matching files from <a href="#test-runner-execution-model">test runner execution model</a>.</li>
<li><code>inspectPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Sets inspector port of test child process.
This can be a number, or a function that takes no arguments and returns a
number. If a nullish value is provided, each process gets its own port,
incremented from the primary's <code>process.debugPort</code>. This option is ignored
if the <code>isolation</code> option is set to <code>'none'</code> as no child processes are
spawned. <strong>Default:</strong> <code>undefined</code>.</li>
<li><code>isolation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Configures the type of test isolation. If set to
<code>'process'</code>, each test file is run in a separate child process. If set to
<code>'none'</code>, all test files run in the current process. <strong>Default:</strong>
<code>'process'</code>.</li>
<li><code>only</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If truthy, the test context will only run tests that
have the <code>only</code> option set</li>
<li><code>setup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A function that accepts the <code>TestsStream</code> instance
and can be used to setup listeners before any tests are run.
<strong>Default:</strong> <code>undefined</code>.</li>
<li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of CLI flags to pass to the <code>node</code> executable when
spawning the subprocesses. This option has no effect when <code>isolation</code> is <code>'none</code>'.
<strong>Default:</strong> <code>[]</code></li>
<li><code>argv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of CLI flags to pass to each test file when spawning the
subprocesses. This option has no effect when <code>isolation</code> is <code>'none'</code>.
<strong>Default:</strong> <code>[]</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test execution.</li>
<li><code>testNamePatterns</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> A String, RegExp or a RegExp Array,
that can be used to only run tests whose name matches the provided pattern.
Test name patterns are interpreted as JavaScript regular expressions.
For each test that is executed, any corresponding test hooks, such as
<code>beforeEach()</code>, are also run.
<strong>Default:</strong> <code>undefined</code>.</li>
<li><code>testSkipPatterns</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> A String, RegExp or a RegExp Array,
that can be used to exclude running tests whose name matches the provided pattern.
Test name patterns are interpreted as JavaScript regular expressions.
For each test that is executed, any corresponding test hooks, such as
<code>beforeEach()</code>, are also run.
<strong>Default:</strong> <code>undefined</code>.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the test execution will
fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
<li><code>watch</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether to run in watch mode or not. <strong>Default:</strong> <code>false</code>.</li>
<li><code>shard</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Running tests in a specific shard. <strong>Default:</strong> <code>undefined</code>.
<ul>
<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> is a positive integer between 1 and <code><total></code>
that specifies the index of the shard to run. This option is <em>required</em>.</li>
<li><code>total</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> is a positive integer that specifies the total number
of shards to split the test files to. This option is <em>required</em>.</li>
</ul>
</li>
<li><code>coverage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> enable <a href="#collecting-code-coverage">code coverage</a> collection.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>coverageExcludeGlobs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Excludes specific files from code coverage
using a glob pattern, which can match both absolute and relative file paths.
This property is only applicable when <code>coverage</code> was set to <code>true</code>.
If both <code>coverageExcludeGlobs</code> and <code>coverageIncludeGlobs</code> are provided,
files must meet <strong>both</strong> criteria to be included in the coverage report.
<strong>Default:</strong> <code>undefined</code>.</li>
<li><code>coverageIncludeGlobs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Includes specific files in code coverage
using a glob pattern, which can match both absolute and relative file paths.
This property is only applicable when <code>coverage</code> was set to <code>true</code>.
If both <code>coverageExcludeGlobs</code> and <code>coverageIncludeGlobs</code> are provided,
files must meet <strong>both</strong> criteria to be included in the coverage report.
<strong>Default:</strong> <code>undefined</code>.</li>
<li><code>lineCoverage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Require a minimum percent of covered lines. If code
coverage does not reach the threshold specified, the process will exit with code <code>1</code>.
<strong>Default:</strong> <code>0</code>.</li>
<li><code>branchCoverage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Require a minimum percent of covered branches. If code
coverage does not reach the threshold specified, the process will exit with code <code>1</code>.
<strong>Default:</strong> <code>0</code>.</li>
<li><code>functionCoverage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Require a minimum percent of covered functions. If code
coverage does not reach the threshold specified, the process will exit with code <code>1</code>.
<strong>Default:</strong> <code>0</code>.</li>
</ul>
</li>
<li>Returns: <a href="test.html#class-testsstream" class="type"><TestsStream></a></li>
</ul>
<p><strong>Note:</strong> <code>shard</code> is used to horizontally parallelize test running across
machines or processes, ideal for large-scale executions across varied
environments. It's incompatible with <code>watch</code> mode, tailored for rapid
code iteration by automatically rerunning tests on file changes.</p>
<pre class="with-47-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { tap } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test/reporters'</span>;
<span class="hljs-keyword">import</span> { run } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
<span class="hljs-keyword">import</span> path <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
<span class="hljs-title function_">run</span>({ <span class="hljs-attr">files</span>: [path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./tests/test.js'</span>)] })
.<span class="hljs-title function_">on</span>(<span class="hljs-string">'test:fail'</span>, <span class="hljs-function">() =></span> {
process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
})
.<span class="hljs-title function_">compose</span>(tap)
.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { tap } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test/reporters'</span>);
<span class="hljs-keyword">const</span> { run } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>);
<span class="hljs-title function_">run</span>({ <span class="hljs-attr">files</span>: [path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./tests/test.js'</span>)] })
.<span class="hljs-title function_">on</span>(<span class="hljs-string">'test:fail'</span>, <span class="hljs-function">() =></span> {
process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
})
.<span class="hljs-title function_">compose</span>(tap)
.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code><button class="copy-button">copy</button></pre>
</section><section><h3><code>suite([name][, options][, fn])</code><span><a class="mark" href="#suitename-options-fn" id="suitename-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_suite_name_options_fn"></a></h3>
<div class="api_metadata">
<span>Added in: v22.0.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the suite, which is displayed when reporting test
results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if <code>fn</code>
does not have a name.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options for the suite.
This supports the same options as <code>test([name][, options][, fn])</code>.</li>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The suite function declaring nested tests and
suites. The first argument to this function is a <a href="#class-suitecontext"><code>SuiteContext</code></a> object.
<strong>Default:</strong> A no-op function.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Immediately fulfilled with <code>undefined</code>.</li>
</ul>
<p>The <code>suite()</code> function is imported from the <code>node:test</code> module.</p>
</section><section><h3><code>suite.skip([name][, options][, fn])</code><span><a class="mark" href="#suiteskipname-options-fn" id="suiteskipname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_suite_skip_name_options_fn"></a></h3>
<div class="api_metadata">
<span>Added in: v22.0.0</span>
</div>
<p>Shorthand for skipping a suite. This is the same as
<a href="#suitename-options-fn"><code>suite([name], { skip: true }[, fn])</code></a>.</p>
</section><section><h3><code>suite.todo([name][, options][, fn])</code><span><a class="mark" href="#suitetodoname-options-fn" id="suitetodoname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_suite_todo_name_options_fn"></a></h3>
<div class="api_metadata">
<span>Added in: v22.0.0</span>
</div>
<p>Shorthand for marking a suite as <code>TODO</code>. This is the same as
<a href="#suitename-options-fn"><code>suite([name], { todo: true }[, fn])</code></a>.</p>
</section><section><h3><code>suite.only([name][, options][, fn])</code><span><a class="mark" href="#suiteonlyname-options-fn" id="suiteonlyname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_suite_only_name_options_fn"></a></h3>
<div class="api_metadata">
<span>Added in: v22.0.0</span>
</div>
<p>Shorthand for marking a suite as <code>only</code>. This is the same as
<a href="#suitename-options-fn"><code>suite([name], { only: true }[, fn])</code></a>.</p>
</section><section><h3><code>test([name][, options][, fn])</code><span><a class="mark" href="#testname-options-fn" id="testname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_test_name_options_fn"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.2.0, v18.17.0</td>
<td><p>Added the <code>skip</code>, <code>todo</code>, and <code>only</code> shorthands.</p></td></tr>
<tr><td>v18.8.0, v16.18.0</td>
<td><p>Add a <code>signal</code> option.</p></td></tr>
<tr><td>v18.7.0, v16.17.0</td>
<td><p>Add a <code>timeout</code> option.</p></td></tr>
<tr><td>v18.0.0, v16.17.0</td>
<td><p><span>Added in: v18.0.0, v16.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the test, which is displayed when reporting test
results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if <code>fn</code>
does not have a name.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the test. The following
properties are supported:
<ul>
<li><code>concurrency</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If a number is provided,
then that many tests would run in parallel within the application thread.
If <code>true</code>, all scheduled asynchronous tests run concurrently within the
thread. If <code>false</code>, only one test runs at a time.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If truthy, and the test context is configured to run
<code>only</code> tests, then this test will be run. Otherwise, the test is skipped.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test.</li>
<li><code>skip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If truthy, the test is skipped. If a string is
provided, that string is displayed in the test results as the reason for
skipping the test. <strong>Default:</strong> <code>false</code>.</li>
<li><code>todo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If truthy, the test marked as <code>TODO</code>. If a string
is provided, that string is displayed in the test results as the reason why
the test is <code>TODO</code>. <strong>Default:</strong> <code>false</code>.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the test will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
<li><code>plan</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of assertions and subtests expected to be run in the test.
If the number of assertions run in the test does not match the number
specified in the plan, the test will fail.
<strong>Default:</strong> <code>undefined</code>.</li>
</ul>
</li>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The function under test. The first argument
to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the test uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with <code>undefined</code> once
the test completes, or immediately if the test runs within a suite.</li>
</ul>
<p>The <code>test()</code> function is the value imported from the <code>test</code> module. Each
invocation of this function results in reporting the test to the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p>
<p>The <code>TestContext</code> object passed to the <code>fn</code> argument can be used to perform
actions related to the current test. Examples include skipping the test, adding
additional diagnostic information, or creating subtests.</p>
<p><code>test()</code> returns a <code>Promise</code> that fulfills once the test completes.
if <code>test()</code> is called within a suite, it fulfills immediately.
The return value can usually be discarded for top level tests.
However, the return value from subtests should be used to prevent the parent
test from finishing first and cancelling the subtest
as shown in the following example.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-comment">// The setTimeout() in the following subtest would cause it to outlive its</span>
<span class="hljs-comment">// parent test if 'await' is removed on the next line. Once the parent test</span>
<span class="hljs-comment">// completes, it will cancel any outstanding subtests.</span>
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'longer running subtest'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> {
<span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">1000</span>);
});
});
});</code> <button class="copy-button">copy</button></pre>
<p>The <code>timeout</code> option can be used to fail the test if it takes longer than
<code>timeout</code> milliseconds to complete. However, it is not a reliable mechanism for
canceling tests because a running test might block the application thread and
thus prevent the scheduled cancellation.</p>
</section><section><h3><code>test.skip([name][, options][, fn])</code><span><a class="mark" href="#testskipname-options-fn" id="testskipname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_test_skip_name_options_fn"></a></h3>
<p>Shorthand for skipping a test,
same as <a href="#testname-options-fn"><code>test([name], { skip: true }[, fn])</code></a>.</p>
</section><section><h3><code>test.todo([name][, options][, fn])</code><span><a class="mark" href="#testtodoname-options-fn" id="testtodoname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_test_todo_name_options_fn"></a></h3>
<p>Shorthand for marking a test as <code>TODO</code>,
same as <a href="#testname-options-fn"><code>test([name], { todo: true }[, fn])</code></a>.</p>
</section><section><h3><code>test.only([name][, options][, fn])</code><span><a class="mark" href="#testonlyname-options-fn" id="testonlyname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_test_only_name_options_fn"></a></h3>
<p>Shorthand for marking a test as <code>only</code>,
same as <a href="#testname-options-fn"><code>test([name], { only: true }[, fn])</code></a>.</p>
</section><section><h3><code>describe([name][, options][, fn])</code><span><a class="mark" href="#describename-options-fn" id="describename-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_describe_name_options_fn"></a></h3>
<p>Alias for <a href="#suitename-options-fn"><code>suite()</code></a>.</p>
<p>The <code>describe()</code> function is imported from the <code>node:test</code> module.</p>
</section><section><h3><code>describe.skip([name][, options][, fn])</code><span><a class="mark" href="#describeskipname-options-fn" id="describeskipname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_describe_skip_name_options_fn"></a></h3>
<p>Shorthand for skipping a suite. This is the same as
<a href="#describename-options-fn"><code>describe([name], { skip: true }[, fn])</code></a>.</p>
</section><section><h3><code>describe.todo([name][, options][, fn])</code><span><a class="mark" href="#describetodoname-options-fn" id="describetodoname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_describe_todo_name_options_fn"></a></h3>
<p>Shorthand for marking a suite as <code>TODO</code>. This is the same as
<a href="#describename-options-fn"><code>describe([name], { todo: true }[, fn])</code></a>.</p>
</section><section><h3><code>describe.only([name][, options][, fn])</code><span><a class="mark" href="#describeonlyname-options-fn" id="describeonlyname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_describe_only_name_options_fn"></a></h3>
<div class="api_metadata">
<span>Added in: v19.8.0, v18.15.0</span>
</div>
<p>Shorthand for marking a suite as <code>only</code>. This is the same as
<a href="#describename-options-fn"><code>describe([name], { only: true }[, fn])</code></a>.</p>
</section><section><h3><code>it([name][, options][, fn])</code><span><a class="mark" href="#itname-options-fn" id="itname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_it_name_options_fn"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.8.0, v18.16.0</td>
<td><p>Calling <code>it()</code> is now equivalent to calling <code>test()</code>.</p></td></tr>
<tr><td>v18.6.0, v16.17.0</td>
<td><p><span>Added in: v18.6.0, v16.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>Alias for <a href="#testname-options-fn"><code>test()</code></a>.</p>
<p>The <code>it()</code> function is imported from the <code>node:test</code> module.</p>
</section><section><h3><code>it.skip([name][, options][, fn])</code><span><a class="mark" href="#itskipname-options-fn" id="itskipname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_it_skip_name_options_fn"></a></h3>
<p>Shorthand for skipping a test,
same as <a href="#testname-options-fn"><code>it([name], { skip: true }[, fn])</code></a>.</p>
</section><section><h3><code>it.todo([name][, options][, fn])</code><span><a class="mark" href="#ittodoname-options-fn" id="ittodoname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_it_todo_name_options_fn"></a></h3>
<p>Shorthand for marking a test as <code>TODO</code>,
same as <a href="#testname-options-fn"><code>it([name], { todo: true }[, fn])</code></a>.</p>
</section><section><h3><code>it.only([name][, options][, fn])</code><span><a class="mark" href="#itonlyname-options-fn" id="itonlyname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_it_only_name_options_fn"></a></h3>
<div class="api_metadata">
<span>Added in: v19.8.0, v18.15.0</span>
</div>
<p>Shorthand for marking a test as <code>only</code>,
same as <a href="#testname-options-fn"><code>it([name], { only: true }[, fn])</code></a>.</p>
</section><section><h3><code>before([fn][, options])</code><span><a class="mark" href="#beforefn-options" id="beforefn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_before_fn_options"></a></h3>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function creates a hook that runs before executing a suite.</p>
<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-title function_">async</span> () => {
<span class="hljs-title function_">before</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'about to run some test'</span>));
<span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
});
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3><code>after([fn][, options])</code><span><a class="mark" href="#afterfn-options" id="afterfn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_after_fn_options"></a></h3>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function creates a hook that runs after executing a suite.</p>
<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-title function_">async</span> () => {
<span class="hljs-title function_">after</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'finished running tests'</span>));
<span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
});
});</code> <button class="copy-button">copy</button></pre>
<p><strong>Note:</strong> The <code>after</code> hook is guaranteed to run,
even if tests within the suite fail.</p>
</section><section><h3><code>beforeEach([fn][, options])</code><span><a class="mark" href="#beforeeachfn-options" id="beforeeachfn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_beforeeach_fn_options"></a></h3>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function creates a hook that runs before each test in the current suite.</p>
<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-title function_">async</span> () => {
<span class="hljs-title function_">beforeEach</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'about to run a test'</span>));
<span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
});
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3><code>afterEach([fn][, options])</code><span><a class="mark" href="#aftereachfn-options" id="aftereachfn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_aftereach_fn_options"></a></h3>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function creates a hook that runs after each test in the current suite.
The <code>afterEach()</code> hook is run even if the test fails.</p>
<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-title function_">async</span> () => {
<span class="hljs-title function_">afterEach</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'finished running a test'</span>));
<span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> {
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
});
});</code> <button class="copy-button">copy</button></pre>
</section><section><h3><code>assert</code><span><a class="mark" href="#assert" id="assert">#</a></span><a aria-hidden="true" class="legacy" id="test_assert"></a></h3>
<div class="api_metadata">
<span>Added in: v22.14.0</span>
</div>
<p>An object whose methods are used to configure available assertions on the
<code>TestContext</code> objects in the current process. The methods from <code>node:assert</code>
and snapshot testing functions are available by default.</p>
<p>It is possible to apply the same configuration to all files by placing common
configuration code in a module
preloaded with <code>--require</code> or <code>--import</code>.</p>
<h4><code>assert.register(name, fn)</code><span><a class="mark" href="#assertregistername-fn" id="assertregistername-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_assert_register_name_fn"></a></h4>
<div class="api_metadata">
<span>Added in: v22.14.0</span>
</div>
<p>Defines a new assertion function with the provided name and function. If an
assertion already exists with the same name, it is overwritten.</p>
</section><section><h3><code>snapshot</code><span><a class="mark" href="#snapshot" id="snapshot">#</a></span><a aria-hidden="true" class="legacy" id="test_snapshot"></a></h3>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<p>An object whose methods are used to configure default snapshot settings in the
current process. It is possible to apply the same configuration to all files by
placing common configuration code in a module preloaded with <code>--require</code> or
<code>--import</code>.</p>
<h4><code>snapshot.setDefaultSnapshotSerializers(serializers)</code><span><a class="mark" href="#snapshotsetdefaultsnapshotserializersserializers" id="snapshotsetdefaultsnapshotserializersserializers">#</a></span><a aria-hidden="true" class="legacy" id="test_snapshot_setdefaultsnapshotserializers_serializers"></a></h4>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<ul>
<li><code>serializers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of synchronous functions used as the default
serializers for snapshot tests.</li>
</ul>
<p>This function is used to customize the default serialization mechanism used by
the test runner. By default, the test runner performs serialization by calling
<code>JSON.stringify(value, null, 2)</code> on the provided value. <code>JSON.stringify()</code> does
have limitations regarding circular structures and supported data types. If a
more robust serialization mechanism is required, this function should be used.</p>
<h4><code>snapshot.setResolveSnapshotPath(fn)</code><span><a class="mark" href="#snapshotsetresolvesnapshotpathfn" id="snapshotsetresolvesnapshotpathfn">#</a></span><a aria-hidden="true" class="legacy" id="test_snapshot_setresolvesnapshotpath_fn"></a></h4>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A function used to compute the location of the snapshot file.
The function receives the path of the test file as its only argument. If the
test is not associated with a file (for example in the REPL), the input is
undefined. <code>fn()</code> must return a string specifying the location of the snapshot
snapshot file.</li>
</ul>
<p>This function is used to customize the location of the snapshot file used for
snapshot testing. By default, the snapshot filename is the same as the entry
point filename with a <code>.snapshot</code> file extension.</p>
</section><section><h3>Class: <code>MockFunctionContext</code><span><a class="mark" href="#class-mockfunctioncontext" id="class-mockfunctioncontext">#</a></span><a aria-hidden="true" class="legacy" id="test_class_mockfunctioncontext"></a></h3>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<p>The <code>MockFunctionContext</code> class is used to inspect or manipulate the behavior of
mocks created via the <a href="#class-mocktracker"><code>MockTracker</code></a> APIs.</p>
<h4><code>ctx.calls</code><span><a class="mark" href="#ctxcalls" id="ctxcalls">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_calls"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li>
</ul>
<p>A getter that returns a copy of the internal array used to track calls to the
mock. Each entry in the array is an object with the following properties.</p>
<ul>
<li><code>arguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of the arguments passed to the mock function.</li>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> If the mocked function threw then this property contains the
thrown value. <strong>Default:</strong> <code>undefined</code>.</li>
<li><code>result</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The value returned by the mocked function.</li>
<li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> An <code>Error</code> object whose stack can be used to determine the
callsite of the mocked function invocation.</li>
<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> If the mocked function is a constructor, this
field contains the class being constructed. Otherwise this will be
<code>undefined</code>.</li>
<li><code>this</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The mocked function's <code>this</code> value.</li>
</ul>
<h4><code>ctx.callCount()</code><span><a class="mark" href="#ctxcallcount" id="ctxcallcount">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_callcount"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of times that this mock has been invoked.</li>
</ul>
<p>This function returns the number of times that this mock has been invoked. This
function is more efficient than checking <code>ctx.calls.length</code> because <code>ctx.calls</code>
is a getter that creates a copy of the internal call tracking array.</p>
<h4><code>ctx.mockImplementation(implementation)</code><span><a class="mark" href="#ctxmockimplementationimplementation" id="ctxmockimplementationimplementation">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_mockimplementation_implementation"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<ul>
<li><code>implementation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The function to be used as the
mock's new implementation.</li>
</ul>
<p>This function is used to change the behavior of an existing mock.</p>
<p>The following example creates a mock function using <code>t.mock.fn()</code>, calls the
mock function, and then changes the mock implementation to a different function.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'changes a mock behavior'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) {
cnt++;
<span class="hljs-keyword">return</span> cnt;
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) {
cnt += <span class="hljs-number">2</span>;
<span class="hljs-keyword">return</span> cnt;
}
<span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">1</span>);
fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">mockImplementation</span>(addTwo);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">3</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">5</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>ctx.mockImplementationOnce(implementation[, onCall])</code><span><a class="mark" href="#ctxmockimplementationonceimplementation-oncall" id="ctxmockimplementationonceimplementation-oncall">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_mockimplementationonce_implementation_oncall"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<ul>
<li><code>implementation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The function to be used as the
mock's implementation for the invocation number specified by <code>onCall</code>.</li>
<li><code>onCall</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The invocation number that will use <code>implementation</code>. If
the specified invocation has already occurred then an exception is thrown.
<strong>Default:</strong> The number of the next invocation.</li>
</ul>
<p>This function is used to change the behavior of an existing mock for a single
invocation. Once invocation <code>onCall</code> has occurred, the mock will revert to
whatever behavior it would have used had <code>mockImplementationOnce()</code> not been
called.</p>
<p>The following example creates a mock function using <code>t.mock.fn()</code>, calls the
mock function, changes the mock implementation to a different function for the
next invocation, and then resumes its previous behavior.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'changes a mock behavior once'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) {
cnt++;
<span class="hljs-keyword">return</span> cnt;
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) {
cnt += <span class="hljs-number">2</span>;
<span class="hljs-keyword">return</span> cnt;
}
<span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">1</span>);
fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">mockImplementationOnce</span>(addTwo);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">3</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">4</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>ctx.resetCalls()</code><span><a class="mark" href="#ctxresetcalls" id="ctxresetcalls">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_resetcalls"></a></h4>
<div class="api_metadata">
<span>Added in: v19.3.0, v18.13.0</span>
</div>
<p>Resets the call history of the mock function.</p>
<h4><code>ctx.restore()</code><span><a class="mark" href="#ctxrestore" id="ctxrestore">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_restore"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<p>Resets the implementation of the mock function to its original behavior. The
mock can still be used after calling this function.</p>
</section><section><h3>Class: <code>MockModuleContext</code><span><a class="mark" href="#class-mockmodulecontext" id="class-mockmodulecontext">#</a></span><a aria-hidden="true" class="legacy" id="test_class_mockmodulecontext"></a></h3>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.0 - Early development</div><p></p>
<p>The <code>MockModuleContext</code> class is used to manipulate the behavior of module mocks
created via the <a href="#class-mocktracker"><code>MockTracker</code></a> APIs.</p>
<h4><code>ctx.restore()</code><span><a class="mark" href="#ctxrestore_1" id="ctxrestore_1">#</a></span><a aria-hidden="true" class="legacy" id="test_ctx_restore_1"></a></h4>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<p>Resets the implementation of the mock module.</p>
</section><section><h3>Class: <code>MockTracker</code><span><a class="mark" href="#class-mocktracker" id="class-mocktracker">#</a></span><a aria-hidden="true" class="legacy" id="test_class_mocktracker"></a></h3>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<p>The <code>MockTracker</code> class is used to manage mocking functionality. The test runner
module provides a top level <code>mock</code> export which is a <code>MockTracker</code> instance.
Each test also provides its own <code>MockTracker</code> instance via the test context's
<code>mock</code> property.</p>
<h4><code>mock.fn([original[, implementation]][, options])</code><span><a class="mark" href="#mockfnoriginal-implementation-options" id="mockfnoriginal-implementation-options">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_fn_original_implementation_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<ul>
<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> An optional function to create a mock on.
<strong>Default:</strong> A no-op function.</li>
<li><code>implementation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> An optional function used as the
mock implementation for <code>original</code>. This is useful for creating mocks that
exhibit one behavior for a specified number of calls and then restore the
behavior of <code>original</code>. <strong>Default:</strong> The function specified by <code>original</code>.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options for the mock function. The
following properties are supported:
<ul>
<li><code>times</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of times that the mock will use the behavior of
<code>implementation</code>. Once the mock function has been called <code>times</code> times, it
will automatically restore the behavior of <code>original</code>. This value must be an
integer greater than zero. <strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy" class="type"><Proxy></a> The mocked function. The mocked function contains a special
<code>mock</code> property, which is an instance of <a href="#class-mockfunctioncontext"><code>MockFunctionContext</code></a>, and can
be used for inspecting and changing the behavior of the mocked function.</li>
</ul>
<p>This function is used to create a mock function.</p>
<p>The following example creates a mock function that increments a counter by one
on each invocation. The <code>times</code> option is used to modify the mock behavior such
that the first two invocations add two to the counter instead of one.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks a counting function'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) {
cnt++;
<span class="hljs-keyword">return</span> cnt;
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) {
cnt += <span class="hljs-number">2</span>;
<span class="hljs-keyword">return</span> cnt;
}
<span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne, addTwo, { <span class="hljs-attr">times</span>: <span class="hljs-number">2</span> });
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">2</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">4</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">5</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">6</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>mock.getter(object, methodName[, implementation][, options])</code><span><a class="mark" href="#mockgetterobject-methodname-implementation-options" id="mockgetterobject-methodname-implementation-options">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_getter_object_methodname_implementation_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.3.0, v18.13.0</span>
</div>
<p>This function is syntax sugar for <a href="#mockmethodobject-methodname-implementation-options"><code>MockTracker.method</code></a> with <code>options.getter</code>
set to <code>true</code>.</p>
<h4><code>mock.method(object, methodName[, implementation][, options])</code><span><a class="mark" href="#mockmethodobject-methodname-implementation-options" id="mockmethodobject-methodname-implementation-options">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_method_object_methodname_implementation_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<ul>
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The object whose method is being mocked.</li>
<li><code>methodName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The identifier of the method on <code>object</code> to mock.
If <code>object[methodName]</code> is not a function, an error is thrown.</li>
<li><code>implementation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> An optional function used as the
mock implementation for <code>object[methodName]</code>. <strong>Default:</strong> The original method
specified by <code>object[methodName]</code>.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options for the mock method. The
following properties are supported:
<ul>
<li><code>getter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, <code>object[methodName]</code> is treated as a getter.
This option cannot be used with the <code>setter</code> option. <strong>Default:</strong> false.</li>
<li><code>setter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, <code>object[methodName]</code> is treated as a setter.
This option cannot be used with the <code>getter</code> option. <strong>Default:</strong> false.</li>
<li><code>times</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of times that the mock will use the behavior of
<code>implementation</code>. Once the mocked method has been called <code>times</code> times, it
will automatically restore the original behavior. This value must be an
integer greater than zero. <strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy" class="type"><Proxy></a> The mocked method. The mocked method contains a special
<code>mock</code> property, which is an instance of <a href="#class-mockfunctioncontext"><code>MockFunctionContext</code></a>, and can
be used for inspecting and changing the behavior of the mocked method.</li>
</ul>
<p>This function is used to create a mock on an existing object method. The
following example demonstrates how a mock is created on an existing object
method.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on an object method'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-keyword">const</span> number = {
<span class="hljs-attr">value</span>: <span class="hljs-number">5</span>,
<span class="hljs-title function_">subtract</span>(<span class="hljs-params">a</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> - a;
},
};
t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">method</span>(number, <span class="hljs-string">'subtract'</span>);
assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">subtract</span>.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-title function_">subtract</span>(<span class="hljs-number">3</span>), <span class="hljs-number">2</span>);
assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">subtract</span>.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-keyword">const</span> call = number.<span class="hljs-property">subtract</span>.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>[<span class="hljs-number">0</span>];
assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>]);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">2</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">target</span>, <span class="hljs-literal">undefined</span>);
assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">this</span>, number);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>mock.module(specifier[, options])</code><span><a class="mark" href="#mockmodulespecifier-options" id="mockmodulespecifier-options">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_module_specifier_options"></a></h4>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.0 - Early development</div><p></p>
<ul>
<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> A string identifying the module to mock.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options for the mock module. The
following properties are supported:
<ul>
<li><code>cache</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>false</code>, each call to <code>require()</code> or <code>import()</code>
generates a new mock module. If <code>true</code>, subsequent calls will return the same
module mock, and the mock module is inserted into the CommonJS cache.
<strong>Default:</strong> false.</li>
<li><code>defaultExport</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> An optional value used as the mocked module's default
export. If this value is not provided, ESM mocks do not include a default
export. If the mock is a CommonJS or builtin module, this setting is used as
the value of <code>module.exports</code>. If this value is not provided, CJS and builtin
mocks use an empty object as the value of <code>module.exports</code>.</li>
<li><code>namedExports</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An optional object whose keys and values are used to
create the named exports of the mock module. If the mock is a CommonJS or
builtin module, these values are copied onto <code>module.exports</code>. Therefore, if a
mock is created with both named exports and a non-object default export, the
mock will throw an exception when used as a CJS or builtin module.</li>
</ul>
</li>
<li>Returns: <a href="test.html#class-mockmodulecontext" class="type"><MockModuleContext></a> An object that can be used to manipulate the mock.</li>
</ul>
<p>This function is used to mock the exports of ECMAScript modules, CommonJS
modules, and Node.js builtin modules. Any references to the original module
prior to mocking are not impacted. In order to enable module mocking, Node.js must
be started with the <a href="cli.html#--experimental-test-module-mocks"><code>--experimental-test-module-mocks</code></a> command-line flag.</p>
<p>The following example demonstrates how a mock is created for a module.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks a builtin module in both module systems'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-comment">// Create a mock of 'node:readline' with a named export named 'fn', which</span>
<span class="hljs-comment">// does not exist in the original 'node:readline' module.</span>
<span class="hljs-keyword">const</span> mock = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">module</span>(<span class="hljs-string">'node:readline'</span>, {
<span class="hljs-attr">namedExports</span>: { <span class="hljs-title function_">fn</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> <span class="hljs-number">42</span>; } },
});
<span class="hljs-keyword">let</span> esmImpl = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:readline'</span>);
<span class="hljs-keyword">let</span> cjsImpl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>);
<span class="hljs-comment">// cursorTo() is an export of the original 'node:readline' module.</span>
assert.<span class="hljs-title function_">strictEqual</span>(esmImpl.<span class="hljs-property">cursorTo</span>, <span class="hljs-literal">undefined</span>);
assert.<span class="hljs-title function_">strictEqual</span>(cjsImpl.<span class="hljs-property">cursorTo</span>, <span class="hljs-literal">undefined</span>);
assert.<span class="hljs-title function_">strictEqual</span>(esmImpl.<span class="hljs-title function_">fn</span>(), <span class="hljs-number">42</span>);
assert.<span class="hljs-title function_">strictEqual</span>(cjsImpl.<span class="hljs-title function_">fn</span>(), <span class="hljs-number">42</span>);
mock.<span class="hljs-title function_">restore</span>();
<span class="hljs-comment">// The mock is restored, so the original builtin module is returned.</span>
esmImpl = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:readline'</span>);
cjsImpl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-keyword">typeof</span> esmImpl.<span class="hljs-property">cursorTo</span>, <span class="hljs-string">'function'</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-keyword">typeof</span> cjsImpl.<span class="hljs-property">cursorTo</span>, <span class="hljs-string">'function'</span>);
assert.<span class="hljs-title function_">strictEqual</span>(esmImpl.<span class="hljs-property">fn</span>, <span class="hljs-literal">undefined</span>);
assert.<span class="hljs-title function_">strictEqual</span>(cjsImpl.<span class="hljs-property">fn</span>, <span class="hljs-literal">undefined</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>mock.reset()</code><span><a class="mark" href="#mockreset" id="mockreset">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_reset"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<p>This function restores the default behavior of all mocks that were previously
created by this <code>MockTracker</code> and disassociates the mocks from the
<code>MockTracker</code> instance. Once disassociated, the mocks can still be used, but the
<code>MockTracker</code> instance can no longer be used to reset their behavior or
otherwise interact with them.</p>
<p>After each test completes, this function is called on the test context's
<code>MockTracker</code>. If the global <code>MockTracker</code> is used extensively, calling this
function manually is recommended.</p>
<h4><code>mock.restoreAll()</code><span><a class="mark" href="#mockrestoreall" id="mockrestoreall">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_restoreall"></a></h4>
<div class="api_metadata">
<span>Added in: v19.1.0, v18.13.0</span>
</div>
<p>This function restores the default behavior of all mocks that were previously
created by this <code>MockTracker</code>. Unlike <code>mock.reset()</code>, <code>mock.restoreAll()</code> does
not disassociate the mocks from the <code>MockTracker</code> instance.</p>
<h4><code>mock.setter(object, methodName[, implementation][, options])</code><span><a class="mark" href="#mocksetterobject-methodname-implementation-options" id="mocksetterobject-methodname-implementation-options">#</a></span><a aria-hidden="true" class="legacy" id="test_mock_setter_object_methodname_implementation_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.3.0, v18.13.0</span>
</div>
<p>This function is syntax sugar for <a href="#mockmethodobject-methodname-implementation-options"><code>MockTracker.method</code></a> with <code>options.setter</code>
set to <code>true</code>.</p>
</section><section><h3>Class: <code>MockTimers</code><span><a class="mark" href="#class-mocktimers" id="class-mocktimers">#</a></span><a aria-hidden="true" class="legacy" id="test_class_mocktimers"></a></h3>
<div class="api_metadata">
<span>Added in: v20.4.0, v18.19.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<p>Mocking timers is a technique commonly used in software testing to simulate and
control the behavior of timers, such as <code>setInterval</code> and <code>setTimeout</code>,
without actually waiting for the specified time intervals.</p>
<p>MockTimers is also able to mock the <code>Date</code> object.</p>
<p>The <a href="#class-mocktracker"><code>MockTracker</code></a> provides a top-level <code>timers</code> export
which is a <code>MockTimers</code> instance.</p>
<h4><code>timers.enable([enableOptions])</code><span><a class="mark" href="#timersenableenableoptions" id="timersenableenableoptions">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_enable_enableoptions"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.2.0, v20.11.0</td>
<td><p>Updated parameters to be an option object with available APIs and the default initial epoch.</p></td></tr>
<tr><td>v20.4.0, v18.19.0</td>
<td><p><span>Added in: v20.4.0, v18.19.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>Enables timer mocking for the specified timers.</p>
<ul>
<li><code>enableOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options for enabling timer
mocking. The following properties are supported:
<ul>
<li><code>apis</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An optional array containing the timers to mock.
The currently supported timer values are <code>'setInterval'</code>, <code>'setTimeout'</code>, <code>'setImmediate'</code>,
and <code>'Date'</code>. <strong>Default:</strong> <code>['setInterval', 'setTimeout', 'setImmediate', 'Date']</code>.
If no array is provided, all time related APIs (<code>'setInterval'</code>, <code>'clearInterval'</code>,
<code>'setTimeout'</code>, <code>'clearTimeout'</code>, <code>'setImmediate'</code>, <code>'clearImmediate'</code>, and
<code>'Date'</code>) will be mocked by default.</li>
<li><code>now</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a> An optional number or Date object representing the
initial time (in milliseconds) to use as the value
for <code>Date.now()</code>. <strong>Default:</strong> <code>0</code>.</li>
</ul>
</li>
</ul>
<p><strong>Note:</strong> When you enable mocking for a specific timer, its associated
clear function will also be implicitly mocked.</p>
<p><strong>Note:</strong> Mocking <code>Date</code> will affect the behavior of the mocked timers
as they use the same internal clock.</p>
<p>Example usage without setting initial time:</p>
<pre class="with-46-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mock } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setInterval'</span>] });</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { mock } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setInterval'</span>] });</code><button class="copy-button">copy</button></pre>
<p>The above example enables mocking for the <code>setInterval</code> timer and
implicitly mocks the <code>clearInterval</code> function. Only the <code>setInterval</code>
and <code>clearInterval</code> functions from <a href="./timers.html">node:timers</a>,
<a href="./timers.html#timers-promises-api">node:timers/promises</a>, and
<code>globalThis</code> will be mocked.</p>
<p>Example usage with initial time set</p>
<pre class="with-50-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mock } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-number">1000</span> });</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { mock } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-number">1000</span> });</code><button class="copy-button">copy</button></pre>
<p>Example usage with initial Date object as time set</p>
<pre class="with-56-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mock } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>() });</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { mock } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>], <span class="hljs-attr">now</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>() });</code><button class="copy-button">copy</button></pre>
<p>Alternatively, if you call <code>mock.timers.enable()</code> without any parameters:</p>
<p>All timers (<code>'setInterval'</code>, <code>'clearInterval'</code>, <code>'setTimeout'</code>, <code>'clearTimeout'</code>,
<code>'setImmediate'</code>, and <code>'clearImmediate'</code>) will be mocked. The <code>setInterval</code>,
<code>clearInterval</code>, <code>setTimeout</code>, <code>clearTimeout</code>, <code>setImmediate</code>, and
<code>clearImmediate</code> functions from <code>node:timers</code>, <code>node:timers/promises</code>, and
<code>globalThis</code> will be mocked. As well as the global <code>Date</code> object.</p>
<h4><code>timers.reset()</code><span><a class="mark" href="#timersreset" id="timersreset">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_reset"></a></h4>
<div class="api_metadata">
<span>Added in: v20.4.0, v18.19.0</span>
</div>
<p>This function restores the default behavior of all mocks that were previously
created by this <code>MockTimers</code> instance and disassociates the mocks
from the <code>MockTracker</code> instance.</p>
<p><strong>Note:</strong> After each test completes, this function is called on
the test context's <code>MockTracker</code>.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mock } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { mock } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>();</code><button class="copy-button">copy</button></pre>
<h4><code>timers[Symbol.dispose]()</code><span><a class="mark" href="#timerssymboldispose" id="timerssymboldispose">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_symbol_dispose"></a></h4>
<p>Calls <code>timers.reset()</code>.</p>
<h4><code>timers.tick([milliseconds])</code><span><a class="mark" href="#timerstickmilliseconds" id="timerstickmilliseconds">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_tick_milliseconds"></a></h4>
<div class="api_metadata">
<span>Added in: v20.4.0, v18.19.0</span>
</div>
<p>Advances time for all mocked timers.</p>
<ul>
<li><code>milliseconds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The amount of time, in milliseconds,
to advance the timers. <strong>Default:</strong> <code>1</code>.</li>
</ul>
<p><strong>Note:</strong> This diverges from how <code>setTimeout</code> in Node.js behaves and accepts
only positive numbers. In Node.js, <code>setTimeout</code> with negative numbers is
only supported for web compatibility reasons.</p>
<p>The following example mocks a <code>setTimeout</code> function and
by using <code>.tick</code> advances in
time triggering all pending timers.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><button class="copy-button">copy</button></pre>
<p>Alternatively, the <code>.tick</code> function can be called many times</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-keyword">const</span> nineSecs = <span class="hljs-number">9000</span>;
<span class="hljs-built_in">setTimeout</span>(fn, nineSecs);
<span class="hljs-keyword">const</span> threeSeconds = <span class="hljs-number">3000</span>;
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(threeSeconds);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(threeSeconds);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(threeSeconds);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-keyword">const</span> nineSecs = <span class="hljs-number">9000</span>;
<span class="hljs-built_in">setTimeout</span>(fn, nineSecs);
<span class="hljs-keyword">const</span> threeSeconds = <span class="hljs-number">3000</span>;
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(threeSeconds);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(threeSeconds);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(threeSeconds);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><button class="copy-button">copy</button></pre>
<p>Advancing time using <code>.tick</code> will also advance the time for any <code>Date</code> object
created after the mock was enabled (if <code>Date</code> was also set to be mocked).</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">9999</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">0</span>);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">9999</span>);
});</code><button class="copy-button">copy</button></pre>
<h5>Using clear functions<span><a class="mark" href="#using-clear-functions" id="using-clear-functions">#</a></span><a aria-hidden="true" class="legacy" id="test_using_clear_functions"></a></h5>
<p>As mentioned, all clear functions from timers (<code>clearTimeout</code>, <code>clearInterval</code>,and
<code>clearImmediate</code>) are implicitly mocked. Take a look at this example using <code>setTimeout</code>:</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-keyword">const</span> id = <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
<span class="hljs-comment">// Implicitly mocked as well</span>
<span class="hljs-built_in">clearTimeout</span>(id);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
<span class="hljs-comment">// As that setTimeout was cleared the mock function will never be called</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-keyword">const</span> id = <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>);
<span class="hljs-comment">// Implicitly mocked as well</span>
<span class="hljs-built_in">clearTimeout</span>(id);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
<span class="hljs-comment">// As that setTimeout was cleared the mock function will never be called</span>
assert.<span class="hljs-title function_">strictEqual</span>(fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">0</span>);
});</code><button class="copy-button">copy</button></pre>
<h5>Working with Node.js timers modules<span><a class="mark" href="#working-with-nodejs-timers-modules" id="working-with-nodejs-timers-modules">#</a></span><a aria-hidden="true" class="legacy" id="test_working_with_node_js_timers_modules"></a></h5>
<p>Once you enable mocking timers, <a href="./timers.html">node:timers</a>,
<a href="./timers.html#timers-promises-api">node:timers/promises</a> modules,
and timers from the Node.js global context are enabled:</p>
<p><strong>Note:</strong> Destructuring functions such as
<code>import { setTimeout } from 'node:timers'</code> is currently
not supported by this API.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-keyword">import</span> nodeTimers <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers'</span>;
<span class="hljs-keyword">import</span> nodeTimersPromises <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-title function_">async</span> (context) => {
<span class="hljs-keyword">const</span> globalTimeoutObjectSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-keyword">const</span> nodeTimerSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-keyword">const</span> nodeTimerPromiseSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(globalTimeoutObjectSpy, <span class="hljs-number">9999</span>);
nodeTimers.<span class="hljs-built_in">setTimeout</span>(nodeTimerSpy, <span class="hljs-number">9999</span>);
<span class="hljs-keyword">const</span> promise = nodeTimersPromises.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">9999</span>).<span class="hljs-title function_">then</span>(nodeTimerPromiseSpy);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(globalTimeoutObjectSpy.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
assert.<span class="hljs-title function_">strictEqual</span>(nodeTimerSpy.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-keyword">await</span> promise;
assert.<span class="hljs-title function_">strictEqual</span>(nodeTimerPromiseSpy.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-keyword">const</span> nodeTimers = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers'</span>);
<span class="hljs-keyword">const</span> nodeTimersPromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'mocks setTimeout to be executed synchronously without having to actually wait for it'</span>, <span class="hljs-title function_">async</span> (context) => {
<span class="hljs-keyword">const</span> globalTimeoutObjectSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-keyword">const</span> nodeTimerSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-keyword">const</span> nodeTimerPromiseSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>();
<span class="hljs-comment">// Optionally choose what to mock</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>] });
<span class="hljs-built_in">setTimeout</span>(globalTimeoutObjectSpy, <span class="hljs-number">9999</span>);
nodeTimers.<span class="hljs-built_in">setTimeout</span>(nodeTimerSpy, <span class="hljs-number">9999</span>);
<span class="hljs-keyword">const</span> promise = nodeTimersPromises.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">9999</span>).<span class="hljs-title function_">then</span>(nodeTimerPromiseSpy);
<span class="hljs-comment">// Advance in time</span>
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">strictEqual</span>(globalTimeoutObjectSpy.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
assert.<span class="hljs-title function_">strictEqual</span>(nodeTimerSpy.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
<span class="hljs-keyword">await</span> promise;
assert.<span class="hljs-title function_">strictEqual</span>(nodeTimerPromiseSpy.<span class="hljs-property">mock</span>.<span class="hljs-title function_">callCount</span>(), <span class="hljs-number">1</span>);
});</code><button class="copy-button">copy</button></pre>
<p>In Node.js, <code>setInterval</code> from <a href="./timers.html#timers-promises-api">node:timers/promises</a>
is an <code>AsyncGenerator</code> and is also supported by this API:</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-keyword">import</span> nodeTimersPromises <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'should tick five times testing a real use case'</span>, <span class="hljs-title function_">async</span> (context) => {
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setInterval'</span>] });
<span class="hljs-keyword">const</span> expectedIterations = <span class="hljs-number">3</span>;
<span class="hljs-keyword">const</span> interval = <span class="hljs-number">1000</span>;
<span class="hljs-keyword">const</span> startedAt = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">const</span> times = [];
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> time <span class="hljs-keyword">of</span> nodeTimersPromises.<span class="hljs-built_in">setInterval</span>(interval, startedAt)) {
times.<span class="hljs-title function_">push</span>(time);
<span class="hljs-keyword">if</span> (times.<span class="hljs-property">length</span> === expectedIterations) <span class="hljs-keyword">break</span>;
}
<span class="hljs-keyword">return</span> times;
}
<span class="hljs-keyword">const</span> r = <span class="hljs-title function_">run</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval);
<span class="hljs-keyword">const</span> timeResults = <span class="hljs-keyword">await</span> r;
assert.<span class="hljs-title function_">strictEqual</span>(timeResults.<span class="hljs-property">length</span>, expectedIterations);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> it = <span class="hljs-number">1</span>; it < expectedIterations; it++) {
assert.<span class="hljs-title function_">strictEqual</span>(timeResults[it - <span class="hljs-number">1</span>], startedAt + (interval * it));
}
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-keyword">const</span> nodeTimersPromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'should tick five times testing a real use case'</span>, <span class="hljs-title function_">async</span> (context) => {
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setInterval'</span>] });
<span class="hljs-keyword">const</span> expectedIterations = <span class="hljs-number">3</span>;
<span class="hljs-keyword">const</span> interval = <span class="hljs-number">1000</span>;
<span class="hljs-keyword">const</span> startedAt = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">const</span> times = [];
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> time <span class="hljs-keyword">of</span> nodeTimersPromises.<span class="hljs-built_in">setInterval</span>(interval, startedAt)) {
times.<span class="hljs-title function_">push</span>(time);
<span class="hljs-keyword">if</span> (times.<span class="hljs-property">length</span> === expectedIterations) <span class="hljs-keyword">break</span>;
}
<span class="hljs-keyword">return</span> times;
}
<span class="hljs-keyword">const</span> r = <span class="hljs-title function_">run</span>();
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval);
<span class="hljs-keyword">const</span> timeResults = <span class="hljs-keyword">await</span> r;
assert.<span class="hljs-title function_">strictEqual</span>(timeResults.<span class="hljs-property">length</span>, expectedIterations);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> it = <span class="hljs-number">1</span>; it < expectedIterations; it++) {
assert.<span class="hljs-title function_">strictEqual</span>(timeResults[it - <span class="hljs-number">1</span>], startedAt + (interval * it));
}
});</code><button class="copy-button">copy</button></pre>
<h4><code>timers.runAll()</code><span><a class="mark" href="#timersrunall" id="timersrunall">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_runall"></a></h4>
<div class="api_metadata">
<span>Added in: v20.4.0, v18.19.0</span>
</div>
<p>Triggers all pending mocked timers immediately. If the <code>Date</code> object is also
mocked, it will also advance the <code>Date</code> object to the furthest timer's time.</p>
<p>The example below triggers all pending timers immediately,
causing them to execute without any delay.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runAll functions following the given order'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> results = [];
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">1</span>), <span class="hljs-number">9999</span>);
<span class="hljs-comment">// Notice that if both timers have the same timeout,</span>
<span class="hljs-comment">// the order of execution is guaranteed</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">3</span>), <span class="hljs-number">8888</span>);
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">2</span>), <span class="hljs-number">8888</span>);
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>();
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, [<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]);
<span class="hljs-comment">// The Date object is also advanced to the furthest timer's time</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">9999</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runAll functions following the given order'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> results = [];
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">1</span>), <span class="hljs-number">9999</span>);
<span class="hljs-comment">// Notice that if both timers have the same timeout,</span>
<span class="hljs-comment">// the order of execution is guaranteed</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">3</span>), <span class="hljs-number">8888</span>);
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">2</span>), <span class="hljs-number">8888</span>);
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>();
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, [<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]);
<span class="hljs-comment">// The Date object is also advanced to the furthest timer's time</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">9999</span>);
});</code><button class="copy-button">copy</button></pre>
<p><strong>Note:</strong> The <code>runAll()</code> function is specifically designed for
triggering timers in the context of timer mocking.
It does not have any effect on real-time system
clocks or actual timers outside of the mocking environment.</p>
<h4><code>timers.setTime(milliseconds)</code><span><a class="mark" href="#timerssettimemilliseconds" id="timerssettimemilliseconds">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_settime_milliseconds"></a></h4>
<div class="api_metadata">
<span>Added in: v21.2.0, v20.11.0</span>
</div>
<p>Sets the current Unix timestamp that will be used as reference for any mocked
<code>Date</code> objects.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runAll functions following the given order'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
<span class="hljs-keyword">const</span> setTime = <span class="hljs-number">1000</span>;
<span class="hljs-comment">// Date.now is not mocked</span>
assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), now);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>] });
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(setTime);
<span class="hljs-comment">// Date.now is now 1000</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), setTime);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'setTime replaces current time'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
<span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
<span class="hljs-keyword">const</span> setTime = <span class="hljs-number">1000</span>;
<span class="hljs-comment">// Date.now is not mocked</span>
assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), now);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'Date'</span>] });
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(setTime);
<span class="hljs-comment">// Date.now is now 1000</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), setTime);
});</code><button class="copy-button">copy</button></pre>
<h5>Dates and Timers working together<span><a class="mark" href="#dates-and-timers-working-together" id="dates-and-timers-working-together">#</a></span><a aria-hidden="true" class="legacy" id="test_dates_and_timers_working_together"></a></h5>
<p>Dates and timer objects are dependent on each other. If you use <code>setTime()</code> to
pass the current time to the mocked <code>Date</code> object, the set timers with
<code>setTimeout</code> and <code>setInterval</code> will <strong>not</strong> be affected.</p>
<p>However, the <code>tick</code> method <strong>will</strong> advanced the mocked <code>Date</code> object.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>;
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runAll functions following the given order'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> results = [];
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">1</span>), <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">12000</span>);
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []);
<span class="hljs-comment">// The date is advanced but the timers don't tick</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">12000</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>);
<span class="hljs-title function_">test</span>(<span class="hljs-string">'runAll functions following the given order'</span>, <span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>({ <span class="hljs-attr">apis</span>: [<span class="hljs-string">'setTimeout'</span>, <span class="hljs-string">'Date'</span>] });
<span class="hljs-keyword">const</span> results = [];
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> results.<span class="hljs-title function_">push</span>(<span class="hljs-number">1</span>), <span class="hljs-number">9999</span>);
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []);
context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">setTime</span>(<span class="hljs-number">12000</span>);
assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []);
<span class="hljs-comment">// The date is advanced but the timers don't tick</span>
assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(), <span class="hljs-number">12000</span>);
});</code><button class="copy-button">copy</button></pre>
</section><section><h3>Class: <code>TestsStream</code><span><a class="mark" href="#class-testsstream" id="class-testsstream">#</a></span><a aria-hidden="true" class="legacy" id="test_class_testsstream"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.0.0, v19.9.0, v18.17.0</td>
<td><p>added type to test:pass and test:fail events for when the test is a suite.</p></td></tr>
<tr><td>v18.9.0, v16.19.0</td>
<td><p><span>Added in: v18.9.0, v16.19.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li>Extends <a href="stream.html#class-streamreadable" class="type"><Readable></a></li>
</ul>
<p>A successful call to <a href="#runoptions"><code>run()</code></a> method will return a new <a href="test.html#class-testsstream" class="type"><TestsStream></a>
object, streaming a series of events representing the execution of the tests.
<code>TestsStream</code> will emit events, in the order of the tests definition</p>
<p>Some of the events are guaranteed to be emitted in the same order as the tests
are defined, while others are emitted in the order that the tests execute.</p>
<h4>Event: <code>'test:coverage'</code><span><a class="mark" href="#event-testcoverage" id="event-testcoverage">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_coverage"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>summary</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object containing the coverage report.
<ul>
<li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of coverage reports for individual files. Each
report is an object with the following schema:
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute path of the file.</li>
<li><code>totalLineCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of lines.</li>
<li><code>totalBranchCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of branches.</li>
<li><code>totalFunctionCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of functions.</li>
<li><code>coveredLineCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of covered lines.</li>
<li><code>coveredBranchCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of covered branches.</li>
<li><code>coveredFunctionCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of covered functions.</li>
<li><code>coveredLinePercent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The percentage of lines covered.</li>
<li><code>coveredBranchPercent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The percentage of branches covered.</li>
<li><code>coveredFunctionPercent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The percentage of functions covered.</li>
<li><code>functions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of functions representing function
coverage.
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the function.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The line number where the function is defined.</li>
<li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of times the function was called.</li>
</ul>
</li>
<li><code>branches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of branches representing branch coverage.
<ul>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The line number where the branch is defined.</li>
<li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of times the branch was taken.</li>
</ul>
</li>
<li><code>lines</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of lines representing line
numbers and the number of times they were covered.
<ul>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The line number.</li>
<li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of times the line was covered.</li>
</ul>
</li>
</ul>
</li>
<li><code>thresholds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object containing whether or not the coverage for
each coverage type.
<ul>
<li><code>function</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The function coverage threshold.</li>
<li><code>branch</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The branch coverage threshold.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The line coverage threshold.</li>
</ul>
</li>
<li><code>totals</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object containing a summary of coverage for all
files.
<ul>
<li><code>totalLineCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of lines.</li>
<li><code>totalBranchCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of branches.</li>
<li><code>totalFunctionCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of functions.</li>
<li><code>coveredLineCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of covered lines.</li>
<li><code>coveredBranchCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of covered branches.</li>
<li><code>coveredFunctionCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of covered functions.</li>
<li><code>coveredLinePercent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The percentage of lines covered.</li>
<li><code>coveredBranchPercent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The percentage of branches covered.</li>
<li><code>coveredFunctionPercent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The percentage of functions covered.</li>
</ul>
</li>
<li><code>workingDirectory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The working directory when code coverage
began. This is useful for displaying relative path names in case the tests
changed the working directory of the Node.js process.</li>
</ul>
</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
</ul>
</li>
</ul>
<p>Emitted when code coverage is enabled and all tests have completed.</p>
<h4>Event: <code>'test:complete'</code><span><a class="mark" href="#event-testcomplete" id="event-testcomplete">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_complete"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>details</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Additional execution metadata.
<ul>
<li><code>passed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the test passed or not.</li>
<li><code>duration_ms</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration of the test in milliseconds.</li>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> An error wrapping the error thrown by the test
if it did not pass.
<ul>
<li><code>cause</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The actual error thrown by the test.</li>
</ul>
</li>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The type of the test, used to denote whether
this is a suite.</li>
</ul>
</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The test name.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
<li><code>testNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ordinal number of the test.</li>
<li><code>todo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present if <a href="#contexttodomessage"><code>context.todo</code></a> is called</li>
<li><code>skip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present if <a href="#contextskipmessage"><code>context.skip</code></a> is called</li>
</ul>
</li>
</ul>
<p>Emitted when a test completes its execution.
This event is not emitted in the same order as the tests are
defined.
The corresponding declaration ordered events are <code>'test:pass'</code> and <code>'test:fail'</code>.</p>
<h4>Event: <code>'test:dequeue'</code><span><a class="mark" href="#event-testdequeue" id="event-testdequeue">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_dequeue"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The test name.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
</ul>
</li>
</ul>
<p>Emitted when a test is dequeued, right before it is executed.
This event is not guaranteed to be emitted in the same order as the tests are
defined. The corresponding declaration ordered event is <code>'test:start'</code>.</p>
<h4>Event: <code>'test:diagnostic'</code><span><a class="mark" href="#event-testdiagnostic" id="event-testdiagnostic">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_diagnostic"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The diagnostic message.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
</ul>
</li>
</ul>
<p>Emitted when <a href="#contextdiagnosticmessage"><code>context.diagnostic</code></a> is called.
This event is guaranteed to be emitted in the same order as the tests are
defined.</p>
<h4>Event: <code>'test:enqueue'</code><span><a class="mark" href="#event-testenqueue" id="event-testenqueue">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_enqueue"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The test name.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
</ul>
</li>
</ul>
<p>Emitted when a test is enqueued for execution.</p>
<h4>Event: <code>'test:fail'</code><span><a class="mark" href="#event-testfail" id="event-testfail">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_fail"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>details</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Additional execution metadata.
<ul>
<li><code>duration_ms</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration of the test in milliseconds.</li>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> An error wrapping the error thrown by the test.
<ul>
<li><code>cause</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The actual error thrown by the test.</li>
</ul>
</li>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The type of the test, used to denote whether
this is a suite.</li>
</ul>
</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The test name.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
<li><code>testNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ordinal number of the test.</li>
<li><code>todo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present if <a href="#contexttodomessage"><code>context.todo</code></a> is called</li>
<li><code>skip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present if <a href="#contextskipmessage"><code>context.skip</code></a> is called</li>
</ul>
</li>
</ul>
<p>Emitted when a test fails.
This event is guaranteed to be emitted in the same order as the tests are
defined.
The corresponding execution ordered event is <code>'test:complete'</code>.</p>
<h4>Event: <code>'test:pass'</code><span><a class="mark" href="#event-testpass" id="event-testpass">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_pass"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>details</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Additional execution metadata.
<ul>
<li><code>duration_ms</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration of the test in milliseconds.</li>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The type of the test, used to denote whether
this is a suite.</li>
</ul>
</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The test name.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
<li><code>testNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ordinal number of the test.</li>
<li><code>todo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present if <a href="#contexttodomessage"><code>context.todo</code></a> is called</li>
<li><code>skip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present if <a href="#contextskipmessage"><code>context.skip</code></a> is called</li>
</ul>
</li>
</ul>
<p>Emitted when a test passes.
This event is guaranteed to be emitted in the same order as the tests are
defined.
The corresponding execution ordered event is <code>'test:complete'</code>.</p>
<h4>Event: <code>'test:plan'</code><span><a class="mark" href="#event-testplan" id="event-testplan">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_plan"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
<li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of subtests that have ran.</li>
</ul>
</li>
</ul>
<p>Emitted when all subtests have completed for a given test.
This event is guaranteed to be emitted in the same order as the tests are
defined.</p>
<h4>Event: <code>'test:start'</code><span><a class="mark" href="#event-teststart" id="event-teststart">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_start"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>column</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The column number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file,
<code>undefined</code> if test was run through the REPL.</li>
<li><code>line</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The line number where the test is defined, or
<code>undefined</code> if the test was run through the REPL.</li>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The test name.</li>
<li><code>nesting</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nesting level of the test.</li>
</ul>
</li>
</ul>
<p>Emitted when a test starts reporting its own and its subtests status.
This event is guaranteed to be emitted in the same order as the tests are
defined.
The corresponding execution ordered event is <code>'test:dequeue'</code>.</p>
<h4>Event: <code>'test:stderr'</code><span><a class="mark" href="#event-teststderr" id="event-teststderr">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_stderr"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The path of the test file.</li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The message written to <code>stderr</code>.</li>
</ul>
</li>
</ul>
<p>Emitted when a running test writes to <code>stderr</code>.
This event is only emitted if <code>--test</code> flag is passed.
This event is not guaranteed to be emitted in the same order as the tests are
defined.</p>
<h4>Event: <code>'test:stdout'</code><span><a class="mark" href="#event-teststdout" id="event-teststdout">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_stdout"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The path of the test file.</li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The message written to <code>stdout</code>.</li>
</ul>
</li>
</ul>
<p>Emitted when a running test writes to <code>stdout</code>.
This event is only emitted if <code>--test</code> flag is passed.
This event is not guaranteed to be emitted in the same order as the tests are
defined.</p>
<h4>Event: <code>'test:summary'</code><span><a class="mark" href="#event-testsummary" id="event-testsummary">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_summary"></a></h4>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>counts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object containing the counts of various test results.
<ul>
<li><code>cancelled</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of cancelled tests.</li>
<li><code>failed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of failed tests.</li>
<li><code>passed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of passed tests.</li>
<li><code>skipped</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of skipped tests.</li>
<li><code>suites</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of suites run.</li>
<li><code>tests</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of tests run, excluding suites.</li>
<li><code>todo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of TODO tests.</li>
<li><code>topLevel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of top level tests and suites.</li>
</ul>
</li>
<li><code>duration_ms</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration of the test run in milliseconds.</li>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> The path of the test file that generated the
summary. If the summary corresponds to multiple files, this value is
<code>undefined</code>.</li>
<li><code>success</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether or not the test run is considered
successful or not. If any error condition occurs, such as a failing test or
unmet coverage threshold, this value will be set to <code>false</code>.</li>
</ul>
</li>
</ul>
<p>Emitted when a test run completes. This event contains metrics pertaining to
the completed test run, and is useful for determining if a test run passed or
failed. If process-level test isolation is used, a <code>'test:summary'</code> event is
generated for each test file in addition to a final cumulative summary.</p>
<h4>Event: <code>'test:watch:drained'</code><span><a class="mark" href="#event-testwatchdrained" id="event-testwatchdrained">#</a></span><a aria-hidden="true" class="legacy" id="test_event_test_watch_drained"></a></h4>
<p>Emitted when no more tests are queued for execution in watch mode.</p>
</section><section><h3>Class: <code>TestContext</code><span><a class="mark" href="#class-testcontext" id="class-testcontext">#</a></span><a aria-hidden="true" class="legacy" id="test_class_testcontext"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>The <code>before</code> function was added to TestContext.</p></td></tr>
<tr><td>v18.0.0, v16.17.0</td>
<td><p><span>Added in: v18.0.0, v16.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>An instance of <code>TestContext</code> is passed to each test function in order to
interact with the test runner. However, the <code>TestContext</code> constructor is not
exposed as part of the API.</p>
<h4><code>context.before([fn][, options])</code><span><a class="mark" href="#contextbeforefn-options" id="contextbeforefn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_before_fn_options"></a></h4>
<div class="api_metadata">
<span>Added in: v20.1.0, v18.17.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function. The first argument
to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function is used to create a hook running before
subtest of the current test.</p>
<h4><code>context.beforeEach([fn][, options])</code><span><a class="mark" href="#contextbeforeeachfn-options" id="contextbeforeeachfn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_beforeeach_fn_options"></a></h4>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function. The first argument
to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function is used to create a hook running
before each subtest of the current test.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
t.<span class="hljs-title function_">beforeEach</span>(<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> t.<span class="hljs-title function_">diagnostic</span>(<span class="hljs-string">`about to run <span class="hljs-subst">${t.name}</span>`</span>));
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(
<span class="hljs-string">'This is a subtest'</span>,
<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
},
);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.after([fn][, options])</code><span><a class="mark" href="#contextafterfn-options" id="contextafterfn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_after_fn_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.3.0, v18.13.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function. The first argument
to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function is used to create a hook that runs after the current test
finishes.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
t.<span class="hljs-title function_">after</span>(<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> t.<span class="hljs-title function_">diagnostic</span>(<span class="hljs-string">`finished running <span class="hljs-subst">${t.name}</span>`</span>));
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.afterEach([fn][, options])</code><span><a class="mark" href="#contextaftereachfn-options" id="contextaftereachfn-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_aftereach_fn_options"></a></h4>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The hook function. The first argument
to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the hook. The following
properties are supported:
<ul>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
</ul>
</li>
</ul>
<p>This function is used to create a hook running
after each subtest of the current test.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
t.<span class="hljs-title function_">afterEach</span>(<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> t.<span class="hljs-title function_">diagnostic</span>(<span class="hljs-string">`finished running <span class="hljs-subst">${t.name}</span>`</span>));
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(
<span class="hljs-string">'This is a subtest'</span>,
<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
},
);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.assert</code><span><a class="mark" href="#contextassert" id="contextassert">#</a></span><a aria-hidden="true" class="legacy" id="test_context_assert"></a></h4>
<div class="api_metadata">
<span>Added in: v22.2.0</span>
</div>
<p>An object containing assertion methods bound to <code>context</code>. The top-level
functions from the <code>node:assert</code> module are exposed here for the purpose of
creating test plans.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-title function_">plan</span>(<span class="hljs-number">1</span>);
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-literal">true</span>, <span class="hljs-literal">true</span>);
});</code> <button class="copy-button">copy</button></pre>
<h5><code>context.assert.fileSnapshot(value, path[, options])</code><span><a class="mark" href="#contextassertfilesnapshotvalue-path-options" id="contextassertfilesnapshotvalue-path-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_assert_filesnapshot_value_path_options"></a></h5>
<div class="api_metadata">
<span>Added in: v22.14.0</span>
</div>
<ul>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> A value to serialize to a string. If Node.js was started with
the <a href="cli.html#--test-update-snapshots"><code>--test-update-snapshots</code></a> flag, the serialized value is written to
<code>path</code>. Otherwise, the serialized value is compared to the contents of the
existing snapshot file.</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The file where the serialized <code>value</code> is written.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options. The following properties
are supported:
<ul>
<li><code>serializers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of synchronous functions used to serialize
<code>value</code> into a string. <code>value</code> is passed as the only argument to the first
serializer function. The return value of each serializer is passed as input
to the next serializer. Once all serializers have run, the resulting value
is coerced to a string. <strong>Default:</strong> If no serializers are provided, the
test runner's default serializers are used.</li>
</ul>
</li>
</ul>
<p>This function serializes <code>value</code> and writes it to the file specified by <code>path</code>.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'snapshot test with default serialization'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">fileSnapshot</span>({ <span class="hljs-attr">value1</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">value2</span>: <span class="hljs-number">2</span> }, <span class="hljs-string">'./snapshots/snapshot.json'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>This function differs from <code>context.assert.snapshot()</code> in the following ways:</p>
<ul>
<li>The snapshot file path is explicitly provided by the user.</li>
<li>Each snapshot file is limited to a single snapshot value.</li>
<li>No additional escaping is performed by the test runner.</li>
</ul>
<p>These differences allow snapshot files to better support features such as syntax
highlighting.</p>
<h5><code>context.assert.snapshot(value[, options])</code><span><a class="mark" href="#contextassertsnapshotvalue-options" id="contextassertsnapshotvalue-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_assert_snapshot_value_options"></a></h5>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<ul>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> A value to serialize to a string. If Node.js was started with
the <a href="cli.html#--test-update-snapshots"><code>--test-update-snapshots</code></a> flag, the serialized value is written to
the snapshot file. Otherwise, the serialized value is compared to the
corresponding value in the existing snapshot file.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Optional configuration options. The following properties
are supported:
<ul>
<li><code>serializers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of synchronous functions used to serialize
<code>value</code> into a string. <code>value</code> is passed as the only argument to the first
serializer function. The return value of each serializer is passed as input
to the next serializer. Once all serializers have run, the resulting value
is coerced to a string. <strong>Default:</strong> If no serializers are provided, the
test runner's default serializers are used.</li>
</ul>
</li>
</ul>
<p>This function implements assertions for snapshot testing.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'snapshot test with default serialization'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">snapshot</span>({ <span class="hljs-attr">value1</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">value2</span>: <span class="hljs-number">2</span> });
});
<span class="hljs-title function_">test</span>(<span class="hljs-string">'snapshot test with custom serialization'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">snapshot</span>({ <span class="hljs-attr">value3</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">value4</span>: <span class="hljs-number">4</span> }, {
<span class="hljs-attr">serializers</span>: [<span class="hljs-function">(<span class="hljs-params">value</span>) =></span> <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(value)],
});
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.diagnostic(message)</code><span><a class="mark" href="#contextdiagnosticmessage" id="contextdiagnosticmessage">#</a></span><a aria-hidden="true" class="legacy" id="test_context_diagnostic_message"></a></h4>
<div class="api_metadata">
<span>Added in: v18.0.0, v16.17.0</span>
</div>
<ul>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Message to be reported.</li>
</ul>
<p>This function is used to write diagnostics to the output. Any diagnostic
information is included at the end of the test's results. This function does
not return a value.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-title function_">diagnostic</span>(<span class="hljs-string">'A diagnostic message'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.filePath</code><span><a class="mark" href="#contextfilepath" id="contextfilepath">#</a></span><a aria-hidden="true" class="legacy" id="test_context_filepath"></a></h4>
<div class="api_metadata">
<span>Added in: v22.6.0</span>
</div>
<p>The absolute path of the test file that created the current test. If a test file
imports additional modules that generate tests, the imported tests will return
the path of the root test file.</p>
<h4><code>context.fullName</code><span><a class="mark" href="#contextfullname" id="contextfullname">#</a></span><a aria-hidden="true" class="legacy" id="test_context_fullname"></a></h4>
<div class="api_metadata">
<span>Added in: v22.3.0</span>
</div>
<p>The name of the test and each of its ancestors, separated by <code>></code>.</p>
<h4><code>context.name</code><span><a class="mark" href="#contextname" id="contextname">#</a></span><a aria-hidden="true" class="legacy" id="test_context_name"></a></h4>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<p>The name of the test.</p>
<h4><code>context.plan(count)</code><span><a class="mark" href="#contextplancount" id="contextplancount">#</a></span><a aria-hidden="true" class="legacy" id="test_context_plan_count"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.13.0</td>
<td><p>This function is no longer experimental.</p></td></tr>
<tr><td>v22.2.0</td>
<td><p><span>Added in: v22.2.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of assertions and subtests that are expected to run.</li>
</ul>
<p>This function is used to set the number of assertions and subtests that are expected to run
within the test. If the number of assertions and subtests that run does not match the
expected count, the test will fail.</p>
<blockquote>
<p>Note: To make sure assertions are tracked, <code>t.assert</code> must be used instead of <code>assert</code> directly.</p>
</blockquote>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-title function_">plan</span>(<span class="hljs-number">2</span>);
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'subtest'</span>, <span class="hljs-function">() =></span> {});
});</code> <button class="copy-button">copy</button></pre>
<p>When working with asynchronous code, the <code>plan</code> function can be used to ensure that the
correct number of assertions are run:</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'planning with streams'</span>, <span class="hljs-function">(<span class="hljs-params">t, done</span>) =></span> {
<span class="hljs-keyword">function</span>* <span class="hljs-title function_">generate</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">yield</span> <span class="hljs-string">'a'</span>;
<span class="hljs-keyword">yield</span> <span class="hljs-string">'b'</span>;
<span class="hljs-keyword">yield</span> <span class="hljs-string">'c'</span>;
}
<span class="hljs-keyword">const</span> expected = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>];
t.<span class="hljs-title function_">plan</span>(expected.<span class="hljs-property">length</span>);
<span class="hljs-keyword">const</span> stream = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title function_">generate</span>());
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> {
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">strictEqual</span>(chunk, expected.<span class="hljs-title function_">shift</span>());
});
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-title function_">done</span>();
});
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.runOnly(shouldRunOnlyTests)</code><span><a class="mark" href="#contextrunonlyshouldrunonlytests" id="contextrunonlyshouldrunonlytests">#</a></span><a aria-hidden="true" class="legacy" id="test_context_runonly_shouldrunonlytests"></a></h4>
<div class="api_metadata">
<span>Added in: v18.0.0, v16.17.0</span>
</div>
<ul>
<li><code>shouldRunOnlyTests</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether or not to run <code>only</code> tests.</li>
</ul>
<p>If <code>shouldRunOnlyTests</code> is truthy, the test context will only run tests that
have the <code>only</code> option set. Otherwise, all tests are run. If Node.js was not
started with the <a href="cli.html#--test-only"><code>--test-only</code></a> command-line option, this function is a
no-op.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// The test context can be set to run subtests with the 'only' option.</span>
t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">true</span>);
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">all</span>([
t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now skipped'</span>),
t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is run'</span>, { <span class="hljs-attr">only</span>: <span class="hljs-literal">true</span> }),
]);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.signal</code><span><a class="mark" href="#contextsignal" id="contextsignal">#</a></span><a aria-hidden="true" class="legacy" id="test_context_signal"></a></h4>
<div class="api_metadata">
<span>Added in: v18.7.0, v16.17.0</span>
</div>
<ul>
<li>Type: <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li>
</ul>
<p>Can be used to abort test subtasks when the test has been aborted.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">fetch</span>(<span class="hljs-string">'some/uri'</span>, { <span class="hljs-attr">signal</span>: t.<span class="hljs-property">signal</span> });
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.skip([message])</code><span><a class="mark" href="#contextskipmessage" id="contextskipmessage">#</a></span><a aria-hidden="true" class="legacy" id="test_context_skip_message"></a></h4>
<div class="api_metadata">
<span>Added in: v18.0.0, v16.17.0</span>
</div>
<ul>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Optional skip message.</li>
</ul>
<p>This function causes the test's output to indicate the test as skipped. If
<code>message</code> is provided, it is included in the output. Calling <code>skip()</code> does
not terminate execution of the test function. This function does not return a
value.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span>
t.<span class="hljs-title function_">skip</span>(<span class="hljs-string">'this is skipped'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.todo([message])</code><span><a class="mark" href="#contexttodomessage" id="contexttodomessage">#</a></span><a aria-hidden="true" class="legacy" id="test_context_todo_message"></a></h4>
<div class="api_metadata">
<span>Added in: v18.0.0, v16.17.0</span>
</div>
<ul>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Optional <code>TODO</code> message.</li>
</ul>
<p>This function adds a <code>TODO</code> directive to the test's output. If <code>message</code> is
provided, it is included in the output. Calling <code>todo()</code> does not terminate
execution of the test function. This function does not return a value.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
<span class="hljs-comment">// This test is marked as `TODO`</span>
t.<span class="hljs-title function_">todo</span>(<span class="hljs-string">'this is a todo'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.test([name][, options][, fn])</code><span><a class="mark" href="#contexttestname-options-fn" id="contexttestname-options-fn">#</a></span><a aria-hidden="true" class="legacy" id="test_context_test_name_options_fn"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.8.0, v16.18.0</td>
<td><p>Add a <code>signal</code> option.</p></td></tr>
<tr><td>v18.7.0, v16.17.0</td>
<td><p>Add a <code>timeout</code> option.</p></td></tr>
<tr><td>v18.0.0, v16.17.0</td>
<td><p><span>Added in: v18.0.0, v16.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the subtest, which is displayed when reporting
test results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if
<code>fn</code> does not have a name.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Configuration options for the subtest. The following
properties are supported:
<ul>
<li><code>concurrency</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> If a number is provided,
then that many tests would run in parallel within the application thread.
If <code>true</code>, it would run all subtests in parallel.
If <code>false</code>, it would only run one test at a time.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>null</code>.</li>
<li><code>only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If truthy, and the test context is configured to run
<code>only</code> tests, then this test will be run. Otherwise, the test is skipped.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test.</li>
<li><code>skip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If truthy, the test is skipped. If a string is
provided, that string is displayed in the test results as the reason for
skipping the test. <strong>Default:</strong> <code>false</code>.</li>
<li><code>todo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If truthy, the test marked as <code>TODO</code>. If a string
is provided, that string is displayed in the test results as the reason why
the test is <code>TODO</code>. <strong>Default:</strong> <code>false</code>.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A number of milliseconds the test will fail after.
If unspecified, subtests inherit this value from their parent.
<strong>Default:</strong> <code>Infinity</code>.</li>
<li><code>plan</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of assertions and subtests expected to be run in the test.
If the number of assertions run in the test does not match the number
specified in the plan, the test will fail.
<strong>Default:</strong> <code>undefined</code>.</li>
</ul>
</li>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> The function under test. The first argument
to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the test uses callbacks,
the callback function is passed as the second argument. <strong>Default:</strong> A no-op
function.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with <code>undefined</code> once the test completes.</li>
</ul>
<p>This function is used to create subtests under the current test. This function
behaves in the same fashion as the top level <a href="#testname-options-fn"><code>test()</code></a> function.</p>
<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-title function_">async</span> (t) => {
<span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(
<span class="hljs-string">'This is a subtest'</span>,
{ <span class="hljs-attr">only</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">skip</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">concurrency</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">todo</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">plan</span>: <span class="hljs-number">1</span> },
<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> {
t.<span class="hljs-property">assert</span>.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>);
},
);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>context.waitFor(condition[, options])</code><span><a class="mark" href="#contextwaitforcondition-options" id="contextwaitforcondition-options">#</a></span><a aria-hidden="true" class="legacy" id="test_context_waitfor_condition_options"></a></h4>
<div class="api_metadata">
<span>Added in: v22.14.0</span>
</div>
<ul>
<li><code>condition</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://tc39.es/ecma262/#sec-async-function-constructor" class="type"><AsyncFunction></a> An assertion function that is invoked
periodically until it completes successfully or the defined polling timeout
elapses. Successful completion is defined as not throwing or rejecting. This
function does not accept any arguments, and is allowed to return any value.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An optional configuration object for the polling operation.
The following properties are supported:
<ul>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds to wait after an unsuccessful
invocation of <code>condition</code> before trying again. <strong>Default:</strong> <code>50</code>.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The poll timeout in milliseconds. If <code>condition</code> has not
succeeded by the time this elapses, an error occurs. <strong>Default:</strong> <code>1000</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with the value returned by <code>condition</code>.</li>
</ul>
<p>This method polls a <code>condition</code> function until that function either returns
successfully or the operation times out.</p>
</section><section><h3>Class: <code>SuiteContext</code><span><a class="mark" href="#class-suitecontext" id="class-suitecontext">#</a></span><a aria-hidden="true" class="legacy" id="test_class_suitecontext"></a></h3>
<div class="api_metadata">
<span>Added in: v18.7.0, v16.17.0</span>
</div>
<p>An instance of <code>SuiteContext</code> is passed to each suite function in order to
interact with the test runner. However, the <code>SuiteContext</code> constructor is not
exposed as part of the API.</p>
<h4><code>context.filePath</code><span><a class="mark" href="#contextfilepath_1" id="contextfilepath_1">#</a></span><a aria-hidden="true" class="legacy" id="test_context_filepath_1"></a></h4>
<div class="api_metadata">
<span>Added in: v22.6.0</span>
</div>
<p>The absolute path of the test file that created the current suite. If a test
file imports additional modules that generate suites, the imported suites will
return the path of the root test file.</p>
<h4><code>context.name</code><span><a class="mark" href="#contextname_1" id="contextname_1">#</a></span><a aria-hidden="true" class="legacy" id="test_context_name_1"></a></h4>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<p>The name of the suite.</p>
<h4><code>context.signal</code><span><a class="mark" href="#contextsignal_1" id="contextsignal_1">#</a></span><a aria-hidden="true" class="legacy" id="test_context_signal_1"></a></h4>
<div class="api_metadata">
<span>Added in: v18.7.0, v16.17.0</span>
</div>
<ul>
<li>Type: <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li>
</ul>
<p>Can be used to abort test subtasks when the test has been aborted.</p></section>
<!-- API END -->
</div>
</div>
</div>
</body>
</html>
|