1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.20.1"> 7 <title>Test runner | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/test.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:494px){.with-34-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:454px){.with-29-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:534px){.with-39-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:742px){.with-65-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:686px){.with-58-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-test"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test active">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="test" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <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"/> 104 <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"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <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"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_1"><a href="#test-runner">Test runner</a></span> 124<ul> 125<li><a href="#subtests">Subtests</a></li> 126<li><a href="#skipping-tests">Skipping tests</a></li> 127<li><a href="#describeit-syntax"><code>describe</code>/<code>it</code> syntax</a></li> 128<li><a href="#only-tests"><code>only</code> tests</a></li> 129<li><a href="#filtering-tests-by-name">Filtering tests by name</a></li> 130<li><a href="#extraneous-asynchronous-activity">Extraneous asynchronous activity</a></li> 131<li><span class="stability_1"><a href="#watch-mode">Watch mode</a></span></li> 132<li><a href="#running-tests-from-the-command-line">Running tests from the command line</a> 133<ul> 134<li><a href="#test-runner-execution-model">Test runner execution model</a></li> 135</ul> 136</li> 137<li><a href="#collecting-code-coverage">Collecting code coverage</a></li> 138<li><a href="#mocking">Mocking</a> 139<ul> 140<li><a href="#timers">Timers</a></li> 141</ul> 142</li> 143<li><a href="#test-reporters">Test reporters</a> 144<ul> 145<li><a href="#custom-reporters">Custom reporters</a></li> 146<li><a href="#multiple-reporters">Multiple reporters</a></li> 147</ul> 148</li> 149<li><a href="#runoptions"><code>run([options])</code></a></li> 150<li><a href="#testname-options-fn"><code>test([name][, options][, fn])</code></a></li> 151<li><a href="#testskipname-options-fn"><code>test.skip([name][, options][, fn])</code></a></li> 152<li><a href="#testtodoname-options-fn"><code>test.todo([name][, options][, fn])</code></a></li> 153<li><a href="#testonlyname-options-fn"><code>test.only([name][, options][, fn])</code></a></li> 154<li><a href="#describename-options-fn"><code>describe([name][, options][, fn])</code></a></li> 155<li><a href="#describeskipname-options-fn"><code>describe.skip([name][, options][, fn])</code></a></li> 156<li><a href="#describetodoname-options-fn"><code>describe.todo([name][, options][, fn])</code></a></li> 157<li><a href="#describeonlyname-options-fn"><code>describe.only([name][, options][, fn])</code></a></li> 158<li><a href="#itname-options-fn"><code>it([name][, options][, fn])</code></a></li> 159<li><a href="#itskipname-options-fn"><code>it.skip([name][, options][, fn])</code></a></li> 160<li><a href="#ittodoname-options-fn"><code>it.todo([name][, options][, fn])</code></a></li> 161<li><a href="#itonlyname-options-fn"><code>it.only([name][, options][, fn])</code></a></li> 162<li><a href="#beforefn-options"><code>before([fn][, options])</code></a></li> 163<li><a href="#afterfn-options"><code>after([fn][, options])</code></a></li> 164<li><a href="#beforeeachfn-options"><code>beforeEach([fn][, options])</code></a></li> 165<li><a href="#aftereachfn-options"><code>afterEach([fn][, options])</code></a></li> 166<li><a href="#class-mockfunctioncontext">Class: <code>MockFunctionContext</code></a> 167<ul> 168<li><a href="#ctxcalls"><code>ctx.calls</code></a></li> 169<li><a href="#ctxcallcount"><code>ctx.callCount()</code></a></li> 170<li><a href="#ctxmockimplementationimplementation"><code>ctx.mockImplementation(implementation)</code></a></li> 171<li><a href="#ctxmockimplementationonceimplementation-oncall"><code>ctx.mockImplementationOnce(implementation[, onCall])</code></a></li> 172<li><a href="#ctxresetcalls"><code>ctx.resetCalls()</code></a></li> 173<li><a href="#ctxrestore"><code>ctx.restore()</code></a></li> 174</ul> 175</li> 176<li><a href="#class-mocktracker">Class: <code>MockTracker</code></a> 177<ul> 178<li><a href="#mockfnoriginal-implementation-options"><code>mock.fn([original[, implementation]][, options])</code></a></li> 179<li><a href="#mockgetterobject-methodname-implementation-options"><code>mock.getter(object, methodName[, implementation][, options])</code></a></li> 180<li><a href="#mockmethodobject-methodname-implementation-options"><code>mock.method(object, methodName[, implementation][, options])</code></a></li> 181<li><a href="#mockreset"><code>mock.reset()</code></a></li> 182<li><a href="#mockrestoreall"><code>mock.restoreAll()</code></a></li> 183<li><a href="#mocksetterobject-methodname-implementation-options"><code>mock.setter(object, methodName[, implementation][, options])</code></a></li> 184</ul> 185</li> 186<li><span class="stability_1"><a href="#class-mocktimers">Class: <code>MockTimers</code></a></span> 187<ul> 188<li><a href="#timersenabletimers"><code>timers.enable([timers])</code></a></li> 189<li><a href="#timersreset"><code>timers.reset()</code></a></li> 190<li><a href="#timerssymboldispose"><code>timers[Symbol.dispose]()</code></a></li> 191<li><a href="#timerstickmilliseconds"><code>timers.tick(milliseconds)</code></a> 192<ul> 193<li><a href="#using-clear-functions">Using clear functions</a></li> 194<li><a href="#working-with-nodejs-timers-modules">Working with Node.js timers modules</a></li> 195</ul> 196</li> 197<li><a href="#timersrunall"><code>timers.runAll()</code></a></li> 198</ul> 199</li> 200<li><a href="#class-testsstream">Class: <code>TestsStream</code></a> 201<ul> 202<li><a href="#event-testcoverage">Event: <code>'test:coverage'</code></a></li> 203<li><a href="#event-testdequeue">Event: <code>'test:dequeue'</code></a></li> 204<li><a href="#event-testdiagnostic">Event: <code>'test:diagnostic'</code></a></li> 205<li><a href="#event-testenqueue">Event: <code>'test:enqueue'</code></a></li> 206<li><a href="#event-testfail">Event: <code>'test:fail'</code></a></li> 207<li><a href="#event-testpass">Event: <code>'test:pass'</code></a></li> 208<li><a href="#event-testplan">Event: <code>'test:plan'</code></a></li> 209<li><a href="#event-teststart">Event: <code>'test:start'</code></a></li> 210<li><a href="#event-teststderr">Event: <code>'test:stderr'</code></a></li> 211<li><a href="#event-teststdout">Event: <code>'test:stdout'</code></a></li> 212<li><a href="#event-testwatchdrained">Event: <code>'test:watch:drained'</code></a></li> 213</ul> 214</li> 215<li><a href="#class-testcontext">Class: <code>TestContext</code></a> 216<ul> 217<li><a href="#contextbeforefn-options"><code>context.before([fn][, options])</code></a></li> 218<li><a href="#contextbeforeeachfn-options"><code>context.beforeEach([fn][, options])</code></a></li> 219<li><a href="#contextafterfn-options"><code>context.after([fn][, options])</code></a></li> 220<li><a href="#contextaftereachfn-options"><code>context.afterEach([fn][, options])</code></a></li> 221<li><a href="#contextdiagnosticmessage"><code>context.diagnostic(message)</code></a></li> 222<li><a href="#contextname"><code>context.name</code></a></li> 223<li><a href="#contextrunonlyshouldrunonlytests"><code>context.runOnly(shouldRunOnlyTests)</code></a></li> 224<li><a href="#contextsignal"><code>context.signal</code></a></li> 225<li><a href="#contextskipmessage"><code>context.skip([message])</code></a></li> 226<li><a href="#contexttodomessage"><code>context.todo([message])</code></a></li> 227<li><a href="#contexttestname-options-fn"><code>context.test([name][, options][, fn])</code></a></li> 228</ul> 229</li> 230<li><a href="#class-suitecontext">Class: <code>SuiteContext</code></a> 231<ul> 232<li><a href="#contextname_1"><code>context.name</code></a></li> 233<li><a href="#contextsignal_1"><code>context.signal</code></a></li> 234</ul> 235</li> 236</ul> 237</li> 238</ul></div></div> 239 </li> 240 241 242 <li class="picker-header"> 243 <a href="#"> 244 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 245 Index 246 </a> 247 248 <div class="picker"><ul> 249<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 250<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 251 252 <li> 253 <a href="index.html">Index</a> 254 </li> 255 </ul> 256 257<hr class="line"> 258<ul> 259<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 260<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 261<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 262<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 263<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 264<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 265<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 266<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 267<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 268<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 269<li><a href="console.html" class="nav-console">Console</a></li> 270<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 271<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 272<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 273<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 274<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 275<li><a href="dns.html" class="nav-dns">DNS</a></li> 276<li><a href="domain.html" class="nav-domain">Domain</a></li> 277<li><a href="errors.html" class="nav-errors">Errors</a></li> 278<li><a href="events.html" class="nav-events">Events</a></li> 279<li><a href="fs.html" class="nav-fs">File system</a></li> 280<li><a href="globals.html" class="nav-globals">Globals</a></li> 281<li><a href="http.html" class="nav-http">HTTP</a></li> 282<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 283<li><a href="https.html" class="nav-https">HTTPS</a></li> 284<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 285<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 286<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 287<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 288<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 289<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 290<li><a href="net.html" class="nav-net">Net</a></li> 291<li><a href="os.html" class="nav-os">OS</a></li> 292<li><a href="path.html" class="nav-path">Path</a></li> 293<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 294<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 295<li><a href="process.html" class="nav-process">Process</a></li> 296<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 297<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 298<li><a href="readline.html" class="nav-readline">Readline</a></li> 299<li><a href="repl.html" class="nav-repl">REPL</a></li> 300<li><a href="report.html" class="nav-report">Report</a></li> 301<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 302<li><a href="stream.html" class="nav-stream">Stream</a></li> 303<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 304<li><a href="test.html" class="nav-test active">Test runner</a></li> 305<li><a href="timers.html" class="nav-timers">Timers</a></li> 306<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 307<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 308<li><a href="tty.html" class="nav-tty">TTY</a></li> 309<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 310<li><a href="url.html" class="nav-url">URL</a></li> 311<li><a href="util.html" class="nav-util">Utilities</a></li> 312<li><a href="v8.html" class="nav-v8">V8</a></li> 313<li><a href="vm.html" class="nav-vm">VM</a></li> 314<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 315<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 316<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 317<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 318<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 319</ul> 320<hr class="line"> 321<ul> 322<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 323</ul></div> 324 </li> 325 326 327 <li class="picker-header"> 328 <a href="#"> 329 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 330 Other versions 331 </a> 332 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/test.html">21.x</a></li> 333<li><a href="https://nodejs.org/docs/latest-v20.x/api/test.html">20.x <b>LTS</b></a></li> 334<li><a href="https://nodejs.org/docs/latest-v19.x/api/test.html">19.x</a></li> 335<li><a href="https://nodejs.org/docs/latest-v18.x/api/test.html">18.x <b>LTS</b></a></li></ol></div> 336 </li> 337 338 <li class="picker-header"> 339 <a href="#"> 340 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 341 Options 342 </a> 343 344 <div class="picker"> 345 <ul> 346 <li> 347 <a href="all.html">View on single page</a> 348 </li> 349 <li> 350 <a href="test.json">View as JSON</a> 351 </li> 352 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/test.md">Edit on GitHub</a></li> 353 </ul> 354 </div> 355 </li> 356 </ul> 357 </div> 358 <hr> 359 </header> 360 361 <details id="toc" open><summary>Table of contents</summary><ul> 362<li><span class="stability_1"><a href="#test-runner">Test runner</a></span> 363<ul> 364<li><a href="#subtests">Subtests</a></li> 365<li><a href="#skipping-tests">Skipping tests</a></li> 366<li><a href="#describeit-syntax"><code>describe</code>/<code>it</code> syntax</a></li> 367<li><a href="#only-tests"><code>only</code> tests</a></li> 368<li><a href="#filtering-tests-by-name">Filtering tests by name</a></li> 369<li><a href="#extraneous-asynchronous-activity">Extraneous asynchronous activity</a></li> 370<li><span class="stability_1"><a href="#watch-mode">Watch mode</a></span></li> 371<li><a href="#running-tests-from-the-command-line">Running tests from the command line</a> 372<ul> 373<li><a href="#test-runner-execution-model">Test runner execution model</a></li> 374</ul> 375</li> 376<li><a href="#collecting-code-coverage">Collecting code coverage</a></li> 377<li><a href="#mocking">Mocking</a> 378<ul> 379<li><a href="#timers">Timers</a></li> 380</ul> 381</li> 382<li><a href="#test-reporters">Test reporters</a> 383<ul> 384<li><a href="#custom-reporters">Custom reporters</a></li> 385<li><a href="#multiple-reporters">Multiple reporters</a></li> 386</ul> 387</li> 388<li><a href="#runoptions"><code>run([options])</code></a></li> 389<li><a href="#testname-options-fn"><code>test([name][, options][, fn])</code></a></li> 390<li><a href="#testskipname-options-fn"><code>test.skip([name][, options][, fn])</code></a></li> 391<li><a href="#testtodoname-options-fn"><code>test.todo([name][, options][, fn])</code></a></li> 392<li><a href="#testonlyname-options-fn"><code>test.only([name][, options][, fn])</code></a></li> 393<li><a href="#describename-options-fn"><code>describe([name][, options][, fn])</code></a></li> 394<li><a href="#describeskipname-options-fn"><code>describe.skip([name][, options][, fn])</code></a></li> 395<li><a href="#describetodoname-options-fn"><code>describe.todo([name][, options][, fn])</code></a></li> 396<li><a href="#describeonlyname-options-fn"><code>describe.only([name][, options][, fn])</code></a></li> 397<li><a href="#itname-options-fn"><code>it([name][, options][, fn])</code></a></li> 398<li><a href="#itskipname-options-fn"><code>it.skip([name][, options][, fn])</code></a></li> 399<li><a href="#ittodoname-options-fn"><code>it.todo([name][, options][, fn])</code></a></li> 400<li><a href="#itonlyname-options-fn"><code>it.only([name][, options][, fn])</code></a></li> 401<li><a href="#beforefn-options"><code>before([fn][, options])</code></a></li> 402<li><a href="#afterfn-options"><code>after([fn][, options])</code></a></li> 403<li><a href="#beforeeachfn-options"><code>beforeEach([fn][, options])</code></a></li> 404<li><a href="#aftereachfn-options"><code>afterEach([fn][, options])</code></a></li> 405<li><a href="#class-mockfunctioncontext">Class: <code>MockFunctionContext</code></a> 406<ul> 407<li><a href="#ctxcalls"><code>ctx.calls</code></a></li> 408<li><a href="#ctxcallcount"><code>ctx.callCount()</code></a></li> 409<li><a href="#ctxmockimplementationimplementation"><code>ctx.mockImplementation(implementation)</code></a></li> 410<li><a href="#ctxmockimplementationonceimplementation-oncall"><code>ctx.mockImplementationOnce(implementation[, onCall])</code></a></li> 411<li><a href="#ctxresetcalls"><code>ctx.resetCalls()</code></a></li> 412<li><a href="#ctxrestore"><code>ctx.restore()</code></a></li> 413</ul> 414</li> 415<li><a href="#class-mocktracker">Class: <code>MockTracker</code></a> 416<ul> 417<li><a href="#mockfnoriginal-implementation-options"><code>mock.fn([original[, implementation]][, options])</code></a></li> 418<li><a href="#mockgetterobject-methodname-implementation-options"><code>mock.getter(object, methodName[, implementation][, options])</code></a></li> 419<li><a href="#mockmethodobject-methodname-implementation-options"><code>mock.method(object, methodName[, implementation][, options])</code></a></li> 420<li><a href="#mockreset"><code>mock.reset()</code></a></li> 421<li><a href="#mockrestoreall"><code>mock.restoreAll()</code></a></li> 422<li><a href="#mocksetterobject-methodname-implementation-options"><code>mock.setter(object, methodName[, implementation][, options])</code></a></li> 423</ul> 424</li> 425<li><span class="stability_1"><a href="#class-mocktimers">Class: <code>MockTimers</code></a></span> 426<ul> 427<li><a href="#timersenabletimers"><code>timers.enable([timers])</code></a></li> 428<li><a href="#timersreset"><code>timers.reset()</code></a></li> 429<li><a href="#timerssymboldispose"><code>timers[Symbol.dispose]()</code></a></li> 430<li><a href="#timerstickmilliseconds"><code>timers.tick(milliseconds)</code></a> 431<ul> 432<li><a href="#using-clear-functions">Using clear functions</a></li> 433<li><a href="#working-with-nodejs-timers-modules">Working with Node.js timers modules</a></li> 434</ul> 435</li> 436<li><a href="#timersrunall"><code>timers.runAll()</code></a></li> 437</ul> 438</li> 439<li><a href="#class-testsstream">Class: <code>TestsStream</code></a> 440<ul> 441<li><a href="#event-testcoverage">Event: <code>'test:coverage'</code></a></li> 442<li><a href="#event-testdequeue">Event: <code>'test:dequeue'</code></a></li> 443<li><a href="#event-testdiagnostic">Event: <code>'test:diagnostic'</code></a></li> 444<li><a href="#event-testenqueue">Event: <code>'test:enqueue'</code></a></li> 445<li><a href="#event-testfail">Event: <code>'test:fail'</code></a></li> 446<li><a href="#event-testpass">Event: <code>'test:pass'</code></a></li> 447<li><a href="#event-testplan">Event: <code>'test:plan'</code></a></li> 448<li><a href="#event-teststart">Event: <code>'test:start'</code></a></li> 449<li><a href="#event-teststderr">Event: <code>'test:stderr'</code></a></li> 450<li><a href="#event-teststdout">Event: <code>'test:stdout'</code></a></li> 451<li><a href="#event-testwatchdrained">Event: <code>'test:watch:drained'</code></a></li> 452</ul> 453</li> 454<li><a href="#class-testcontext">Class: <code>TestContext</code></a> 455<ul> 456<li><a href="#contextbeforefn-options"><code>context.before([fn][, options])</code></a></li> 457<li><a href="#contextbeforeeachfn-options"><code>context.beforeEach([fn][, options])</code></a></li> 458<li><a href="#contextafterfn-options"><code>context.after([fn][, options])</code></a></li> 459<li><a href="#contextaftereachfn-options"><code>context.afterEach([fn][, options])</code></a></li> 460<li><a href="#contextdiagnosticmessage"><code>context.diagnostic(message)</code></a></li> 461<li><a href="#contextname"><code>context.name</code></a></li> 462<li><a href="#contextrunonlyshouldrunonlytests"><code>context.runOnly(shouldRunOnlyTests)</code></a></li> 463<li><a href="#contextsignal"><code>context.signal</code></a></li> 464<li><a href="#contextskipmessage"><code>context.skip([message])</code></a></li> 465<li><a href="#contexttodomessage"><code>context.todo([message])</code></a></li> 466<li><a href="#contexttestname-options-fn"><code>context.test([name][, options][, fn])</code></a></li> 467</ul> 468</li> 469<li><a href="#class-suitecontext">Class: <code>SuiteContext</code></a> 470<ul> 471<li><a href="#contextname_1"><code>context.name</code></a></li> 472<li><a href="#contextsignal_1"><code>context.signal</code></a></li> 473</ul> 474</li> 475</ul> 476</li> 477</ul></details> 478 479 <div id="apicontent"> 480 <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> 481 482<div class="api_metadata"> 483<span>Added in: v18.0.0, v16.17.0</span> 484</div> 485<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 486<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/test.js">lib/test.js</a></p> 487<p>The <code>node:test</code> module facilitates the creation of JavaScript tests. 488To access it:</p> 489 490<pre class="with-34-chars"><input class="js-flavor-selector" 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> 491<p>This module is only available under the <code>node:</code> scheme. The following will not 492work:</p> 493 494<pre class="with-29-chars"><input class="js-flavor-selector" 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">'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">'test'</span>);</code><button class="copy-button">copy</button></pre> 495<p>Tests created via the <code>test</code> module consist of a single function that is 496processed in one of three ways:</p> 497<ol> 498<li>A synchronous function that is considered failing if it throws an exception, 499and is considered passing otherwise.</li> 500<li>A function that returns a <code>Promise</code> that is considered failing if the 501<code>Promise</code> rejects, and is considered passing if the <code>Promise</code> resolves.</li> 502<li>A function that receives a callback function. If the callback receives any 503truthy value as its first argument, the test is considered failing. If a 504falsy value is passed as the first argument to the callback, the test is 505considered passing. If the test function receives a callback function and 506also returns a <code>Promise</code>, the test will fail.</li> 507</ol> 508<p>The following example illustrates how tests are written using the 509<code>test</code> module.</p> 510<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> { 511 <span class="hljs-comment">// This test passes because it does not throw an exception.</span> 512 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 513}); 514 515<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> { 516 <span class="hljs-comment">// This test fails because it throws an exception.</span> 517 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 518}); 519 520<span class="hljs-title function_">test</span>(<span class="hljs-string">'asynchronous passing test'</span>, <span class="hljs-keyword">async</span> (t) => { 521 <span class="hljs-comment">// This test passes because the Promise returned by the async</span> 522 <span class="hljs-comment">// function is not rejected.</span> 523 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 524}); 525 526<span class="hljs-title function_">test</span>(<span class="hljs-string">'asynchronous failing test'</span>, <span class="hljs-keyword">async</span> (t) => { 527 <span class="hljs-comment">// This test fails because the Promise returned by the async</span> 528 <span class="hljs-comment">// function is rejected.</span> 529 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 530}); 531 532<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> { 533 <span class="hljs-comment">// Promises can be used directly as well.</span> 534 <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> { 535 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 536 <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>)); 537 }); 538 }); 539}); 540 541<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> { 542 <span class="hljs-comment">// done() is the callback function. When the setImmediate() runs, it invokes</span> 543 <span class="hljs-comment">// done() with no arguments.</span> 544 <span class="hljs-title function_">setImmediate</span>(done); 545}); 546 547<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> { 548 <span class="hljs-comment">// When the setImmediate() runs, done() is invoked with an Error object and</span> 549 <span class="hljs-comment">// the test fails.</span> 550 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 551 <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>)); 552 }); 553});</code> <button class="copy-button">copy</button></pre> 554<p>If any tests fail, the process exit code is set to <code>1</code>.</p> 555<section><h3>Subtests<span><a class="mark" href="#subtests" id="subtests">#</a></span><a aria-hidden="true" class="legacy" id="test_subtests"></a></h3> 556<p>The test context's <code>test()</code> method allows subtests to be created. This method 557behaves identically to the top level <code>test()</code> function. The following example 558demonstrates the creation of a top level test with two subtests.</p> 559<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 560 <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> { 561 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 562 }); 563 564 <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> { 565 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>); 566 }); 567});</code> <button class="copy-button">copy</button></pre> 568<p>In this example, <code>await</code> is used to ensure that both subtests have completed. 569This is necessary because parent tests do not wait for their subtests to 570complete. Any subtests that are still outstanding when their parent finishes 571are cancelled and treated as failures. Any subtest failures cause the parent 572test to fail.</p> 573</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> 574<p>Individual tests can be skipped by passing the <code>skip</code> option to the test, or by 575calling the test context's <code>skip()</code> method as shown in the 576following example.</p> 577<pre><code class="language-js"><span class="hljs-comment">// The skip option is used, but no message is provided.</span> 578<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> { 579 <span class="hljs-comment">// This code is never executed.</span> 580}); 581 582<span class="hljs-comment">// The skip option is used, and a message is provided.</span> 583<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> { 584 <span class="hljs-comment">// This code is never executed.</span> 585}); 586 587<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> { 588 <span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span> 589 t.<span class="hljs-title function_">skip</span>(); 590}); 591 592<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> { 593 <span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span> 594 t.<span class="hljs-title function_">skip</span>(<span class="hljs-string">'this is skipped'</span>); 595});</code> <button class="copy-button">copy</button></pre> 596</section><section><h3><code>describe</code>/<code>it</code> syntax<span><a class="mark" href="#describeit-syntax" id="describeit-syntax">#</a></span><a aria-hidden="true" class="legacy" id="test_describe_it_syntax"></a></h3> 597<p>Running tests can also be done using <code>describe</code> to declare a suite 598and <code>it</code> to declare a test. 599A suite is used to organize and group related tests together. 600<code>it</code> is a shorthand for <a href="#testname-options-fn"><code>test()</code></a>.</p> 601<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'A thing'</span>, <span class="hljs-function">() =></span> { 602 <span class="hljs-title function_">it</span>(<span class="hljs-string">'should work'</span>, <span class="hljs-function">() =></span> { 603 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 604 }); 605 606 <span class="hljs-title function_">it</span>(<span class="hljs-string">'should be ok'</span>, <span class="hljs-function">() =></span> { 607 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>); 608 }); 609 610 <span class="hljs-title function_">describe</span>(<span class="hljs-string">'a nested thing'</span>, <span class="hljs-function">() =></span> { 611 <span class="hljs-title function_">it</span>(<span class="hljs-string">'should work'</span>, <span class="hljs-function">() =></span> { 612 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">3</span>, <span class="hljs-number">3</span>); 613 }); 614 }); 615});</code> <button class="copy-button">copy</button></pre> 616<p><code>describe</code> and <code>it</code> are imported from the <code>node:test</code> module.</p> 617 618<pre class="with-46-chars"><input class="js-flavor-selector" 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> 619</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> 620<p>If Node.js is started with the <a href="cli.html#--test-only"><code>--test-only</code></a> command-line option, it is 621possible to skip all top level tests except for a selected subset by passing 622the <code>only</code> option to the tests that should be run. When a test with the <code>only</code> 623option set is run, all subtests are also run. The test context's <code>runOnly()</code> 624method can be used to implement the same behavior at the subtest level.</p> 625<pre><code class="language-js"><span class="hljs-comment">// Assume Node.js is run with the --test-only command-line option.</span> 626<span class="hljs-comment">// The 'only' option is set, so this test is run.</span> 627<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-keyword">async</span> (t) => { 628 <span class="hljs-comment">// Within this test, all subtests are run by default.</span> 629 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'running subtest'</span>); 630 631 <span class="hljs-comment">// The test context can be updated to run subtests with the 'only' option.</span> 632 t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">true</span>); 633 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now skipped'</span>); 634 <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> }); 635 636 <span class="hljs-comment">// Switch the context back to execute all tests.</span> 637 t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">false</span>); 638 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now run'</span>); 639 640 <span class="hljs-comment">// Explicitly do not run these tests.</span> 641 <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> }); 642 <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> }); 643}); 644 645<span class="hljs-comment">// The 'only' option is not set, so this test is skipped.</span> 646<span class="hljs-title function_">test</span>(<span class="hljs-string">'this test is not run'</span>, <span class="hljs-function">() =></span> { 647 <span class="hljs-comment">// This code is not run.</span> 648 <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>); 649});</code> <button class="copy-button">copy</button></pre> 650</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> 651<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 652whose name matches the provided pattern. Test name patterns are interpreted as 653JavaScript regular expressions. The <code>--test-name-pattern</code> option can be 654specified multiple times in order to run nested tests. For each test that is 655executed, any corresponding test hooks, such as <code>beforeEach()</code>, are also 656run.</p> 657<p>Given the following test file, starting Node.js with the 658<code>--test-name-pattern="test [1-3]"</code> option would cause the test runner to execute 659<code>test 1</code>, <code>test 2</code>, and <code>test 3</code>. If <code>test 1</code> did not match the test name 660pattern, then its subtests would not execute, despite matching the pattern. The 661same set of tests could also be executed by passing <code>--test-name-pattern</code> 662multiple times (e.g. <code>--test-name-pattern="test 1"</code>, 663<code>--test-name-pattern="test 2"</code>, etc.).</p> 664<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'test 1'</span>, <span class="hljs-keyword">async</span> (t) => { 665 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 2'</span>); 666 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 3'</span>); 667}); 668 669<span class="hljs-title function_">test</span>(<span class="hljs-string">'Test 4'</span>, <span class="hljs-keyword">async</span> (t) => { 670 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'Test 5'</span>); 671 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 6'</span>); 672});</code> <button class="copy-button">copy</button></pre> 673<p>Test name patterns can also be specified using regular expression literals. This 674allows regular expression flags to be used. In the previous example, starting 675Node.js with <code>--test-name-pattern="/test [4-5]/i"</code> would match <code>Test 4</code> and 676<code>Test 5</code> because the pattern is case-insensitive.</p> 677<p>Test name patterns do not change the set of files that the test runner executes.</p> 678</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> 679<p>Once a test function finishes executing, the results are reported as quickly 680as possible while maintaining the order of the tests. However, it is possible 681for the test function to generate asynchronous activity that outlives the test 682itself. The test runner handles this type of activity, but does not delay the 683reporting of test results in order to accommodate it.</p> 684<p>In the following example, a test completes with two <code>setImmediate()</code> 685operations still outstanding. The first <code>setImmediate()</code> attempts to create a 686new subtest. Because the parent test has already finished and output its 687results, the new subtest is immediately marked as failed, and reported later 688to the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p> 689<p>The second <code>setImmediate()</code> creates an <code>uncaughtException</code> event. 690<code>uncaughtException</code> and <code>unhandledRejection</code> events originating from a completed 691test are marked as failed by the <code>test</code> module and reported as diagnostic 692warnings at the top level by the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p> 693<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> { 694 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 695 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> { 696 <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>); 697 }); 698 }); 699 700 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 701 <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>); 702 }); 703 704 <span class="hljs-comment">// The test finishes after this line.</span> 705});</code> <button class="copy-button">copy</button></pre> 706</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> 707<div class="api_metadata"> 708<span>Added in: v18.13.0</span> 709</div> 710<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 711<p>The Node.js test runner supports running in watch mode by passing the <code>--watch</code> flag:</p> 712<pre><code class="language-bash">node --<span class="hljs-built_in">test</span> --watch</code> <button class="copy-button">copy</button></pre> 713<p>In watch mode, the test runner will watch for changes to test files and 714their dependencies. When a change is detected, the test runner will 715rerun the tests affected by the change. 716The test runner will continue to run until the process is terminated.</p> 717</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> 718<p>The Node.js test runner can be invoked from the command line by passing the 719<a href="cli.html#--test"><code>--test</code></a> flag:</p> 720<pre><code class="language-bash">node --<span class="hljs-built_in">test</span></code> <button class="copy-button">copy</button></pre> 721<p>By default, Node.js will recursively search the current directory for 722JavaScript source files matching a specific naming convention. Matching files 723are executed as test files. More information on the expected test file naming 724convention and behavior can be found in the <a href="#test-runner-execution-model">test runner execution model</a> 725section.</p> 726<p>Alternatively, one or more paths can be provided as the final argument(s) to 727the Node.js command, as shown below.</p> 728<pre><code class="language-bash">node --<span class="hljs-built_in">test</span> test1.js test2.mjs custom_test_dir/</code> <button class="copy-button">copy</button></pre> 729<p>In this example, the test runner will execute the files <code>test1.js</code> and 730<code>test2.mjs</code>. The test runner will also recursively search the 731<code>custom_test_dir/</code> directory for test files to execute.</p> 732<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> 733<p>When searching for test files to execute, the test runner behaves as follows:</p> 734<ul> 735<li>Any files explicitly provided by the user are executed.</li> 736<li>If the user did not explicitly specify any paths, the current working 737directory is recursively searched for files as specified in the following 738steps.</li> 739<li><code>node_modules</code> directories are skipped unless explicitly provided by the 740user.</li> 741<li>If a directory named <code>test</code> is encountered, the test runner will search it 742recursively for all all <code>.js</code>, <code>.cjs</code>, and <code>.mjs</code> files. All of these files 743are treated as test files, and do not need to match the specific naming 744convention detailed below. This is to accommodate projects that place all of 745their tests in a single <code>test</code> directory.</li> 746<li>In all other directories, <code>.js</code>, <code>.cjs</code>, and <code>.mjs</code> files matching the 747following patterns are treated as test files: 748<ul> 749<li><code>^test$</code> - Files whose basename is the string <code>'test'</code>. Examples: 750<code>test.js</code>, <code>test.cjs</code>, <code>test.mjs</code>.</li> 751<li><code>^test-.+</code> - Files whose basename starts with the string <code>'test-'</code> 752followed by one or more characters. Examples: <code>test-example.js</code>, 753<code>test-another-example.mjs</code>.</li> 754<li><code>.+[\.\-\_]test$</code> - Files whose basename ends with <code>.test</code>, <code>-test</code>, or 755<code>_test</code>, preceded by one or more characters. Examples: <code>example.test.js</code>, 756<code>example-test.cjs</code>, <code>example_test.mjs</code>.</li> 757<li>Other file types understood by Node.js such as <code>.node</code> and <code>.json</code> are not 758automatically executed by the test runner, but are supported if explicitly 759provided on the command line.</li> 760</ul> 761</li> 762</ul> 763<p>Each matching test file is executed in a separate child process. The maximum 764number of child processes running at any time is controlled by the 765<a href="cli.html#--test-concurrency"><code>--test-concurrency</code></a> flag. If the child process finishes with an exit code 766of 0, the test is considered passing. Otherwise, the test is considered to be a 767failure. Test files must be executable by Node.js, but are not required to use 768the <code>node:test</code> module internally.</p> 769<p>Each test file is executed as if it was a regular script. That is, if the test 770file itself uses <code>node:test</code> to define tests, all of those tests will be 771executed within a single application thread, regardless of the value of the 772<code>concurrency</code> option of <a href="#testname-options-fn"><code>test()</code></a>.</p> 773</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> 774<p>When Node.js is started with the <a href="cli.html#--experimental-test-coverage"><code>--experimental-test-coverage</code></a> 775command-line flag, code coverage is collected and statistics are reported once 776all tests have completed. If the <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a> environment variable is 777used to specify a code coverage directory, the generated V8 coverage files are 778written to that directory. Node.js core modules and files within 779<code>node_modules/</code> directories are not included in the coverage report. If 780coverage is enabled, the coverage report is sent to any <a href="#test-reporters">test reporters</a> via 781the <code>'test:coverage'</code> event.</p> 782<p>Coverage can be disabled on a series of lines using the following 783comment syntax:</p> 784<pre><code class="language-js"><span class="hljs-comment">/* node:coverage disable */</span> 785<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) { 786 <span class="hljs-comment">// Code in this branch will never be executed, but the lines are ignored for</span> 787 <span class="hljs-comment">// coverage purposes. All lines following the 'disable' comment are ignored</span> 788 <span class="hljs-comment">// until a corresponding 'enable' comment is encountered.</span> 789 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>); 790} 791<span class="hljs-comment">/* node:coverage enable */</span></code> <button class="copy-button">copy</button></pre> 792<p>Coverage can also be disabled for a specified number of lines. After the 793specified number of lines, coverage will be automatically reenabled. If the 794number of lines is not explicitly provided, a single line is ignored.</p> 795<pre><code class="language-js"><span class="hljs-comment">/* node:coverage ignore next */</span> 796<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>); } 797 798<span class="hljs-comment">/* node:coverage ignore next 3 */</span> 799<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) { 800 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>); 801}</code> <button class="copy-button">copy</button></pre> 802<p>The test runner's code coverage functionality has the following limitations, 803which will be addressed in a future Node.js release:</p> 804<ul> 805<li>Source maps are not supported.</li> 806<li>Excluding specific files or directories from the coverage report is not 807supported.</li> 808</ul> 809</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> 810<p>The <code>node:test</code> module supports mocking during testing via a top-level <code>mock</code> 811object. The following example creates a spy on a function that adds two numbers 812together. The spy is then used to assert that the function was called as 813expected.</p> 814 815<pre class="with-39-chars"><input class="js-flavor-selector" 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>; 816<span class="hljs-keyword">import</span> { mock, test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 817 818<span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on a function'</span>, <span class="hljs-function">() =></span> { 819 <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> { 820 <span class="hljs-keyword">return</span> a + b; 821 }); 822 823 assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">0</span>); 824 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>); 825 assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">1</span>); 826 827 <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>]; 828 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>]); 829 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">7</span>); 830 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>); 831 832 <span class="hljs-comment">// Reset the globally tracked mocks.</span> 833 mock.<span class="hljs-title function_">reset</span>(); 834});</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>; 835<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 836<span class="hljs-keyword">const</span> { mock, test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 837 838<span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on a function'</span>, <span class="hljs-function">() =></span> { 839 <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> { 840 <span class="hljs-keyword">return</span> a + b; 841 }); 842 843 assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">0</span>); 844 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>); 845 assert.<span class="hljs-title function_">strictEqual</span>(sum.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">1</span>); 846 847 <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>]; 848 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>]); 849 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">7</span>); 850 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>); 851 852 <span class="hljs-comment">// Reset the globally tracked mocks.</span> 853 mock.<span class="hljs-title function_">reset</span>(); 854});</code><button class="copy-button">copy</button></pre> 855<p>The same mocking functionality is also exposed on the <a href="#class-testcontext"><code>TestContext</code></a> object 856of each test. The following example creates a spy on an object method using the 857API exposed on the <code>TestContext</code>. The benefit of mocking via the test context is 858that the test runner will automatically restore all mocked functionality once 859the test finishes.</p> 860<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> { 861 <span class="hljs-keyword">const</span> number = { 862 <span class="hljs-attr">value</span>: <span class="hljs-number">5</span>, 863 <span class="hljs-title function_">add</span>(<span class="hljs-params">a</span>) { 864 <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> + a; 865 }, 866 }; 867 868 t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">method</span>(number, <span class="hljs-string">'add'</span>); 869 assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">add</span>.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">0</span>); 870 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>); 871 assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">add</span>.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">1</span>); 872 873 <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>]; 874 875 assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>]); 876 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">8</span>); 877 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">target</span>, <span class="hljs-literal">undefined</span>); 878 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">this</span>, number); 879});</code> <button class="copy-button">copy</button></pre> 880<h4>Timers<span><a class="mark" href="#timers" id="timers">#</a></span><a aria-hidden="true" class="legacy" id="test_timers"></a></h4> 881<p>Mocking timers is a technique commonly used in software testing to simulate and 882control the behavior of timers, such as <code>setInterval</code> and <code>setTimeout</code>, 883without actually waiting for the specified time intervals.</p> 884<p>Refer to the <a href="#class-mocktimers"><code>MockTimers</code></a> class for a full list of methods and features.</p> 885<p>This allows developers to write more reliable and 886predictable tests for time-dependent functionality.</p> 887<p>The example below shows how to mock <code>setTimeout</code>. 888Using <code>.enable(['setTimeout']);</code> 889it will mock the <code>setTimeout</code> functions in the <a href="./timers.html">node:timers</a> and 890<a href="./timers.html#timers-promises-api">node:timers/promises</a> modules, 891as well as from the Node.js global context.</p> 892<p><strong>Note:</strong> Destructuring functions such as 893<code>import { setTimeout } from 'node:timers'</code> 894is currently not supported by this API.</p> 895<pre><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>; 896<span class="hljs-keyword">import</span> { mock, test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 897 898<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> { 899 <span class="hljs-keyword">const</span> fn = mock.<span class="hljs-title function_">fn</span>(); 900 901 <span class="hljs-comment">// Optionally choose what to mock</span> 902 mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 903 <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 904 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>); 905 906 <span class="hljs-comment">// Advance in time</span> 907 mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>); 908 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>); 909 910 <span class="hljs-comment">// Reset the globally tracked mocks.</span> 911 mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>(); 912 913 <span class="hljs-comment">// If you call reset mock instance, it will also reset timers instance</span> 914 mock.<span class="hljs-title function_">reset</span>(); 915});</code> <button class="copy-button">copy</button></pre> 916<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 917<span class="hljs-keyword">const</span> { mock, test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 918 919<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> { 920 <span class="hljs-keyword">const</span> fn = mock.<span class="hljs-title function_">fn</span>(); 921 922 <span class="hljs-comment">// Optionally choose what to mock</span> 923 mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 924 <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 925 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>); 926 927 <span class="hljs-comment">// Advance in time</span> 928 mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>); 929 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>); 930 931 <span class="hljs-comment">// Reset the globally tracked mocks.</span> 932 mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>(); 933 934 <span class="hljs-comment">// If you call reset mock instance, it'll also reset timers instance</span> 935 mock.<span class="hljs-title function_">reset</span>(); 936});</code> <button class="copy-button">copy</button></pre> 937<p>The same mocking functionality is also exposed in the mock property on the <a href="#class-testcontext"><code>TestContext</code></a> object 938of each test. The benefit of mocking via the test context is 939that the test runner will automatically restore all mocked timers 940functionality once the test finishes.</p> 941<pre><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>; 942<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 943 944<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> { 945 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 946 947 <span class="hljs-comment">// Optionally choose what to mock</span> 948 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 949 <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 950 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>); 951 952 <span class="hljs-comment">// Advance in time</span> 953 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>); 954 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>); 955});</code> <button class="copy-button">copy</button></pre> 956<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 957<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 958 959<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> { 960 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 961 962 <span class="hljs-comment">// Optionally choose what to mock</span> 963 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 964 <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 965 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>); 966 967 <span class="hljs-comment">// Advance in time</span> 968 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>); 969 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>); 970});</code> <button class="copy-button">copy</button></pre> 971</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> 972<div class="api_metadata"> 973<details class="changelog"><summary>History</summary> 974<table> 975<tbody><tr><th>Version</th><th>Changes</th></tr> 976<tr><td>v18.17.0</td> 977<td><p>Reporters are now exposed at <code>node:test/reporters</code>.</p></td></tr> 978<tr><td>v18.15.0</td> 979<td><p><span>Added in: v18.15.0</span></p></td></tr> 980</tbody></table> 981</details> 982</div> 983<p>The <code>node:test</code> module supports passing <a href="cli.html#--test-reporter"><code>--test-reporter</code></a> 984flags for the test runner to use a specific reporter.</p> 985<p>The following built-reporters are supported:</p> 986<ul> 987<li> 988<p><code>tap</code> 989The <code>tap</code> reporter outputs the test results in the <a href="https://testanything.org/">TAP</a> format.</p> 990</li> 991<li> 992<p><code>spec</code> 993The <code>spec</code> reporter outputs the test results in a human-readable format.</p> 994</li> 995<li> 996<p><code>dot</code> 997The <code>dot</code> reporter outputs the test results in a compact format, 998where each passing test is represented by a <code>.</code>, 999and each failing test is represented by a <code>X</code>.</p> 1000</li> 1001<li> 1002<p><code>junit</code> 1003The junit reporter outputs test results in a jUnit XML format</p> 1004</li> 1005</ul> 1006<p>When <code>stdout</code> is a <a href="tty.html">TTY</a>, the <code>spec</code> reporter is used by default. 1007Otherwise, the <code>tap</code> reporter is used by default.</p> 1008<p>The reporters are available via the <code>node:test/reporters</code> module:</p> 1009 1010<pre class="with-65-chars"><input class="js-flavor-selector" 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 } <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 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test/reporters'</span>);</code><button class="copy-button">copy</button></pre> 1011<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> 1012<p><a href="cli.html#--test-reporter"><code>--test-reporter</code></a> can be used to specify a path to custom reporter. 1013A custom reporter is a module that exports a value 1014accepted by <a href="stream.html#streamcomposestreams">stream.compose</a>. 1015Reporters should transform events emitted by a <a href="test.html#class-testsstream" class="type"><TestsStream></a></p> 1016<p>Example of a custom reporter using <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a>:</p> 1017 1018<pre class="with-45-chars"><input class="js-flavor-selector" 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>; 1019 1020<span class="hljs-keyword">const</span> customReporter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Transform</span>({ 1021 <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>, 1022 <span class="hljs-title function_">transform</span>(<span class="hljs-params">event, encoding, callback</span>) { 1023 <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 1024 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 1025 <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>); 1026 <span class="hljs-keyword">break</span>; 1027 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 1028 <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>); 1029 <span class="hljs-keyword">break</span>; 1030 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 1031 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test watch queue drained'</span>); 1032 <span class="hljs-keyword">break</span>; 1033 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 1034 <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>); 1035 <span class="hljs-keyword">break</span>; 1036 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 1037 <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>); 1038 <span class="hljs-keyword">break</span>; 1039 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 1040 <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>); 1041 <span class="hljs-keyword">break</span>; 1042 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 1043 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test plan'</span>); 1044 <span class="hljs-keyword">break</span>; 1045 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 1046 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 1047 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 1048 <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>); 1049 <span class="hljs-keyword">break</span>; 1050 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 1051 <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>; 1052 <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>); 1053 <span class="hljs-keyword">break</span>; 1054 } 1055 } 1056 }, 1057}); 1058 1059<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>); 1060 1061<span class="hljs-keyword">const</span> customReporter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Transform</span>({ 1062 <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>, 1063 <span class="hljs-title function_">transform</span>(<span class="hljs-params">event, encoding, callback</span>) { 1064 <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 1065 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 1066 <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>); 1067 <span class="hljs-keyword">break</span>; 1068 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 1069 <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>); 1070 <span class="hljs-keyword">break</span>; 1071 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 1072 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test watch queue drained'</span>); 1073 <span class="hljs-keyword">break</span>; 1074 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 1075 <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>); 1076 <span class="hljs-keyword">break</span>; 1077 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 1078 <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>); 1079 <span class="hljs-keyword">break</span>; 1080 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 1081 <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>); 1082 <span class="hljs-keyword">break</span>; 1083 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 1084 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test plan'</span>); 1085 <span class="hljs-keyword">break</span>; 1086 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 1087 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 1088 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 1089 <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>); 1090 <span class="hljs-keyword">break</span>; 1091 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 1092 <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>; 1093 <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>); 1094 <span class="hljs-keyword">break</span>; 1095 } 1096 } 1097 }, 1098}); 1099 1100<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = customReporter;</code><button class="copy-button">copy</button></pre> 1101<p>Example of a custom reporter using a generator function:</p> 1102 1103<pre class="with-58-chars"><input class="js-flavor-selector" 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>) { 1104 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> source) { 1105 <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 1106 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 1107 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued`</span>; 1108 <span class="hljs-keyword">break</span>; 1109 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 1110 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued`</span>; 1111 <span class="hljs-keyword">break</span>; 1112 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 1113 <span class="hljs-keyword">yield</span> <span class="hljs-string">'test watch queue drained'</span>; 1114 <span class="hljs-keyword">break</span>; 1115 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 1116 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started\n`</span>; 1117 <span class="hljs-keyword">break</span>; 1118 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 1119 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed\n`</span>; 1120 <span class="hljs-keyword">break</span>; 1121 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 1122 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed\n`</span>; 1123 <span class="hljs-keyword">break</span>; 1124 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 1125 <span class="hljs-keyword">yield</span> <span class="hljs-string">'test plan'</span>; 1126 <span class="hljs-keyword">break</span>; 1127 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 1128 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 1129 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 1130 <span class="hljs-keyword">yield</span> <span class="hljs-string">`<span class="hljs-subst">${event.data.message}</span>\n`</span>; 1131 <span class="hljs-keyword">break</span>; 1132 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 1133 <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>; 1134 <span class="hljs-keyword">yield</span> <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>; 1135 <span class="hljs-keyword">break</span>; 1136 } 1137 } 1138 } 1139}</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>) { 1140 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> source) { 1141 <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 1142 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 1143 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued`</span>; 1144 <span class="hljs-keyword">break</span>; 1145 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 1146 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued`</span>; 1147 <span class="hljs-keyword">break</span>; 1148 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 1149 <span class="hljs-keyword">yield</span> <span class="hljs-string">'test watch queue drained'</span>; 1150 <span class="hljs-keyword">break</span>; 1151 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 1152 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started\n`</span>; 1153 <span class="hljs-keyword">break</span>; 1154 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 1155 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed\n`</span>; 1156 <span class="hljs-keyword">break</span>; 1157 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 1158 <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed\n`</span>; 1159 <span class="hljs-keyword">break</span>; 1160 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 1161 <span class="hljs-keyword">yield</span> <span class="hljs-string">'test plan\n'</span>; 1162 <span class="hljs-keyword">break</span>; 1163 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 1164 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 1165 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 1166 <span class="hljs-keyword">yield</span> <span class="hljs-string">`<span class="hljs-subst">${event.data.message}</span>\n`</span>; 1167 <span class="hljs-keyword">break</span>; 1168 <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 1169 <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>; 1170 <span class="hljs-keyword">yield</span> <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>; 1171 <span class="hljs-keyword">break</span>; 1172 } 1173 } 1174 } 1175};</code><button class="copy-button">copy</button></pre> 1176<p>The value provided to <code>--test-reporter</code> should be a string like one used in an 1177<code>import()</code> in JavaScript code.</p> 1178<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> 1179<p>The <a href="cli.html#--test-reporter"><code>--test-reporter</code></a> flag can be specified multiple times to report test 1180results in several formats. In this situation 1181it is required to specify a destination for each reporter 1182using <a href="cli.html#--test-reporter-destination"><code>--test-reporter-destination</code></a>. 1183Destination can be <code>stdout</code>, <code>stderr</code>, or a file path. 1184Reporters and destinations are paired according 1185to the order they were specified.</p> 1186<p>In the following example, the <code>spec</code> reporter will output to <code>stdout</code>, 1187and the <code>dot</code> reporter will output to <code>file.txt</code>:</p> 1188<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> 1189<p>When a single reporter is specified, the destination will default to <code>stdout</code>, 1190unless a destination is explicitly provided.</p> 1191</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> 1192<div class="api_metadata"> 1193<details class="changelog"><summary>History</summary> 1194<table> 1195<tbody><tr><th>Version</th><th>Changes</th></tr> 1196<tr><td>v18.17.0</td> 1197<td><p>Add a testNamePatterns option.</p></td></tr> 1198<tr><td>v18.9.0</td> 1199<td><p><span>Added in: v18.9.0</span></p></td></tr> 1200</tbody></table> 1201</details> 1202</div> 1203<ul> 1204<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 1205properties are supported: 1206<ul> 1207<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, 1208then that many test processes would run in parallel, where each process 1209corresponds to one test file. 1210If <code>true</code>, it would run <code>os.availableParallelism() - 1</code> test files in 1211parallel. 1212If <code>false</code>, it would only run one test file at a time. 1213<strong>Default:</strong> <code>false</code>.</li> 1214<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. 1215<strong>Default</strong> matching files from <a href="#test-runner-execution-model">test runner execution model</a>.</li> 1216<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. 1217This can be a number, or a function that takes no arguments and returns a 1218number. If a nullish value is provided, each process gets its own port, 1219incremented from the primary's <code>process.debugPort</code>. 1220<strong>Default:</strong> <code>undefined</code>.</li> 1221<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 1222have the <code>only</code> option set</li> 1223<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 1224and can be used to setup listeners before any tests are run. 1225<strong>Default:</strong> <code>undefined</code>.</li> 1226<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test execution.</li> 1227<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, 1228that can be used to only run tests whose name matches the provided pattern. 1229Test name patterns are interpreted as JavaScript regular expressions. 1230For each test that is executed, any corresponding test hooks, such as 1231<code>beforeEach()</code>, are also run. 1232<strong>Default:</strong> <code>undefined</code>.</li> 1233<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 1234fail after. 1235If unspecified, subtests inherit this value from their parent. 1236<strong>Default:</strong> <code>Infinity</code>.</li> 1237<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> 1238<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>. 1239<ul> 1240<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> 1241that specifies the index of the shard to run. This option is <em>required</em>.</li> 1242<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 1243of shards to split the test files to. This option is <em>required</em>.</li> 1244</ul> 1245</li> 1246</ul> 1247</li> 1248<li>Returns: <a href="test.html#class-testsstream" class="type"><TestsStream></a></li> 1249</ul> 1250 1251<pre class="with-47-chars"><input class="js-flavor-selector" 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>; 1252<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 1253 1254<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>)] }) 1255 .<span class="hljs-title function_">compose</span>(tap) 1256 .<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>); 1257 1258<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>)] }) 1259 .<span class="hljs-title function_">compose</span>(tap) 1260 .<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code><button class="copy-button">copy</button></pre> 1261</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> 1262<div class="api_metadata"> 1263<details class="changelog"><summary>History</summary> 1264<table> 1265<tbody><tr><th>Version</th><th>Changes</th></tr> 1266<tr><td>v18.17.0</td> 1267<td><p>Added the <code>skip</code>, <code>todo</code>, and <code>only</code> shorthands.</p></td></tr> 1268<tr><td>v18.8.0</td> 1269<td><p>Add a <code>signal</code> option.</p></td></tr> 1270<tr><td>v18.7.0</td> 1271<td><p>Add a <code>timeout</code> option.</p></td></tr> 1272<tr><td>v18.0.0</td> 1273<td><p><span>Added in: v18.0.0</span></p></td></tr> 1274</tbody></table> 1275</details> 1276</div> 1277<ul> 1278<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 1279results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if <code>fn</code> 1280does not have a name.</li> 1281<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 1282properties are supported: 1283<ul> 1284<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, 1285then that many tests would run in parallel within the application thread. 1286If <code>true</code>, all scheduled asynchronous tests run concurrently within the 1287thread. If <code>false</code>, only one test runs at a time. 1288If unspecified, subtests inherit this value from their parent. 1289<strong>Default:</strong> <code>false</code>.</li> 1290<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 1291<code>only</code> tests, then this test will be run. Otherwise, the test is skipped. 1292<strong>Default:</strong> <code>false</code>.</li> 1293<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test.</li> 1294<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 1295provided, that string is displayed in the test results as the reason for 1296skipping the test. <strong>Default:</strong> <code>false</code>.</li> 1297<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 1298is provided, that string is displayed in the test results as the reason why 1299the test is <code>TODO</code>. <strong>Default:</strong> <code>false</code>.</li> 1300<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. 1301If unspecified, subtests inherit this value from their parent. 1302<strong>Default:</strong> <code>Infinity</code>.</li> 1303</ul> 1304</li> 1305<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 1306to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the test uses callbacks, 1307the callback function is passed as the second argument. <strong>Default:</strong> A no-op 1308function.</li> 1309<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Resolved with <code>undefined</code> once 1310the test completes, or immediately if the test runs within <a href="#describename-options-fn"><code>describe()</code></a>.</li> 1311</ul> 1312<p>The <code>test()</code> function is the value imported from the <code>test</code> module. Each 1313invocation of this function results in reporting the test to the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p> 1314<p>The <code>TestContext</code> object passed to the <code>fn</code> argument can be used to perform 1315actions related to the current test. Examples include skipping the test, adding 1316additional diagnostic information, or creating subtests.</p> 1317<p><code>test()</code> returns a <code>Promise</code> that resolves once the test completes. 1318if <code>test()</code> is called within a <code>describe()</code> block, it resolve immediately. 1319The return value can usually be discarded for top level tests. 1320However, the return value from subtests should be used to prevent the parent 1321test from finishing first and cancelling the subtest 1322as shown in the following example.</p> 1323<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 1324 <span class="hljs-comment">// The setTimeout() in the following subtest would cause it to outlive its</span> 1325 <span class="hljs-comment">// parent test if 'await' is removed on the next line. Once the parent test</span> 1326 <span class="hljs-comment">// completes, it will cancel any outstanding subtests.</span> 1327 <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-keyword">async</span> (t) => { 1328 <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> { 1329 <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">1000</span>); 1330 }); 1331 }); 1332});</code> <button class="copy-button">copy</button></pre> 1333<p>The <code>timeout</code> option can be used to fail the test if it takes longer than 1334<code>timeout</code> milliseconds to complete. However, it is not a reliable mechanism for 1335canceling tests because a running test might block the application thread and 1336thus prevent the scheduled cancellation.</p> 1337</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> 1338<p>Shorthand for skipping a test, 1339same as <a href="#testname-options-fn"><code>test([name], { skip: true }[, fn])</code></a>.</p> 1340</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> 1341<p>Shorthand for marking a test as <code>TODO</code>, 1342same as <a href="#testname-options-fn"><code>test([name], { todo: true }[, fn])</code></a>.</p> 1343</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> 1344<p>Shorthand for marking a test as <code>only</code>, 1345same as <a href="#testname-options-fn"><code>test([name], { only: true }[, fn])</code></a>.</p> 1346</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> 1347<ul> 1348<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 1349results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if <code>fn</code> 1350does not have a name.</li> 1351<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 suite. 1352supports the same options as <code>test([name][, options][, fn])</code>.</li> 1353<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 suite 1354declaring all subtests and subsuites. 1355The first argument to this function is a <a href="#class-suitecontext"><code>SuiteContext</code></a> object. 1356<strong>Default:</strong> A no-op function.</li> 1357<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> 1358</ul> 1359<p>The <code>describe()</code> function imported from the <code>node:test</code> module. Each 1360invocation of this function results in the creation of a Subtest. 1361After invocation of top level <code>describe</code> functions, 1362all top level tests and suites will execute.</p> 1363</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> 1364<p>Shorthand for skipping a suite, same as <a href="#describename-options-fn"><code>describe([name], { skip: true }[, fn])</code></a>.</p> 1365</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> 1366<p>Shorthand for marking a suite as <code>TODO</code>, same as 1367<a href="#describename-options-fn"><code>describe([name], { todo: true }[, fn])</code></a>.</p> 1368</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> 1369<div class="api_metadata"> 1370<span>Added in: v18.15.0</span> 1371</div> 1372<p>Shorthand for marking a suite as <code>only</code>, same as 1373<a href="#describename-options-fn"><code>describe([name], { only: true }[, fn])</code></a>.</p> 1374</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> 1375<div class="api_metadata"> 1376<details class="changelog"><summary>History</summary> 1377<table> 1378<tbody><tr><th>Version</th><th>Changes</th></tr> 1379<tr><td>v18.16.0</td> 1380<td><p>Calling <code>it()</code> is now equivalent to calling <code>test()</code>.</p></td></tr> 1381<tr><td>v18.6.0, v16.17.0</td> 1382<td><p><span>Added in: v18.6.0, v16.17.0</span></p></td></tr> 1383</tbody></table> 1384</details> 1385</div> 1386<p>Shorthand for <a href="#testname-options-fn"><code>test()</code></a>.</p> 1387<p>The <code>it()</code> function is imported from the <code>node:test</code> module.</p> 1388</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> 1389<p>Shorthand for skipping a test, 1390same as <a href="#testname-options-fn"><code>it([name], { skip: true }[, fn])</code></a>.</p> 1391</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> 1392<p>Shorthand for marking a test as <code>TODO</code>, 1393same as <a href="#testname-options-fn"><code>it([name], { todo: true }[, fn])</code></a>.</p> 1394</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> 1395<div class="api_metadata"> 1396<span>Added in: v18.15.0</span> 1397</div> 1398<p>Shorthand for marking a test as <code>only</code>, 1399same as <a href="#testname-options-fn"><code>it([name], { only: true }[, fn])</code></a>.</p> 1400</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> 1401<div class="api_metadata"> 1402<span>Added in: v18.8.0</span> 1403</div> 1404<ul> 1405<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. 1406If the hook uses callbacks, 1407the callback function is passed as the second argument. <strong>Default:</strong> A no-op 1408function.</li> 1409<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 1410properties are supported: 1411<ul> 1412<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 1413<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. 1414If unspecified, subtests inherit this value from their parent. 1415<strong>Default:</strong> <code>Infinity</code>.</li> 1416</ul> 1417</li> 1418</ul> 1419<p>This function is used to create a hook running before running a suite.</p> 1420<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 1421 <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>)); 1422 <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 1423 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 1424 }); 1425});</code> <button class="copy-button">copy</button></pre> 1426</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> 1427<div class="api_metadata"> 1428<span>Added in: v18.8.0</span> 1429</div> 1430<ul> 1431<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. 1432If the hook uses callbacks, 1433the callback function is passed as the second argument. <strong>Default:</strong> A no-op 1434function.</li> 1435<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 1436properties are supported: 1437<ul> 1438<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 1439<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. 1440If unspecified, subtests inherit this value from their parent. 1441<strong>Default:</strong> <code>Infinity</code>.</li> 1442</ul> 1443</li> 1444</ul> 1445<p>This function is used to create a hook running after running a suite.</p> 1446<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 1447 <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>)); 1448 <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 1449 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 1450 }); 1451});</code> <button class="copy-button">copy</button></pre> 1452</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> 1453<div class="api_metadata"> 1454<span>Added in: v18.8.0</span> 1455</div> 1456<ul> 1457<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. 1458If the hook uses callbacks, 1459the callback function is passed as the second argument. <strong>Default:</strong> A no-op 1460function.</li> 1461<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 1462properties are supported: 1463<ul> 1464<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 1465<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. 1466If unspecified, subtests inherit this value from their parent. 1467<strong>Default:</strong> <code>Infinity</code>.</li> 1468</ul> 1469</li> 1470</ul> 1471<p>This function is used to create a hook running 1472before each subtest of the current suite.</p> 1473<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 1474 <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>)); 1475 <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 1476 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 1477 }); 1478});</code> <button class="copy-button">copy</button></pre> 1479</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> 1480<div class="api_metadata"> 1481<span>Added in: v18.8.0</span> 1482</div> 1483<ul> 1484<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. 1485If the hook uses callbacks, 1486the callback function is passed as the second argument. <strong>Default:</strong> A no-op 1487function.</li> 1488<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 1489properties are supported: 1490<ul> 1491<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 1492<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. 1493If unspecified, subtests inherit this value from their parent. 1494<strong>Default:</strong> <code>Infinity</code>.</li> 1495</ul> 1496</li> 1497</ul> 1498<p>This function is used to create a hook running 1499after each subtest of the current test.</p> 1500<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 1501 <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>)); 1502 <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 1503 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 1504 }); 1505});</code> <button class="copy-button">copy</button></pre> 1506</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> 1507<div class="api_metadata"> 1508<span>Added in: v18.13.0</span> 1509</div> 1510<p>The <code>MockFunctionContext</code> class is used to inspect or manipulate the behavior of 1511mocks created via the <a href="#class-mocktracker"><code>MockTracker</code></a> APIs.</p> 1512<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> 1513<div class="api_metadata"> 1514<span>Added in: v18.13.0</span> 1515</div> 1516<ul> 1517<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 1518</ul> 1519<p>A getter that returns a copy of the internal array used to track calls to the 1520mock. Each entry in the array is an object with the following properties.</p> 1521<ul> 1522<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> 1523<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 1524thrown value. <strong>Default:</strong> <code>undefined</code>.</li> 1525<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> 1526<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 1527callsite of the mocked function invocation.</li> 1528<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 1529field contains the class being constructed. Otherwise this will be 1530<code>undefined</code>.</li> 1531<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> 1532</ul> 1533<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> 1534<div class="api_metadata"> 1535<span>Added in: v18.13.0</span> 1536</div> 1537<ul> 1538<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> 1539</ul> 1540<p>This function returns the number of times that this mock has been invoked. This 1541function is more efficient than checking <code>ctx.calls.length</code> because <code>ctx.calls</code> 1542is a getter that creates a copy of the internal call tracking array.</p> 1543<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> 1544<div class="api_metadata"> 1545<span>Added in: v18.13.0</span> 1546</div> 1547<ul> 1548<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 1549mock's new implementation.</li> 1550</ul> 1551<p>This function is used to change the behavior of an existing mock.</p> 1552<p>The following example creates a mock function using <code>t.mock.fn()</code>, calls the 1553mock function, and then changes the mock implementation to a different function.</p> 1554<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> { 1555 <span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>; 1556 1557 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) { 1558 cnt++; 1559 <span class="hljs-keyword">return</span> cnt; 1560 } 1561 1562 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) { 1563 cnt += <span class="hljs-number">2</span>; 1564 <span class="hljs-keyword">return</span> cnt; 1565 } 1566 1567 <span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne); 1568 1569 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">1</span>); 1570 fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">mockImplementation</span>(addTwo); 1571 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">3</span>); 1572 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">5</span>); 1573});</code> <button class="copy-button">copy</button></pre> 1574<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> 1575<div class="api_metadata"> 1576<span>Added in: v18.13.0</span> 1577</div> 1578<ul> 1579<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 1580mock's implementation for the invocation number specified by <code>onCall</code>.</li> 1581<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 1582the specified invocation has already occurred then an exception is thrown. 1583<strong>Default:</strong> The number of the next invocation.</li> 1584</ul> 1585<p>This function is used to change the behavior of an existing mock for a single 1586invocation. Once invocation <code>onCall</code> has occurred, the mock will revert to 1587whatever behavior it would have used had <code>mockImplementationOnce()</code> not been 1588called.</p> 1589<p>The following example creates a mock function using <code>t.mock.fn()</code>, calls the 1590mock function, changes the mock implementation to a different function for the 1591next invocation, and then resumes its previous behavior.</p> 1592<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> { 1593 <span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>; 1594 1595 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) { 1596 cnt++; 1597 <span class="hljs-keyword">return</span> cnt; 1598 } 1599 1600 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) { 1601 cnt += <span class="hljs-number">2</span>; 1602 <span class="hljs-keyword">return</span> cnt; 1603 } 1604 1605 <span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne); 1606 1607 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">1</span>); 1608 fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">mockImplementationOnce</span>(addTwo); 1609 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">3</span>); 1610 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">4</span>); 1611});</code> <button class="copy-button">copy</button></pre> 1612<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> 1613<div class="api_metadata"> 1614<span>Added in: v18.13.0</span> 1615</div> 1616<p>Resets the call history of the mock function.</p> 1617<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> 1618<div class="api_metadata"> 1619<span>Added in: v18.13.0</span> 1620</div> 1621<p>Resets the implementation of the mock function to its original behavior. The 1622mock can still be used after calling this function.</p> 1623</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> 1624<div class="api_metadata"> 1625<span>Added in: v18.13.0</span> 1626</div> 1627<p>The <code>MockTracker</code> class is used to manage mocking functionality. The test runner 1628module provides a top level <code>mock</code> export which is a <code>MockTracker</code> instance. 1629Each test also provides its own <code>MockTracker</code> instance via the test context's 1630<code>mock</code> property.</p> 1631<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> 1632<div class="api_metadata"> 1633<span>Added in: v18.13.0</span> 1634</div> 1635<ul> 1636<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. 1637<strong>Default:</strong> A no-op function.</li> 1638<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 1639mock implementation for <code>original</code>. This is useful for creating mocks that 1640exhibit one behavior for a specified number of calls and then restore the 1641behavior of <code>original</code>. <strong>Default:</strong> The function specified by <code>original</code>.</li> 1642<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 1643following properties are supported: 1644<ul> 1645<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 1646<code>implementation</code>. Once the mock function has been called <code>times</code> times, it 1647will automatically restore the behavior of <code>original</code>. This value must be an 1648integer greater than zero. <strong>Default:</strong> <code>Infinity</code>.</li> 1649</ul> 1650</li> 1651<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 1652<code>mock</code> property, which is an instance of <a href="#class-mockfunctioncontext"><code>MockFunctionContext</code></a>, and can 1653be used for inspecting and changing the behavior of the mocked function.</li> 1654</ul> 1655<p>This function is used to create a mock function.</p> 1656<p>The following example creates a mock function that increments a counter by one 1657on each invocation. The <code>times</code> option is used to modify the mock behavior such 1658that the first two invocations add two to the counter instead of one.</p> 1659<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> { 1660 <span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>; 1661 1662 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) { 1663 cnt++; 1664 <span class="hljs-keyword">return</span> cnt; 1665 } 1666 1667 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) { 1668 cnt += <span class="hljs-number">2</span>; 1669 <span class="hljs-keyword">return</span> cnt; 1670 } 1671 1672 <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> }); 1673 1674 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">2</span>); 1675 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">4</span>); 1676 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">5</span>); 1677 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">6</span>); 1678});</code> <button class="copy-button">copy</button></pre> 1679<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> 1680<div class="api_metadata"> 1681<span>Added in: v18.13.0</span> 1682</div> 1683<p>This function is syntax sugar for <a href="#mockmethodobject-methodname-implementation-options"><code>MockTracker.method</code></a> with <code>options.getter</code> 1684set to <code>true</code>.</p> 1685<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> 1686<div class="api_metadata"> 1687<span>Added in: v18.13.0</span> 1688</div> 1689<ul> 1690<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> 1691<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. 1692If <code>object[methodName]</code> is not a function, an error is thrown.</li> 1693<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 1694mock implementation for <code>object[methodName]</code>. <strong>Default:</strong> The original method 1695specified by <code>object[methodName]</code>.</li> 1696<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 1697following properties are supported: 1698<ul> 1699<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. 1700This option cannot be used with the <code>setter</code> option. <strong>Default:</strong> false.</li> 1701<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. 1702This option cannot be used with the <code>getter</code> option. <strong>Default:</strong> false.</li> 1703<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 1704<code>implementation</code>. Once the mocked method has been called <code>times</code> times, it 1705will automatically restore the original behavior. This value must be an 1706integer greater than zero. <strong>Default:</strong> <code>Infinity</code>.</li> 1707</ul> 1708</li> 1709<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 1710<code>mock</code> property, which is an instance of <a href="#class-mockfunctioncontext"><code>MockFunctionContext</code></a>, and can 1711be used for inspecting and changing the behavior of the mocked method.</li> 1712</ul> 1713<p>This function is used to create a mock on an existing object method. The 1714following example demonstrates how a mock is created on an existing object 1715method.</p> 1716<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> { 1717 <span class="hljs-keyword">const</span> number = { 1718 <span class="hljs-attr">value</span>: <span class="hljs-number">5</span>, 1719 <span class="hljs-title function_">subtract</span>(<span class="hljs-params">a</span>) { 1720 <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> - a; 1721 }, 1722 }; 1723 1724 t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">method</span>(number, <span class="hljs-string">'subtract'</span>); 1725 assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">subtract</span>.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">0</span>); 1726 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>); 1727 assert.<span class="hljs-title function_">strictEqual</span>(number.<span class="hljs-property">subtract</span>.<span class="hljs-property">mock</span>.<span class="hljs-property">calls</span>.<span class="hljs-property">length</span>, <span class="hljs-number">1</span>); 1728 1729 <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>]; 1730 1731 assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>]); 1732 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">2</span>); 1733 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>); 1734 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">target</span>, <span class="hljs-literal">undefined</span>); 1735 assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">this</span>, number); 1736});</code> <button class="copy-button">copy</button></pre> 1737<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> 1738<div class="api_metadata"> 1739<span>Added in: v18.13.0</span> 1740</div> 1741<p>This function restores the default behavior of all mocks that were previously 1742created by this <code>MockTracker</code> and disassociates the mocks from the 1743<code>MockTracker</code> instance. Once disassociated, the mocks can still be used, but the 1744<code>MockTracker</code> instance can no longer be used to reset their behavior or 1745otherwise interact with them.</p> 1746<p>After each test completes, this function is called on the test context's 1747<code>MockTracker</code>. If the global <code>MockTracker</code> is used extensively, calling this 1748function manually is recommended.</p> 1749<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> 1750<div class="api_metadata"> 1751<span>Added in: v18.13.0</span> 1752</div> 1753<p>This function restores the default behavior of all mocks that were previously 1754created by this <code>MockTracker</code>. Unlike <code>mock.reset()</code>, <code>mock.restoreAll()</code> does 1755not disassociate the mocks from the <code>MockTracker</code> instance.</p> 1756<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> 1757<div class="api_metadata"> 1758<span>Added in: v18.13.0</span> 1759</div> 1760<p>This function is syntax sugar for <a href="#mockmethodobject-methodname-implementation-options"><code>MockTracker.method</code></a> with <code>options.setter</code> 1761set to <code>true</code>.</p> 1762</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> 1763<div class="api_metadata"> 1764<span>Added in: v18.19.0</span> 1765</div> 1766<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1767<p>Mocking timers is a technique commonly used in software testing to simulate and 1768control the behavior of timers, such as <code>setInterval</code> and <code>setTimeout</code>, 1769without actually waiting for the specified time intervals.</p> 1770<p>The <a href="#class-mocktracker"><code>MockTracker</code></a> provides a top-level <code>timers</code> export 1771which is a <code>MockTimers</code> instance.</p> 1772<h4><code>timers.enable([timers])</code><span><a class="mark" href="#timersenabletimers" id="timersenabletimers">#</a></span><a aria-hidden="true" class="legacy" id="test_timers_enable_timers"></a></h4> 1773<div class="api_metadata"> 1774<span>Added in: v18.19.0</span> 1775</div> 1776<p>Enables timer mocking for the specified timers.</p> 1777<ul> 1778<li><code>timers</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. 1779The currently supported timer values are <code>'setInterval'</code>, <code>'setTimeout'</code>, 1780and <code>'setImmediate'</code>. If no value is provided, all timers (<code>'setInterval'</code>, 1781<code>'clearInterval'</code>, <code>'setTimeout'</code>, <code>'clearTimeout'</code>, <code>'setImmediate'</code>, 1782and <code>'clearImmediate'</code>) will be mocked by default.</li> 1783</ul> 1784<p><strong>Note:</strong> When you enable mocking for a specific timer, its associated 1785clear function will also be implicitly mocked.</p> 1786<p>Example usage:</p> 1787 1788<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1789mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</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>); 1790mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setInterval'</span>]);</code><button class="copy-button">copy</button></pre> 1791<p>The above example enables mocking for the <code>setInterval</code> timer and 1792implicitly mocks the <code>clearInterval</code> function. Only the <code>setInterval</code> 1793and <code>clearInterval</code> functions from <a href="./timers.html">node:timers</a>, 1794<a href="./timers.html#timers-promises-api">node:timers/promises</a>, and 1795<code>globalThis</code> will be mocked.</p> 1796<p>Alternatively, if you call <code>mock.timers.enable()</code> without any parameters:</p> 1797<p>All timers (<code>'setInterval'</code>, <code>'clearInterval'</code>, <code>'setTimeout'</code>, and <code>'clearTimeout'</code>) 1798will be mocked. The <code>setInterval</code>, <code>clearInterval</code>, <code>setTimeout</code>, and <code>clearTimeout</code> 1799functions from <code>node:timers</code>, <code>node:timers/promises</code>, 1800and <code>globalThis</code> will be mocked.</p> 1801<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> 1802<div class="api_metadata"> 1803<span>Added in: v18.19.0</span> 1804</div> 1805<p>This function restores the default behavior of all mocks that were previously 1806created by this <code>MockTimers</code> instance and disassociates the mocks 1807from the <code>MockTracker</code> instance.</p> 1808<p><strong>Note:</strong> After each test completes, this function is called on 1809the test context's <code>MockTracker</code>.</p> 1810 1811<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1812mock.<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>); 1813mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>();</code><button class="copy-button">copy</button></pre> 1814<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> 1815<p>Calls <code>timers.reset()</code>.</p> 1816<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> 1817<div class="api_metadata"> 1818<span>Added in: v18.19.0</span> 1819</div> 1820<p>Advances time for all mocked timers.</p> 1821<ul> 1822<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, 1823to advance the timers.</li> 1824</ul> 1825<p><strong>Note:</strong> This diverges from how <code>setTimeout</code> in Node.js behaves and accepts 1826only positive numbers. In Node.js, <code>setTimeout</code> with negative numbers is 1827only supported for web compatibility reasons.</p> 1828<p>The following example mocks a <code>setTimeout</code> function and 1829by using <code>.tick</code> advances in 1830time triggering all pending timers.</p> 1831 1832<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1833<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 1834 1835<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> { 1836 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1837 1838 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1839 1840 <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 1841 1842 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>); 1843 1844 <span class="hljs-comment">// Advance in time</span> 1845 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>); 1846 1847 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>); 1848});</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>); 1849<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 1850 1851<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> { 1852 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1853 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1854 1855 <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 1856 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>); 1857 1858 <span class="hljs-comment">// Advance in time</span> 1859 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>); 1860 1861 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>); 1862});</code><button class="copy-button">copy</button></pre> 1863<p>Alternativelly, the <code>.tick</code> function can be called many times</p> 1864 1865<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1866<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 1867 1868<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> { 1869 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1870 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1871 <span class="hljs-keyword">const</span> nineSecs = <span class="hljs-number">9000</span>; 1872 <span class="hljs-built_in">setTimeout</span>(fn, nineSecs); 1873 1874 <span class="hljs-keyword">const</span> twoSeconds = <span class="hljs-number">3000</span>; 1875 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 1876 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 1877 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 1878 1879 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>); 1880});</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>); 1881<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 1882 1883<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> { 1884 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1885 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1886 <span class="hljs-keyword">const</span> nineSecs = <span class="hljs-number">9000</span>; 1887 <span class="hljs-built_in">setTimeout</span>(fn, nineSecs); 1888 1889 <span class="hljs-keyword">const</span> twoSeconds = <span class="hljs-number">3000</span>; 1890 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 1891 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 1892 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 1893 1894 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>); 1895});</code><button class="copy-button">copy</button></pre> 1896<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> 1897<p>As mentioned, all clear functions from timers (<code>clearTimeout</code> and <code>clearInterval</code>) 1898are implicity mocked. Take a look at this example using <code>setTimeout</code>:</p> 1899 1900<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1901<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 1902 1903<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> { 1904 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1905 1906 <span class="hljs-comment">// Optionally choose what to mock</span> 1907 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1908 <span class="hljs-keyword">const</span> id = <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 1909 1910 <span class="hljs-comment">// Implicity mocked as well</span> 1911 <span class="hljs-built_in">clearTimeout</span>(id); 1912 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>); 1913 1914 <span class="hljs-comment">// As that setTimeout was cleared the mock function will never be called</span> 1915 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>); 1916});</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>); 1917<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 1918 1919<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> { 1920 <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1921 1922 <span class="hljs-comment">// Optionally choose what to mock</span> 1923 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1924 <span class="hljs-keyword">const</span> id = <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 1925 1926 <span class="hljs-comment">// Implicity mocked as well</span> 1927 <span class="hljs-built_in">clearTimeout</span>(id); 1928 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>); 1929 1930 <span class="hljs-comment">// As that setTimeout was cleared the mock function will never be called</span> 1931 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>); 1932});</code><button class="copy-button">copy</button></pre> 1933<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> 1934<p>Once you enable mocking timers, <a href="./timers.html">node:timers</a>, 1935<a href="./timers.html#timers-promises-api">node:timers/promises</a> modules, 1936and timers from the Node.js global context are enabled:</p> 1937<p><strong>Note:</strong> Destructuring functions such as 1938<code>import { setTimeout } from 'node:timers'</code> is currently 1939not supported by this API.</p> 1940 1941<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1942<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 1943<span class="hljs-keyword">import</span> nodeTimers <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers'</span>; 1944<span class="hljs-keyword">import</span> nodeTimersPromises <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>; 1945 1946<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-keyword">async</span> (context) => { 1947 <span class="hljs-keyword">const</span> globalTimeoutObjectSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1948 <span class="hljs-keyword">const</span> nodeTimerSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1949 <span class="hljs-keyword">const</span> nodeTimerPromiseSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1950 1951 <span class="hljs-comment">// Optionally choose what to mock</span> 1952 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1953 <span class="hljs-built_in">setTimeout</span>(globalTimeoutObjectSpy, <span class="hljs-number">9999</span>); 1954 nodeTimers.<span class="hljs-built_in">setTimeout</span>(nodeTimerSpy, <span class="hljs-number">9999</span>); 1955 1956 <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); 1957 1958 <span class="hljs-comment">// Advance in time</span> 1959 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>); 1960 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>); 1961 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>); 1962 <span class="hljs-keyword">await</span> promise; 1963 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>); 1964});</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>); 1965<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 1966<span class="hljs-keyword">const</span> nodeTimers = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers'</span>); 1967<span class="hljs-keyword">const</span> nodeTimersPromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 1968 1969<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-keyword">async</span> (context) => { 1970 <span class="hljs-keyword">const</span> globalTimeoutObjectSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1971 <span class="hljs-keyword">const</span> nodeTimerSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1972 <span class="hljs-keyword">const</span> nodeTimerPromiseSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 1973 1974 <span class="hljs-comment">// Optionally choose what to mock</span> 1975 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 1976 <span class="hljs-built_in">setTimeout</span>(globalTimeoutObjectSpy, <span class="hljs-number">9999</span>); 1977 nodeTimers.<span class="hljs-built_in">setTimeout</span>(nodeTimerSpy, <span class="hljs-number">9999</span>); 1978 1979 <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); 1980 1981 <span class="hljs-comment">// Advance in time</span> 1982 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>); 1983 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>); 1984 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>); 1985 <span class="hljs-keyword">await</span> promise; 1986 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>); 1987});</code><button class="copy-button">copy</button></pre> 1988<p>In Node.js, <code>setInterval</code> from <a href="./timers.html#timers-promises-api">node:timers/promises</a> 1989is an <code>AsyncGenerator</code> and is also supported by this API:</p> 1990 1991<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 1992<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 1993<span class="hljs-keyword">import</span> nodeTimersPromises <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>; 1994<span class="hljs-title function_">test</span>(<span class="hljs-string">'should tick five times testing a real use case'</span>, <span class="hljs-keyword">async</span> (context) => { 1995 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setInterval'</span>]); 1996 1997 <span class="hljs-keyword">const</span> expectedIterations = <span class="hljs-number">3</span>; 1998 <span class="hljs-keyword">const</span> interval = <span class="hljs-number">1000</span>; 1999 <span class="hljs-keyword">const</span> startedAt = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 2000 <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { 2001 <span class="hljs-keyword">const</span> times = []; 2002 <span class="hljs-keyword">for</span> <span class="hljs-keyword">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)) { 2003 times.<span class="hljs-title function_">push</span>(time); 2004 <span class="hljs-keyword">if</span> (times.<span class="hljs-property">length</span> === expectedIterations) <span class="hljs-keyword">break</span>; 2005 } 2006 <span class="hljs-keyword">return</span> times; 2007 } 2008 2009 <span class="hljs-keyword">const</span> r = <span class="hljs-title function_">run</span>(); 2010 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 2011 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 2012 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 2013 2014 <span class="hljs-keyword">const</span> timeResults = <span class="hljs-keyword">await</span> r; 2015 assert.<span class="hljs-title function_">strictEqual</span>(timeResults.<span class="hljs-property">length</span>, expectedIterations); 2016 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> it = <span class="hljs-number">1</span>; it < expectedIterations; it++) { 2017 assert.<span class="hljs-title function_">strictEqual</span>(timeResults[it - <span class="hljs-number">1</span>], startedAt + (interval * it)); 2018 } 2019});</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>); 2020<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 2021<span class="hljs-keyword">const</span> nodeTimersPromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 2022<span class="hljs-title function_">test</span>(<span class="hljs-string">'should tick five times testing a real use case'</span>, <span class="hljs-keyword">async</span> (context) => { 2023 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setInterval'</span>]); 2024 2025 <span class="hljs-keyword">const</span> expectedIterations = <span class="hljs-number">3</span>; 2026 <span class="hljs-keyword">const</span> interval = <span class="hljs-number">1000</span>; 2027 <span class="hljs-keyword">const</span> startedAt = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 2028 <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { 2029 <span class="hljs-keyword">const</span> times = []; 2030 <span class="hljs-keyword">for</span> <span class="hljs-keyword">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)) { 2031 times.<span class="hljs-title function_">push</span>(time); 2032 <span class="hljs-keyword">if</span> (times.<span class="hljs-property">length</span> === expectedIterations) <span class="hljs-keyword">break</span>; 2033 } 2034 <span class="hljs-keyword">return</span> times; 2035 } 2036 2037 <span class="hljs-keyword">const</span> r = <span class="hljs-title function_">run</span>(); 2038 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 2039 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 2040 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 2041 2042 <span class="hljs-keyword">const</span> timeResults = <span class="hljs-keyword">await</span> r; 2043 assert.<span class="hljs-title function_">strictEqual</span>(timeResults.<span class="hljs-property">length</span>, expectedIterations); 2044 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> it = <span class="hljs-number">1</span>; it < expectedIterations; it++) { 2045 assert.<span class="hljs-title function_">strictEqual</span>(timeResults[it - <span class="hljs-number">1</span>], startedAt + (interval * it)); 2046 } 2047});</code><button class="copy-button">copy</button></pre> 2048<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> 2049<div class="api_metadata"> 2050<span>Added in: v18.19.0</span> 2051</div> 2052<p>Triggers all pending mocked timers immediately.</p> 2053<p>The example below triggers all pending timers immediately, 2054causing them to execute without any delay.</p> 2055 2056<pre class="with-38-chars"><input class="js-flavor-selector" 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>; 2057<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 2058 2059<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> { 2060 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 2061 <span class="hljs-keyword">const</span> results = []; 2062 <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>); 2063 2064 <span class="hljs-comment">// Notice that if both timers have the same timeout,</span> 2065 <span class="hljs-comment">// the order of execution is guaranteed</span> 2066 <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>); 2067 <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>); 2068 2069 assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []); 2070 2071 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>(); 2072 2073 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>]); 2074});</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>); 2075<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 2076 2077<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> { 2078 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 2079 <span class="hljs-keyword">const</span> results = []; 2080 <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>); 2081 2082 <span class="hljs-comment">// Notice that if both timers have the same timeout,</span> 2083 <span class="hljs-comment">// the order of execution is guaranteed</span> 2084 <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>); 2085 <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>); 2086 2087 assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []); 2088 2089 context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>(); 2090 2091 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>]); 2092});</code><button class="copy-button">copy</button></pre> 2093<p><strong>Note:</strong> The <code>runAll()</code> function is specifically designed for 2094triggering timers in the context of timer mocking. 2095It does not have any effect on real-time system 2096clocks or actual timers outside of the mocking environment.</p> 2097</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> 2098<div class="api_metadata"> 2099<details class="changelog"><summary>History</summary> 2100<table> 2101<tbody><tr><th>Version</th><th>Changes</th></tr> 2102<tr><td>v18.17.0</td> 2103<td><p>added type to test:pass and test:fail events for when the test is a suite.</p></td></tr> 2104<tr><td>v18.9.0, v16.19.0</td> 2105<td><p><span>Added in: v18.9.0, v16.19.0</span></p></td></tr> 2106</tbody></table> 2107</details> 2108</div> 2109<ul> 2110<li>Extends <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 2111</ul> 2112<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> 2113object, streaming a series of events representing the execution of the tests. 2114<code>TestsStream</code> will emit events, in the order of the tests definition</p> 2115<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> 2116<ul> 2117<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2118<ul> 2119<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. 2120<ul> 2121<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 2122report is an object with the following schema: 2123<ul> 2124<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> 2125<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> 2126<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> 2127<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> 2128<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> 2129<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> 2130<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> 2131<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> 2132<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> 2133<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> 2134<li><code>uncoveredLineNumbers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array of integers representing line 2135numbers that are uncovered.</li> 2136</ul> 2137</li> 2138<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 2139files. 2140<ul> 2141<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> 2142<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> 2143<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> 2144<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> 2145<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> 2146<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> 2147<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> 2148<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> 2149<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> 2150</ul> 2151</li> 2152<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 2153began. This is useful for displaying relative path names in case the tests 2154changed the working directory of the Node.js process.</li> 2155</ul> 2156</li> 2157<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> 2158</ul> 2159</li> 2160</ul> 2161<p>Emitted when code coverage is enabled and all tests have completed.</p> 2162<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> 2163<ul> 2164<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2165<ul> 2166<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 2167<code>undefined</code> if the test was run through the REPL.</li> 2168<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, 2169<code>undefined</code> if test was run through the REPL.</li> 2170<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 2171<code>undefined</code> if the test was run through the REPL.</li> 2172<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> 2173<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> 2174</ul> 2175</li> 2176</ul> 2177<p>Emitted when a test is dequeued, right before it is executed.</p> 2178<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> 2179<ul> 2180<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2181<ul> 2182<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 2183<code>undefined</code> if the test was run through the REPL.</li> 2184<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, 2185<code>undefined</code> if test was run through the REPL.</li> 2186<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 2187<code>undefined</code> if the test was run through the REPL.</li> 2188<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> 2189<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> 2190</ul> 2191</li> 2192</ul> 2193<p>Emitted when <a href="#contextdiagnosticmessage"><code>context.diagnostic</code></a> is called.</p> 2194<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> 2195<ul> 2196<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2197<ul> 2198<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 2199<code>undefined</code> if the test was run through the REPL.</li> 2200<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, 2201<code>undefined</code> if test was run through the REPL.</li> 2202<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 2203<code>undefined</code> if the test was run through the REPL.</li> 2204<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> 2205<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> 2206</ul> 2207</li> 2208</ul> 2209<p>Emitted when a test is enqueued for execution.</p> 2210<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> 2211<ul> 2212<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2213<ul> 2214<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 2215<code>undefined</code> if the test was run through the REPL.</li> 2216<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. 2217<ul> 2218<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> 2219<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. 2220<ul> 2221<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> 2222</ul> 2223</li> 2224<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 2225this is a suite.</li> 2226</ul> 2227</li> 2228<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, 2229<code>undefined</code> if test was run through the REPL.</li> 2230<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 2231<code>undefined</code> if the test was run through the REPL.</li> 2232<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> 2233<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> 2234<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> 2235<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> 2236<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> 2237</ul> 2238</li> 2239</ul> 2240<p>Emitted when a test fails.</p> 2241<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> 2242<ul> 2243<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2244<ul> 2245<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 2246<code>undefined</code> if the test was run through the REPL.</li> 2247<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. 2248<ul> 2249<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> 2250<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 2251this is a suite.</li> 2252</ul> 2253</li> 2254<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, 2255<code>undefined</code> if test was run through the REPL.</li> 2256<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 2257<code>undefined</code> if the test was run through the REPL.</li> 2258<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> 2259<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> 2260<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> 2261<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> 2262<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> 2263</ul> 2264</li> 2265</ul> 2266<p>Emitted when a test passes.</p> 2267<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> 2268<ul> 2269<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2270<ul> 2271<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 2272<code>undefined</code> if the test was run through the REPL.</li> 2273<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, 2274<code>undefined</code> if test was run through the REPL.</li> 2275<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 2276<code>undefined</code> if the test was run through the REPL.</li> 2277<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> 2278<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> 2279</ul> 2280</li> 2281</ul> 2282<p>Emitted when all subtests have completed for a given test.</p> 2283<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> 2284<ul> 2285<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2286<ul> 2287<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 2288<code>undefined</code> if the test was run through the REPL.</li> 2289<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, 2290<code>undefined</code> if test was run through the REPL.</li> 2291<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 2292<code>undefined</code> if the test was run through the REPL.</li> 2293<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> 2294<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> 2295</ul> 2296</li> 2297</ul> 2298<p>Emitted when a test starts reporting its own and its subtests status. 2299This event is guaranteed to be emitted in the same order as the tests are 2300defined.</p> 2301<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> 2302<ul> 2303<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2304<ul> 2305<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 2306<code>undefined</code> if the test was run through the REPL.</li> 2307<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> 2308<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 2309<code>undefined</code> if the test was run through the REPL.</li> 2310<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> 2311</ul> 2312</li> 2313</ul> 2314<p>Emitted when a running test writes to <code>stderr</code>. 2315This event is only emitted if <code>--test</code> flag is passed.</p> 2316<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> 2317<ul> 2318<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2319<ul> 2320<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 2321<code>undefined</code> if the test was run through the REPL.</li> 2322<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> 2323<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 2324<code>undefined</code> if the test was run through the REPL.</li> 2325<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> 2326</ul> 2327</li> 2328</ul> 2329<p>Emitted when a running test writes to <code>stdout</code>. 2330This event is only emitted if <code>--test</code> flag is passed.</p> 2331<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> 2332<p>Emitted when no more tests are queued for execution in watch mode.</p> 2333</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> 2334<div class="api_metadata"> 2335<details class="changelog"><summary>History</summary> 2336<table> 2337<tbody><tr><th>Version</th><th>Changes</th></tr> 2338<tr><td>v18.17.0</td> 2339<td><p>The <code>before</code> function was added to TestContext.</p></td></tr> 2340<tr><td>v18.0.0</td> 2341<td><p><span>Added in: v18.0.0</span></p></td></tr> 2342</tbody></table> 2343</details> 2344</div> 2345<p>An instance of <code>TestContext</code> is passed to each test function in order to 2346interact with the test runner. However, the <code>TestContext</code> constructor is not 2347exposed as part of the API.</p> 2348<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> 2349<div class="api_metadata"> 2350<span>Added in: v18.17.0</span> 2351</div> 2352<ul> 2353<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 2354to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 2355the callback function is passed as the second argument. <strong>Default:</strong> A no-op 2356function.</li> 2357<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 2358properties are supported: 2359<ul> 2360<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 2361<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. 2362If unspecified, subtests inherit this value from their parent. 2363<strong>Default:</strong> <code>Infinity</code>.</li> 2364</ul> 2365</li> 2366</ul> 2367<p>This function is used to create a hook running before 2368subtest of the current test.</p> 2369<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> 2370<div class="api_metadata"> 2371<span>Added in: v18.8.0</span> 2372</div> 2373<ul> 2374<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 2375to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 2376the callback function is passed as the second argument. <strong>Default:</strong> A no-op 2377function.</li> 2378<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 2379properties are supported: 2380<ul> 2381<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 2382<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. 2383If unspecified, subtests inherit this value from their parent. 2384<strong>Default:</strong> <code>Infinity</code>.</li> 2385</ul> 2386</li> 2387</ul> 2388<p>This function is used to create a hook running 2389before each subtest of the current test.</p> 2390<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 2391 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>)); 2392 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>( 2393 <span class="hljs-string">'This is a subtest'</span>, 2394 <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> { 2395 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 2396 }, 2397 ); 2398});</code> <button class="copy-button">copy</button></pre> 2399<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> 2400<div class="api_metadata"> 2401<span>Added in: v18.13.0</span> 2402</div> 2403<ul> 2404<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 2405to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 2406the callback function is passed as the second argument. <strong>Default:</strong> A no-op 2407function.</li> 2408<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 2409properties are supported: 2410<ul> 2411<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 2412<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. 2413If unspecified, subtests inherit this value from their parent. 2414<strong>Default:</strong> <code>Infinity</code>.</li> 2415</ul> 2416</li> 2417</ul> 2418<p>This function is used to create a hook that runs after the current test 2419finishes.</p> 2420<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 2421 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>)); 2422 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 2423});</code> <button class="copy-button">copy</button></pre> 2424<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> 2425<div class="api_metadata"> 2426<span>Added in: v18.8.0</span> 2427</div> 2428<ul> 2429<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 2430to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 2431the callback function is passed as the second argument. <strong>Default:</strong> A no-op 2432function.</li> 2433<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 2434properties are supported: 2435<ul> 2436<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 2437<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. 2438If unspecified, subtests inherit this value from their parent. 2439<strong>Default:</strong> <code>Infinity</code>.</li> 2440</ul> 2441</li> 2442</ul> 2443<p>This function is used to create a hook running 2444after each subtest of the current test.</p> 2445<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 2446 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>)); 2447 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>( 2448 <span class="hljs-string">'This is a subtest'</span>, 2449 <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> { 2450 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 2451 }, 2452 ); 2453});</code> <button class="copy-button">copy</button></pre> 2454<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> 2455<div class="api_metadata"> 2456<span>Added in: v18.0.0</span> 2457</div> 2458<ul> 2459<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> 2460</ul> 2461<p>This function is used to write diagnostics to the output. Any diagnostic 2462information is included at the end of the test's results. This function does 2463not return a value.</p> 2464<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> { 2465 t.<span class="hljs-title function_">diagnostic</span>(<span class="hljs-string">'A diagnostic message'</span>); 2466});</code> <button class="copy-button">copy</button></pre> 2467<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> 2468<div class="api_metadata"> 2469<span>Added in: v18.8.0</span> 2470</div> 2471<p>The name of the test.</p> 2472<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> 2473<div class="api_metadata"> 2474<span>Added in: v18.0.0</span> 2475</div> 2476<ul> 2477<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> 2478</ul> 2479<p>If <code>shouldRunOnlyTests</code> is truthy, the test context will only run tests that 2480have the <code>only</code> option set. Otherwise, all tests are run. If Node.js was not 2481started with the <a href="cli.html#--test-only"><code>--test-only</code></a> command-line option, this function is a 2482no-op.</p> 2483<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> { 2484 <span class="hljs-comment">// The test context can be set to run subtests with the 'only' option.</span> 2485 t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">true</span>); 2486 <span class="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">all</span>([ 2487 t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now skipped'</span>), 2488 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> }), 2489 ]); 2490});</code> <button class="copy-button">copy</button></pre> 2491<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> 2492<div class="api_metadata"> 2493<span>Added in: v18.7.0</span> 2494</div> 2495<ul> 2496<li><a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to abort test subtasks when the test has been 2497aborted.</li> 2498</ul> 2499<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 2500 <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> }); 2501});</code> <button class="copy-button">copy</button></pre> 2502<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> 2503<div class="api_metadata"> 2504<span>Added in: v18.0.0</span> 2505</div> 2506<ul> 2507<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> 2508</ul> 2509<p>This function causes the test's output to indicate the test as skipped. If 2510<code>message</code> is provided, it is included in the output. Calling <code>skip()</code> does 2511not terminate execution of the test function. This function does not return a 2512value.</p> 2513<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> { 2514 <span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span> 2515 t.<span class="hljs-title function_">skip</span>(<span class="hljs-string">'this is skipped'</span>); 2516});</code> <button class="copy-button">copy</button></pre> 2517<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> 2518<div class="api_metadata"> 2519<span>Added in: v18.0.0</span> 2520</div> 2521<ul> 2522<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> 2523</ul> 2524<p>This function adds a <code>TODO</code> directive to the test's output. If <code>message</code> is 2525provided, it is included in the output. Calling <code>todo()</code> does not terminate 2526execution of the test function. This function does not return a value.</p> 2527<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> { 2528 <span class="hljs-comment">// This test is marked as `TODO`</span> 2529 t.<span class="hljs-title function_">todo</span>(<span class="hljs-string">'this is a todo'</span>); 2530});</code> <button class="copy-button">copy</button></pre> 2531<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> 2532<div class="api_metadata"> 2533<details class="changelog"><summary>History</summary> 2534<table> 2535<tbody><tr><th>Version</th><th>Changes</th></tr> 2536<tr><td>v18.8.0</td> 2537<td><p>Add a <code>signal</code> option.</p></td></tr> 2538<tr><td>v18.7.0</td> 2539<td><p>Add a <code>timeout</code> option.</p></td></tr> 2540<tr><td>v18.0.0</td> 2541<td><p><span>Added in: v18.0.0</span></p></td></tr> 2542</tbody></table> 2543</details> 2544</div> 2545<ul> 2546<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 2547test results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if 2548<code>fn</code> does not have a name.</li> 2549<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 2550properties are supported: 2551<ul> 2552<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, 2553then that many tests would run in parallel within the application thread. 2554If <code>true</code>, it would run all subtests in parallel. 2555If <code>false</code>, it would only run one test at a time. 2556If unspecified, subtests inherit this value from their parent. 2557<strong>Default:</strong> <code>null</code>.</li> 2558<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 2559<code>only</code> tests, then this test will be run. Otherwise, the test is skipped. 2560<strong>Default:</strong> <code>false</code>.</li> 2561<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test.</li> 2562<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 2563provided, that string is displayed in the test results as the reason for 2564skipping the test. <strong>Default:</strong> <code>false</code>.</li> 2565<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 2566is provided, that string is displayed in the test results as the reason why 2567the test is <code>TODO</code>. <strong>Default:</strong> <code>false</code>.</li> 2568<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. 2569If unspecified, subtests inherit this value from their parent. 2570<strong>Default:</strong> <code>Infinity</code>.</li> 2571</ul> 2572</li> 2573<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 2574to this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the test uses callbacks, 2575the callback function is passed as the second argument. <strong>Default:</strong> A no-op 2576function.</li> 2577<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Resolved with <code>undefined</code> once the test completes.</li> 2578</ul> 2579<p>This function is used to create subtests under the current test. This function 2580behaves in the same fashion as the top level <a href="#testname-options-fn"><code>test()</code></a> function.</p> 2581<pre><code class="language-js"><span class="hljs-title function_">test</span>(<span class="hljs-string">'top level test'</span>, <span class="hljs-keyword">async</span> (t) => { 2582 <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>( 2583 <span class="hljs-string">'This is a subtest'</span>, 2584 { <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> }, 2585 <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> { 2586 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 2587 }, 2588 ); 2589});</code> <button class="copy-button">copy</button></pre> 2590</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> 2591<div class="api_metadata"> 2592<span>Added in: v18.7.0</span> 2593</div> 2594<p>An instance of <code>SuiteContext</code> is passed to each suite function in order to 2595interact with the test runner. However, the <code>SuiteContext</code> constructor is not 2596exposed as part of the API.</p> 2597<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> 2598<div class="api_metadata"> 2599<span>Added in: v18.8.0</span> 2600</div> 2601<p>The name of the suite.</p> 2602<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> 2603<div class="api_metadata"> 2604<span>Added in: v18.7.0</span> 2605</div> 2606<ul> 2607<li><a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to abort test subtasks when the test has been 2608aborted.</li> 2609</ul></section> 2610 <!-- API END --> 2611 </div> 2612 </div> 2613 </div> 2614</body> 2615</html> 2616