11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Test runner | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/test.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <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> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-test"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test active">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="test" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#test-runner">Test runner</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#subtests">Subtests</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#skipping-tests">Skipping tests</a></li> 1271cb0ef41Sopenharmony_ci<li><a href="#describeit-syntax"><code>describe</code>/<code>it</code> syntax</a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#only-tests"><code>only</code> tests</a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#filtering-tests-by-name">Filtering tests by name</a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#extraneous-asynchronous-activity">Extraneous asynchronous activity</a></li> 1311cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#watch-mode">Watch mode</a></span></li> 1321cb0ef41Sopenharmony_ci<li><a href="#running-tests-from-the-command-line">Running tests from the command line</a> 1331cb0ef41Sopenharmony_ci<ul> 1341cb0ef41Sopenharmony_ci<li><a href="#test-runner-execution-model">Test runner execution model</a></li> 1351cb0ef41Sopenharmony_ci</ul> 1361cb0ef41Sopenharmony_ci</li> 1371cb0ef41Sopenharmony_ci<li><a href="#collecting-code-coverage">Collecting code coverage</a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#mocking">Mocking</a> 1391cb0ef41Sopenharmony_ci<ul> 1401cb0ef41Sopenharmony_ci<li><a href="#timers">Timers</a></li> 1411cb0ef41Sopenharmony_ci</ul> 1421cb0ef41Sopenharmony_ci</li> 1431cb0ef41Sopenharmony_ci<li><a href="#test-reporters">Test reporters</a> 1441cb0ef41Sopenharmony_ci<ul> 1451cb0ef41Sopenharmony_ci<li><a href="#custom-reporters">Custom reporters</a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#multiple-reporters">Multiple reporters</a></li> 1471cb0ef41Sopenharmony_ci</ul> 1481cb0ef41Sopenharmony_ci</li> 1491cb0ef41Sopenharmony_ci<li><a href="#runoptions"><code>run([options])</code></a></li> 1501cb0ef41Sopenharmony_ci<li><a href="#testname-options-fn"><code>test([name][, options][, fn])</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#testskipname-options-fn"><code>test.skip([name][, options][, fn])</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#testtodoname-options-fn"><code>test.todo([name][, options][, fn])</code></a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#testonlyname-options-fn"><code>test.only([name][, options][, fn])</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#describename-options-fn"><code>describe([name][, options][, fn])</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#describeskipname-options-fn"><code>describe.skip([name][, options][, fn])</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#describetodoname-options-fn"><code>describe.todo([name][, options][, fn])</code></a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#describeonlyname-options-fn"><code>describe.only([name][, options][, fn])</code></a></li> 1581cb0ef41Sopenharmony_ci<li><a href="#itname-options-fn"><code>it([name][, options][, fn])</code></a></li> 1591cb0ef41Sopenharmony_ci<li><a href="#itskipname-options-fn"><code>it.skip([name][, options][, fn])</code></a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#ittodoname-options-fn"><code>it.todo([name][, options][, fn])</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#itonlyname-options-fn"><code>it.only([name][, options][, fn])</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#beforefn-options"><code>before([fn][, options])</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#afterfn-options"><code>after([fn][, options])</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#beforeeachfn-options"><code>beforeEach([fn][, options])</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#aftereachfn-options"><code>afterEach([fn][, options])</code></a></li> 1661cb0ef41Sopenharmony_ci<li><a href="#class-mockfunctioncontext">Class: <code>MockFunctionContext</code></a> 1671cb0ef41Sopenharmony_ci<ul> 1681cb0ef41Sopenharmony_ci<li><a href="#ctxcalls"><code>ctx.calls</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#ctxcallcount"><code>ctx.callCount()</code></a></li> 1701cb0ef41Sopenharmony_ci<li><a href="#ctxmockimplementationimplementation"><code>ctx.mockImplementation(implementation)</code></a></li> 1711cb0ef41Sopenharmony_ci<li><a href="#ctxmockimplementationonceimplementation-oncall"><code>ctx.mockImplementationOnce(implementation[, onCall])</code></a></li> 1721cb0ef41Sopenharmony_ci<li><a href="#ctxresetcalls"><code>ctx.resetCalls()</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#ctxrestore"><code>ctx.restore()</code></a></li> 1741cb0ef41Sopenharmony_ci</ul> 1751cb0ef41Sopenharmony_ci</li> 1761cb0ef41Sopenharmony_ci<li><a href="#class-mocktracker">Class: <code>MockTracker</code></a> 1771cb0ef41Sopenharmony_ci<ul> 1781cb0ef41Sopenharmony_ci<li><a href="#mockfnoriginal-implementation-options"><code>mock.fn([original[, implementation]][, options])</code></a></li> 1791cb0ef41Sopenharmony_ci<li><a href="#mockgetterobject-methodname-implementation-options"><code>mock.getter(object, methodName[, implementation][, options])</code></a></li> 1801cb0ef41Sopenharmony_ci<li><a href="#mockmethodobject-methodname-implementation-options"><code>mock.method(object, methodName[, implementation][, options])</code></a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#mockreset"><code>mock.reset()</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#mockrestoreall"><code>mock.restoreAll()</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#mocksetterobject-methodname-implementation-options"><code>mock.setter(object, methodName[, implementation][, options])</code></a></li> 1841cb0ef41Sopenharmony_ci</ul> 1851cb0ef41Sopenharmony_ci</li> 1861cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-mocktimers">Class: <code>MockTimers</code></a></span> 1871cb0ef41Sopenharmony_ci<ul> 1881cb0ef41Sopenharmony_ci<li><a href="#timersenabletimers"><code>timers.enable([timers])</code></a></li> 1891cb0ef41Sopenharmony_ci<li><a href="#timersreset"><code>timers.reset()</code></a></li> 1901cb0ef41Sopenharmony_ci<li><a href="#timerssymboldispose"><code>timers[Symbol.dispose]()</code></a></li> 1911cb0ef41Sopenharmony_ci<li><a href="#timerstickmilliseconds"><code>timers.tick(milliseconds)</code></a> 1921cb0ef41Sopenharmony_ci<ul> 1931cb0ef41Sopenharmony_ci<li><a href="#using-clear-functions">Using clear functions</a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#working-with-nodejs-timers-modules">Working with Node.js timers modules</a></li> 1951cb0ef41Sopenharmony_ci</ul> 1961cb0ef41Sopenharmony_ci</li> 1971cb0ef41Sopenharmony_ci<li><a href="#timersrunall"><code>timers.runAll()</code></a></li> 1981cb0ef41Sopenharmony_ci</ul> 1991cb0ef41Sopenharmony_ci</li> 2001cb0ef41Sopenharmony_ci<li><a href="#class-testsstream">Class: <code>TestsStream</code></a> 2011cb0ef41Sopenharmony_ci<ul> 2021cb0ef41Sopenharmony_ci<li><a href="#event-testcoverage">Event: <code>'test:coverage'</code></a></li> 2031cb0ef41Sopenharmony_ci<li><a href="#event-testdequeue">Event: <code>'test:dequeue'</code></a></li> 2041cb0ef41Sopenharmony_ci<li><a href="#event-testdiagnostic">Event: <code>'test:diagnostic'</code></a></li> 2051cb0ef41Sopenharmony_ci<li><a href="#event-testenqueue">Event: <code>'test:enqueue'</code></a></li> 2061cb0ef41Sopenharmony_ci<li><a href="#event-testfail">Event: <code>'test:fail'</code></a></li> 2071cb0ef41Sopenharmony_ci<li><a href="#event-testpass">Event: <code>'test:pass'</code></a></li> 2081cb0ef41Sopenharmony_ci<li><a href="#event-testplan">Event: <code>'test:plan'</code></a></li> 2091cb0ef41Sopenharmony_ci<li><a href="#event-teststart">Event: <code>'test:start'</code></a></li> 2101cb0ef41Sopenharmony_ci<li><a href="#event-teststderr">Event: <code>'test:stderr'</code></a></li> 2111cb0ef41Sopenharmony_ci<li><a href="#event-teststdout">Event: <code>'test:stdout'</code></a></li> 2121cb0ef41Sopenharmony_ci<li><a href="#event-testwatchdrained">Event: <code>'test:watch:drained'</code></a></li> 2131cb0ef41Sopenharmony_ci</ul> 2141cb0ef41Sopenharmony_ci</li> 2151cb0ef41Sopenharmony_ci<li><a href="#class-testcontext">Class: <code>TestContext</code></a> 2161cb0ef41Sopenharmony_ci<ul> 2171cb0ef41Sopenharmony_ci<li><a href="#contextbeforefn-options"><code>context.before([fn][, options])</code></a></li> 2181cb0ef41Sopenharmony_ci<li><a href="#contextbeforeeachfn-options"><code>context.beforeEach([fn][, options])</code></a></li> 2191cb0ef41Sopenharmony_ci<li><a href="#contextafterfn-options"><code>context.after([fn][, options])</code></a></li> 2201cb0ef41Sopenharmony_ci<li><a href="#contextaftereachfn-options"><code>context.afterEach([fn][, options])</code></a></li> 2211cb0ef41Sopenharmony_ci<li><a href="#contextdiagnosticmessage"><code>context.diagnostic(message)</code></a></li> 2221cb0ef41Sopenharmony_ci<li><a href="#contextname"><code>context.name</code></a></li> 2231cb0ef41Sopenharmony_ci<li><a href="#contextrunonlyshouldrunonlytests"><code>context.runOnly(shouldRunOnlyTests)</code></a></li> 2241cb0ef41Sopenharmony_ci<li><a href="#contextsignal"><code>context.signal</code></a></li> 2251cb0ef41Sopenharmony_ci<li><a href="#contextskipmessage"><code>context.skip([message])</code></a></li> 2261cb0ef41Sopenharmony_ci<li><a href="#contexttodomessage"><code>context.todo([message])</code></a></li> 2271cb0ef41Sopenharmony_ci<li><a href="#contexttestname-options-fn"><code>context.test([name][, options][, fn])</code></a></li> 2281cb0ef41Sopenharmony_ci</ul> 2291cb0ef41Sopenharmony_ci</li> 2301cb0ef41Sopenharmony_ci<li><a href="#class-suitecontext">Class: <code>SuiteContext</code></a> 2311cb0ef41Sopenharmony_ci<ul> 2321cb0ef41Sopenharmony_ci<li><a href="#contextname_1"><code>context.name</code></a></li> 2331cb0ef41Sopenharmony_ci<li><a href="#contextsignal_1"><code>context.signal</code></a></li> 2341cb0ef41Sopenharmony_ci</ul> 2351cb0ef41Sopenharmony_ci</li> 2361cb0ef41Sopenharmony_ci</ul> 2371cb0ef41Sopenharmony_ci</li> 2381cb0ef41Sopenharmony_ci</ul></div></div> 2391cb0ef41Sopenharmony_ci </li> 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci <li class="picker-header"> 2431cb0ef41Sopenharmony_ci <a href="#"> 2441cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2451cb0ef41Sopenharmony_ci Index 2461cb0ef41Sopenharmony_ci </a> 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci <div class="picker"><ul> 2491cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2501cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci <li> 2531cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2541cb0ef41Sopenharmony_ci </li> 2551cb0ef41Sopenharmony_ci </ul> 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci<hr class="line"> 2581cb0ef41Sopenharmony_ci<ul> 2591cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2791cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2801cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2811cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2821cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2831cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2841cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2851cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2861cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2871cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2881cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2891cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2901cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2911cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2921cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2941cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 3001cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 3011cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 3041cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test active">Test runner</a></li> 3051cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 3061cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 3071cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 3081cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 3091cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 3101cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 3111cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 3121cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 3131cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 3151cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 3161cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 3171cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 3181cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 3191cb0ef41Sopenharmony_ci</ul> 3201cb0ef41Sopenharmony_ci<hr class="line"> 3211cb0ef41Sopenharmony_ci<ul> 3221cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 3231cb0ef41Sopenharmony_ci</ul></div> 3241cb0ef41Sopenharmony_ci </li> 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci <li class="picker-header"> 3281cb0ef41Sopenharmony_ci <a href="#"> 3291cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3301cb0ef41Sopenharmony_ci Other versions 3311cb0ef41Sopenharmony_ci </a> 3321cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/test.html">21.x</a></li> 3331cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/test.html">20.x <b>LTS</b></a></li> 3341cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/test.html">19.x</a></li> 3351cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/test.html">18.x <b>LTS</b></a></li></ol></div> 3361cb0ef41Sopenharmony_ci </li> 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ci <li class="picker-header"> 3391cb0ef41Sopenharmony_ci <a href="#"> 3401cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3411cb0ef41Sopenharmony_ci Options 3421cb0ef41Sopenharmony_ci </a> 3431cb0ef41Sopenharmony_ci 3441cb0ef41Sopenharmony_ci <div class="picker"> 3451cb0ef41Sopenharmony_ci <ul> 3461cb0ef41Sopenharmony_ci <li> 3471cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3481cb0ef41Sopenharmony_ci </li> 3491cb0ef41Sopenharmony_ci <li> 3501cb0ef41Sopenharmony_ci <a href="test.json">View as JSON</a> 3511cb0ef41Sopenharmony_ci </li> 3521cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/test.md">Edit on GitHub</a></li> 3531cb0ef41Sopenharmony_ci </ul> 3541cb0ef41Sopenharmony_ci </div> 3551cb0ef41Sopenharmony_ci </li> 3561cb0ef41Sopenharmony_ci </ul> 3571cb0ef41Sopenharmony_ci </div> 3581cb0ef41Sopenharmony_ci <hr> 3591cb0ef41Sopenharmony_ci </header> 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3621cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#test-runner">Test runner</a></span> 3631cb0ef41Sopenharmony_ci<ul> 3641cb0ef41Sopenharmony_ci<li><a href="#subtests">Subtests</a></li> 3651cb0ef41Sopenharmony_ci<li><a href="#skipping-tests">Skipping tests</a></li> 3661cb0ef41Sopenharmony_ci<li><a href="#describeit-syntax"><code>describe</code>/<code>it</code> syntax</a></li> 3671cb0ef41Sopenharmony_ci<li><a href="#only-tests"><code>only</code> tests</a></li> 3681cb0ef41Sopenharmony_ci<li><a href="#filtering-tests-by-name">Filtering tests by name</a></li> 3691cb0ef41Sopenharmony_ci<li><a href="#extraneous-asynchronous-activity">Extraneous asynchronous activity</a></li> 3701cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#watch-mode">Watch mode</a></span></li> 3711cb0ef41Sopenharmony_ci<li><a href="#running-tests-from-the-command-line">Running tests from the command line</a> 3721cb0ef41Sopenharmony_ci<ul> 3731cb0ef41Sopenharmony_ci<li><a href="#test-runner-execution-model">Test runner execution model</a></li> 3741cb0ef41Sopenharmony_ci</ul> 3751cb0ef41Sopenharmony_ci</li> 3761cb0ef41Sopenharmony_ci<li><a href="#collecting-code-coverage">Collecting code coverage</a></li> 3771cb0ef41Sopenharmony_ci<li><a href="#mocking">Mocking</a> 3781cb0ef41Sopenharmony_ci<ul> 3791cb0ef41Sopenharmony_ci<li><a href="#timers">Timers</a></li> 3801cb0ef41Sopenharmony_ci</ul> 3811cb0ef41Sopenharmony_ci</li> 3821cb0ef41Sopenharmony_ci<li><a href="#test-reporters">Test reporters</a> 3831cb0ef41Sopenharmony_ci<ul> 3841cb0ef41Sopenharmony_ci<li><a href="#custom-reporters">Custom reporters</a></li> 3851cb0ef41Sopenharmony_ci<li><a href="#multiple-reporters">Multiple reporters</a></li> 3861cb0ef41Sopenharmony_ci</ul> 3871cb0ef41Sopenharmony_ci</li> 3881cb0ef41Sopenharmony_ci<li><a href="#runoptions"><code>run([options])</code></a></li> 3891cb0ef41Sopenharmony_ci<li><a href="#testname-options-fn"><code>test([name][, options][, fn])</code></a></li> 3901cb0ef41Sopenharmony_ci<li><a href="#testskipname-options-fn"><code>test.skip([name][, options][, fn])</code></a></li> 3911cb0ef41Sopenharmony_ci<li><a href="#testtodoname-options-fn"><code>test.todo([name][, options][, fn])</code></a></li> 3921cb0ef41Sopenharmony_ci<li><a href="#testonlyname-options-fn"><code>test.only([name][, options][, fn])</code></a></li> 3931cb0ef41Sopenharmony_ci<li><a href="#describename-options-fn"><code>describe([name][, options][, fn])</code></a></li> 3941cb0ef41Sopenharmony_ci<li><a href="#describeskipname-options-fn"><code>describe.skip([name][, options][, fn])</code></a></li> 3951cb0ef41Sopenharmony_ci<li><a href="#describetodoname-options-fn"><code>describe.todo([name][, options][, fn])</code></a></li> 3961cb0ef41Sopenharmony_ci<li><a href="#describeonlyname-options-fn"><code>describe.only([name][, options][, fn])</code></a></li> 3971cb0ef41Sopenharmony_ci<li><a href="#itname-options-fn"><code>it([name][, options][, fn])</code></a></li> 3981cb0ef41Sopenharmony_ci<li><a href="#itskipname-options-fn"><code>it.skip([name][, options][, fn])</code></a></li> 3991cb0ef41Sopenharmony_ci<li><a href="#ittodoname-options-fn"><code>it.todo([name][, options][, fn])</code></a></li> 4001cb0ef41Sopenharmony_ci<li><a href="#itonlyname-options-fn"><code>it.only([name][, options][, fn])</code></a></li> 4011cb0ef41Sopenharmony_ci<li><a href="#beforefn-options"><code>before([fn][, options])</code></a></li> 4021cb0ef41Sopenharmony_ci<li><a href="#afterfn-options"><code>after([fn][, options])</code></a></li> 4031cb0ef41Sopenharmony_ci<li><a href="#beforeeachfn-options"><code>beforeEach([fn][, options])</code></a></li> 4041cb0ef41Sopenharmony_ci<li><a href="#aftereachfn-options"><code>afterEach([fn][, options])</code></a></li> 4051cb0ef41Sopenharmony_ci<li><a href="#class-mockfunctioncontext">Class: <code>MockFunctionContext</code></a> 4061cb0ef41Sopenharmony_ci<ul> 4071cb0ef41Sopenharmony_ci<li><a href="#ctxcalls"><code>ctx.calls</code></a></li> 4081cb0ef41Sopenharmony_ci<li><a href="#ctxcallcount"><code>ctx.callCount()</code></a></li> 4091cb0ef41Sopenharmony_ci<li><a href="#ctxmockimplementationimplementation"><code>ctx.mockImplementation(implementation)</code></a></li> 4101cb0ef41Sopenharmony_ci<li><a href="#ctxmockimplementationonceimplementation-oncall"><code>ctx.mockImplementationOnce(implementation[, onCall])</code></a></li> 4111cb0ef41Sopenharmony_ci<li><a href="#ctxresetcalls"><code>ctx.resetCalls()</code></a></li> 4121cb0ef41Sopenharmony_ci<li><a href="#ctxrestore"><code>ctx.restore()</code></a></li> 4131cb0ef41Sopenharmony_ci</ul> 4141cb0ef41Sopenharmony_ci</li> 4151cb0ef41Sopenharmony_ci<li><a href="#class-mocktracker">Class: <code>MockTracker</code></a> 4161cb0ef41Sopenharmony_ci<ul> 4171cb0ef41Sopenharmony_ci<li><a href="#mockfnoriginal-implementation-options"><code>mock.fn([original[, implementation]][, options])</code></a></li> 4181cb0ef41Sopenharmony_ci<li><a href="#mockgetterobject-methodname-implementation-options"><code>mock.getter(object, methodName[, implementation][, options])</code></a></li> 4191cb0ef41Sopenharmony_ci<li><a href="#mockmethodobject-methodname-implementation-options"><code>mock.method(object, methodName[, implementation][, options])</code></a></li> 4201cb0ef41Sopenharmony_ci<li><a href="#mockreset"><code>mock.reset()</code></a></li> 4211cb0ef41Sopenharmony_ci<li><a href="#mockrestoreall"><code>mock.restoreAll()</code></a></li> 4221cb0ef41Sopenharmony_ci<li><a href="#mocksetterobject-methodname-implementation-options"><code>mock.setter(object, methodName[, implementation][, options])</code></a></li> 4231cb0ef41Sopenharmony_ci</ul> 4241cb0ef41Sopenharmony_ci</li> 4251cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-mocktimers">Class: <code>MockTimers</code></a></span> 4261cb0ef41Sopenharmony_ci<ul> 4271cb0ef41Sopenharmony_ci<li><a href="#timersenabletimers"><code>timers.enable([timers])</code></a></li> 4281cb0ef41Sopenharmony_ci<li><a href="#timersreset"><code>timers.reset()</code></a></li> 4291cb0ef41Sopenharmony_ci<li><a href="#timerssymboldispose"><code>timers[Symbol.dispose]()</code></a></li> 4301cb0ef41Sopenharmony_ci<li><a href="#timerstickmilliseconds"><code>timers.tick(milliseconds)</code></a> 4311cb0ef41Sopenharmony_ci<ul> 4321cb0ef41Sopenharmony_ci<li><a href="#using-clear-functions">Using clear functions</a></li> 4331cb0ef41Sopenharmony_ci<li><a href="#working-with-nodejs-timers-modules">Working with Node.js timers modules</a></li> 4341cb0ef41Sopenharmony_ci</ul> 4351cb0ef41Sopenharmony_ci</li> 4361cb0ef41Sopenharmony_ci<li><a href="#timersrunall"><code>timers.runAll()</code></a></li> 4371cb0ef41Sopenharmony_ci</ul> 4381cb0ef41Sopenharmony_ci</li> 4391cb0ef41Sopenharmony_ci<li><a href="#class-testsstream">Class: <code>TestsStream</code></a> 4401cb0ef41Sopenharmony_ci<ul> 4411cb0ef41Sopenharmony_ci<li><a href="#event-testcoverage">Event: <code>'test:coverage'</code></a></li> 4421cb0ef41Sopenharmony_ci<li><a href="#event-testdequeue">Event: <code>'test:dequeue'</code></a></li> 4431cb0ef41Sopenharmony_ci<li><a href="#event-testdiagnostic">Event: <code>'test:diagnostic'</code></a></li> 4441cb0ef41Sopenharmony_ci<li><a href="#event-testenqueue">Event: <code>'test:enqueue'</code></a></li> 4451cb0ef41Sopenharmony_ci<li><a href="#event-testfail">Event: <code>'test:fail'</code></a></li> 4461cb0ef41Sopenharmony_ci<li><a href="#event-testpass">Event: <code>'test:pass'</code></a></li> 4471cb0ef41Sopenharmony_ci<li><a href="#event-testplan">Event: <code>'test:plan'</code></a></li> 4481cb0ef41Sopenharmony_ci<li><a href="#event-teststart">Event: <code>'test:start'</code></a></li> 4491cb0ef41Sopenharmony_ci<li><a href="#event-teststderr">Event: <code>'test:stderr'</code></a></li> 4501cb0ef41Sopenharmony_ci<li><a href="#event-teststdout">Event: <code>'test:stdout'</code></a></li> 4511cb0ef41Sopenharmony_ci<li><a href="#event-testwatchdrained">Event: <code>'test:watch:drained'</code></a></li> 4521cb0ef41Sopenharmony_ci</ul> 4531cb0ef41Sopenharmony_ci</li> 4541cb0ef41Sopenharmony_ci<li><a href="#class-testcontext">Class: <code>TestContext</code></a> 4551cb0ef41Sopenharmony_ci<ul> 4561cb0ef41Sopenharmony_ci<li><a href="#contextbeforefn-options"><code>context.before([fn][, options])</code></a></li> 4571cb0ef41Sopenharmony_ci<li><a href="#contextbeforeeachfn-options"><code>context.beforeEach([fn][, options])</code></a></li> 4581cb0ef41Sopenharmony_ci<li><a href="#contextafterfn-options"><code>context.after([fn][, options])</code></a></li> 4591cb0ef41Sopenharmony_ci<li><a href="#contextaftereachfn-options"><code>context.afterEach([fn][, options])</code></a></li> 4601cb0ef41Sopenharmony_ci<li><a href="#contextdiagnosticmessage"><code>context.diagnostic(message)</code></a></li> 4611cb0ef41Sopenharmony_ci<li><a href="#contextname"><code>context.name</code></a></li> 4621cb0ef41Sopenharmony_ci<li><a href="#contextrunonlyshouldrunonlytests"><code>context.runOnly(shouldRunOnlyTests)</code></a></li> 4631cb0ef41Sopenharmony_ci<li><a href="#contextsignal"><code>context.signal</code></a></li> 4641cb0ef41Sopenharmony_ci<li><a href="#contextskipmessage"><code>context.skip([message])</code></a></li> 4651cb0ef41Sopenharmony_ci<li><a href="#contexttodomessage"><code>context.todo([message])</code></a></li> 4661cb0ef41Sopenharmony_ci<li><a href="#contexttestname-options-fn"><code>context.test([name][, options][, fn])</code></a></li> 4671cb0ef41Sopenharmony_ci</ul> 4681cb0ef41Sopenharmony_ci</li> 4691cb0ef41Sopenharmony_ci<li><a href="#class-suitecontext">Class: <code>SuiteContext</code></a> 4701cb0ef41Sopenharmony_ci<ul> 4711cb0ef41Sopenharmony_ci<li><a href="#contextname_1"><code>context.name</code></a></li> 4721cb0ef41Sopenharmony_ci<li><a href="#contextsignal_1"><code>context.signal</code></a></li> 4731cb0ef41Sopenharmony_ci</ul> 4741cb0ef41Sopenharmony_ci</li> 4751cb0ef41Sopenharmony_ci</ul> 4761cb0ef41Sopenharmony_ci</li> 4771cb0ef41Sopenharmony_ci</ul></details> 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci <div id="apicontent"> 4801cb0ef41Sopenharmony_ci <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> 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ci<div class="api_metadata"> 4831cb0ef41Sopenharmony_ci<span>Added in: v18.0.0, v16.17.0</span> 4841cb0ef41Sopenharmony_ci</div> 4851cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 4861cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/test.js">lib/test.js</a></p> 4871cb0ef41Sopenharmony_ci<p>The <code>node:test</code> module facilitates the creation of JavaScript tests. 4881cb0ef41Sopenharmony_ciTo access it:</p> 4891cb0ef41Sopenharmony_ci 4901cb0ef41Sopenharmony_ci<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> 4911cb0ef41Sopenharmony_ci<p>This module is only available under the <code>node:</code> scheme. The following will not 4921cb0ef41Sopenharmony_ciwork:</p> 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci<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> 4951cb0ef41Sopenharmony_ci<p>Tests created via the <code>test</code> module consist of a single function that is 4961cb0ef41Sopenharmony_ciprocessed in one of three ways:</p> 4971cb0ef41Sopenharmony_ci<ol> 4981cb0ef41Sopenharmony_ci<li>A synchronous function that is considered failing if it throws an exception, 4991cb0ef41Sopenharmony_ciand is considered passing otherwise.</li> 5001cb0ef41Sopenharmony_ci<li>A function that returns a <code>Promise</code> that is considered failing if the 5011cb0ef41Sopenharmony_ci<code>Promise</code> rejects, and is considered passing if the <code>Promise</code> resolves.</li> 5021cb0ef41Sopenharmony_ci<li>A function that receives a callback function. If the callback receives any 5031cb0ef41Sopenharmony_citruthy value as its first argument, the test is considered failing. If a 5041cb0ef41Sopenharmony_cifalsy value is passed as the first argument to the callback, the test is 5051cb0ef41Sopenharmony_ciconsidered passing. If the test function receives a callback function and 5061cb0ef41Sopenharmony_cialso returns a <code>Promise</code>, the test will fail.</li> 5071cb0ef41Sopenharmony_ci</ol> 5081cb0ef41Sopenharmony_ci<p>The following example illustrates how tests are written using the 5091cb0ef41Sopenharmony_ci<code>test</code> module.</p> 5101cb0ef41Sopenharmony_ci<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> { 5111cb0ef41Sopenharmony_ci <span class="hljs-comment">// This test passes because it does not throw an exception.</span> 5121cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 5131cb0ef41Sopenharmony_ci}); 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci<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> { 5161cb0ef41Sopenharmony_ci <span class="hljs-comment">// This test fails because it throws an exception.</span> 5171cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 5181cb0ef41Sopenharmony_ci}); 5191cb0ef41Sopenharmony_ci 5201cb0ef41Sopenharmony_ci<span class="hljs-title function_">test</span>(<span class="hljs-string">'asynchronous passing test'</span>, <span class="hljs-keyword">async</span> (t) => { 5211cb0ef41Sopenharmony_ci <span class="hljs-comment">// This test passes because the Promise returned by the async</span> 5221cb0ef41Sopenharmony_ci <span class="hljs-comment">// function is not rejected.</span> 5231cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 5241cb0ef41Sopenharmony_ci}); 5251cb0ef41Sopenharmony_ci 5261cb0ef41Sopenharmony_ci<span class="hljs-title function_">test</span>(<span class="hljs-string">'asynchronous failing test'</span>, <span class="hljs-keyword">async</span> (t) => { 5271cb0ef41Sopenharmony_ci <span class="hljs-comment">// This test fails because the Promise returned by the async</span> 5281cb0ef41Sopenharmony_ci <span class="hljs-comment">// function is rejected.</span> 5291cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 5301cb0ef41Sopenharmony_ci}); 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_ci<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> { 5331cb0ef41Sopenharmony_ci <span class="hljs-comment">// Promises can be used directly as well.</span> 5341cb0ef41Sopenharmony_ci <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> { 5351cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 5361cb0ef41Sopenharmony_ci <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>)); 5371cb0ef41Sopenharmony_ci }); 5381cb0ef41Sopenharmony_ci }); 5391cb0ef41Sopenharmony_ci}); 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ci<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> { 5421cb0ef41Sopenharmony_ci <span class="hljs-comment">// done() is the callback function. When the setImmediate() runs, it invokes</span> 5431cb0ef41Sopenharmony_ci <span class="hljs-comment">// done() with no arguments.</span> 5441cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(done); 5451cb0ef41Sopenharmony_ci}); 5461cb0ef41Sopenharmony_ci 5471cb0ef41Sopenharmony_ci<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> { 5481cb0ef41Sopenharmony_ci <span class="hljs-comment">// When the setImmediate() runs, done() is invoked with an Error object and</span> 5491cb0ef41Sopenharmony_ci <span class="hljs-comment">// the test fails.</span> 5501cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 5511cb0ef41Sopenharmony_ci <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>)); 5521cb0ef41Sopenharmony_ci }); 5531cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5541cb0ef41Sopenharmony_ci<p>If any tests fail, the process exit code is set to <code>1</code>.</p> 5551cb0ef41Sopenharmony_ci<section><h3>Subtests<span><a class="mark" href="#subtests" id="subtests">#</a></span><a aria-hidden="true" class="legacy" id="test_subtests"></a></h3> 5561cb0ef41Sopenharmony_ci<p>The test context's <code>test()</code> method allows subtests to be created. This method 5571cb0ef41Sopenharmony_cibehaves identically to the top level <code>test()</code> function. The following example 5581cb0ef41Sopenharmony_cidemonstrates the creation of a top level test with two subtests.</p> 5591cb0ef41Sopenharmony_ci<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) => { 5601cb0ef41Sopenharmony_ci <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> { 5611cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 5621cb0ef41Sopenharmony_ci }); 5631cb0ef41Sopenharmony_ci 5641cb0ef41Sopenharmony_ci <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> { 5651cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>); 5661cb0ef41Sopenharmony_ci }); 5671cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5681cb0ef41Sopenharmony_ci<p>In this example, <code>await</code> is used to ensure that both subtests have completed. 5691cb0ef41Sopenharmony_ciThis is necessary because parent tests do not wait for their subtests to 5701cb0ef41Sopenharmony_cicomplete. Any subtests that are still outstanding when their parent finishes 5711cb0ef41Sopenharmony_ciare cancelled and treated as failures. Any subtest failures cause the parent 5721cb0ef41Sopenharmony_citest to fail.</p> 5731cb0ef41Sopenharmony_ci</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> 5741cb0ef41Sopenharmony_ci<p>Individual tests can be skipped by passing the <code>skip</code> option to the test, or by 5751cb0ef41Sopenharmony_cicalling the test context's <code>skip()</code> method as shown in the 5761cb0ef41Sopenharmony_cifollowing example.</p> 5771cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// The skip option is used, but no message is provided.</span> 5781cb0ef41Sopenharmony_ci<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> { 5791cb0ef41Sopenharmony_ci <span class="hljs-comment">// This code is never executed.</span> 5801cb0ef41Sopenharmony_ci}); 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_ci<span class="hljs-comment">// The skip option is used, and a message is provided.</span> 5831cb0ef41Sopenharmony_ci<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> { 5841cb0ef41Sopenharmony_ci <span class="hljs-comment">// This code is never executed.</span> 5851cb0ef41Sopenharmony_ci}); 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ci<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> { 5881cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span> 5891cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">skip</span>(); 5901cb0ef41Sopenharmony_ci}); 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci<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> { 5931cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span> 5941cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">skip</span>(<span class="hljs-string">'this is skipped'</span>); 5951cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5961cb0ef41Sopenharmony_ci</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> 5971cb0ef41Sopenharmony_ci<p>Running tests can also be done using <code>describe</code> to declare a suite 5981cb0ef41Sopenharmony_ciand <code>it</code> to declare a test. 5991cb0ef41Sopenharmony_ciA suite is used to organize and group related tests together. 6001cb0ef41Sopenharmony_ci<code>it</code> is a shorthand for <a href="#testname-options-fn"><code>test()</code></a>.</p> 6011cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'A thing'</span>, <span class="hljs-function">() =></span> { 6021cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'should work'</span>, <span class="hljs-function">() =></span> { 6031cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 6041cb0ef41Sopenharmony_ci }); 6051cb0ef41Sopenharmony_ci 6061cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'should be ok'</span>, <span class="hljs-function">() =></span> { 6071cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>); 6081cb0ef41Sopenharmony_ci }); 6091cb0ef41Sopenharmony_ci 6101cb0ef41Sopenharmony_ci <span class="hljs-title function_">describe</span>(<span class="hljs-string">'a nested thing'</span>, <span class="hljs-function">() =></span> { 6111cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'should work'</span>, <span class="hljs-function">() =></span> { 6121cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">3</span>, <span class="hljs-number">3</span>); 6131cb0ef41Sopenharmony_ci }); 6141cb0ef41Sopenharmony_ci }); 6151cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 6161cb0ef41Sopenharmony_ci<p><code>describe</code> and <code>it</code> are imported from the <code>node:test</code> module.</p> 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ci<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> 6191cb0ef41Sopenharmony_ci</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> 6201cb0ef41Sopenharmony_ci<p>If Node.js is started with the <a href="cli.html#--test-only"><code>--test-only</code></a> command-line option, it is 6211cb0ef41Sopenharmony_cipossible to skip all top level tests except for a selected subset by passing 6221cb0ef41Sopenharmony_cithe <code>only</code> option to the tests that should be run. When a test with the <code>only</code> 6231cb0ef41Sopenharmony_cioption set is run, all subtests are also run. The test context's <code>runOnly()</code> 6241cb0ef41Sopenharmony_cimethod can be used to implement the same behavior at the subtest level.</p> 6251cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Assume Node.js is run with the --test-only command-line option.</span> 6261cb0ef41Sopenharmony_ci<span class="hljs-comment">// The 'only' option is set, so this test is run.</span> 6271cb0ef41Sopenharmony_ci<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) => { 6281cb0ef41Sopenharmony_ci <span class="hljs-comment">// Within this test, all subtests are run by default.</span> 6291cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'running subtest'</span>); 6301cb0ef41Sopenharmony_ci 6311cb0ef41Sopenharmony_ci <span class="hljs-comment">// The test context can be updated to run subtests with the 'only' option.</span> 6321cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">true</span>); 6331cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now skipped'</span>); 6341cb0ef41Sopenharmony_ci <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> }); 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ci <span class="hljs-comment">// Switch the context back to execute all tests.</span> 6371cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">false</span>); 6381cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now run'</span>); 6391cb0ef41Sopenharmony_ci 6401cb0ef41Sopenharmony_ci <span class="hljs-comment">// Explicitly do not run these tests.</span> 6411cb0ef41Sopenharmony_ci <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> }); 6421cb0ef41Sopenharmony_ci <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> }); 6431cb0ef41Sopenharmony_ci}); 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci<span class="hljs-comment">// The 'only' option is not set, so this test is skipped.</span> 6461cb0ef41Sopenharmony_ci<span class="hljs-title function_">test</span>(<span class="hljs-string">'this test is not run'</span>, <span class="hljs-function">() =></span> { 6471cb0ef41Sopenharmony_ci <span class="hljs-comment">// This code is not run.</span> 6481cb0ef41Sopenharmony_ci <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>); 6491cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 6501cb0ef41Sopenharmony_ci</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> 6511cb0ef41Sopenharmony_ci<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 6521cb0ef41Sopenharmony_ciwhose name matches the provided pattern. Test name patterns are interpreted as 6531cb0ef41Sopenharmony_ciJavaScript regular expressions. The <code>--test-name-pattern</code> option can be 6541cb0ef41Sopenharmony_cispecified multiple times in order to run nested tests. For each test that is 6551cb0ef41Sopenharmony_ciexecuted, any corresponding test hooks, such as <code>beforeEach()</code>, are also 6561cb0ef41Sopenharmony_cirun.</p> 6571cb0ef41Sopenharmony_ci<p>Given the following test file, starting Node.js with the 6581cb0ef41Sopenharmony_ci<code>--test-name-pattern="test [1-3]"</code> option would cause the test runner to execute 6591cb0ef41Sopenharmony_ci<code>test 1</code>, <code>test 2</code>, and <code>test 3</code>. If <code>test 1</code> did not match the test name 6601cb0ef41Sopenharmony_cipattern, then its subtests would not execute, despite matching the pattern. The 6611cb0ef41Sopenharmony_cisame set of tests could also be executed by passing <code>--test-name-pattern</code> 6621cb0ef41Sopenharmony_cimultiple times (e.g. <code>--test-name-pattern="test 1"</code>, 6631cb0ef41Sopenharmony_ci<code>--test-name-pattern="test 2"</code>, etc.).</p> 6641cb0ef41Sopenharmony_ci<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) => { 6651cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 2'</span>); 6661cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 3'</span>); 6671cb0ef41Sopenharmony_ci}); 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ci<span class="hljs-title function_">test</span>(<span class="hljs-string">'Test 4'</span>, <span class="hljs-keyword">async</span> (t) => { 6701cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'Test 5'</span>); 6711cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'test 6'</span>); 6721cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 6731cb0ef41Sopenharmony_ci<p>Test name patterns can also be specified using regular expression literals. This 6741cb0ef41Sopenharmony_ciallows regular expression flags to be used. In the previous example, starting 6751cb0ef41Sopenharmony_ciNode.js with <code>--test-name-pattern="/test [4-5]/i"</code> would match <code>Test 4</code> and 6761cb0ef41Sopenharmony_ci<code>Test 5</code> because the pattern is case-insensitive.</p> 6771cb0ef41Sopenharmony_ci<p>Test name patterns do not change the set of files that the test runner executes.</p> 6781cb0ef41Sopenharmony_ci</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> 6791cb0ef41Sopenharmony_ci<p>Once a test function finishes executing, the results are reported as quickly 6801cb0ef41Sopenharmony_cias possible while maintaining the order of the tests. However, it is possible 6811cb0ef41Sopenharmony_cifor the test function to generate asynchronous activity that outlives the test 6821cb0ef41Sopenharmony_ciitself. The test runner handles this type of activity, but does not delay the 6831cb0ef41Sopenharmony_cireporting of test results in order to accommodate it.</p> 6841cb0ef41Sopenharmony_ci<p>In the following example, a test completes with two <code>setImmediate()</code> 6851cb0ef41Sopenharmony_cioperations still outstanding. The first <code>setImmediate()</code> attempts to create a 6861cb0ef41Sopenharmony_cinew subtest. Because the parent test has already finished and output its 6871cb0ef41Sopenharmony_ciresults, the new subtest is immediately marked as failed, and reported later 6881cb0ef41Sopenharmony_cito the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p> 6891cb0ef41Sopenharmony_ci<p>The second <code>setImmediate()</code> creates an <code>uncaughtException</code> event. 6901cb0ef41Sopenharmony_ci<code>uncaughtException</code> and <code>unhandledRejection</code> events originating from a completed 6911cb0ef41Sopenharmony_citest are marked as failed by the <code>test</code> module and reported as diagnostic 6921cb0ef41Sopenharmony_ciwarnings at the top level by the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p> 6931cb0ef41Sopenharmony_ci<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> { 6941cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 6951cb0ef41Sopenharmony_ci 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> { 6961cb0ef41Sopenharmony_ci <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>); 6971cb0ef41Sopenharmony_ci }); 6981cb0ef41Sopenharmony_ci }); 6991cb0ef41Sopenharmony_ci 7001cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 7011cb0ef41Sopenharmony_ci <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>); 7021cb0ef41Sopenharmony_ci }); 7031cb0ef41Sopenharmony_ci 7041cb0ef41Sopenharmony_ci <span class="hljs-comment">// The test finishes after this line.</span> 7051cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 7061cb0ef41Sopenharmony_ci</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> 7071cb0ef41Sopenharmony_ci<div class="api_metadata"> 7081cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 7091cb0ef41Sopenharmony_ci</div> 7101cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 7111cb0ef41Sopenharmony_ci<p>The Node.js test runner supports running in watch mode by passing the <code>--watch</code> flag:</p> 7121cb0ef41Sopenharmony_ci<pre><code class="language-bash">node --<span class="hljs-built_in">test</span> --watch</code> <button class="copy-button">copy</button></pre> 7131cb0ef41Sopenharmony_ci<p>In watch mode, the test runner will watch for changes to test files and 7141cb0ef41Sopenharmony_citheir dependencies. When a change is detected, the test runner will 7151cb0ef41Sopenharmony_cirerun the tests affected by the change. 7161cb0ef41Sopenharmony_ciThe test runner will continue to run until the process is terminated.</p> 7171cb0ef41Sopenharmony_ci</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> 7181cb0ef41Sopenharmony_ci<p>The Node.js test runner can be invoked from the command line by passing the 7191cb0ef41Sopenharmony_ci<a href="cli.html#--test"><code>--test</code></a> flag:</p> 7201cb0ef41Sopenharmony_ci<pre><code class="language-bash">node --<span class="hljs-built_in">test</span></code> <button class="copy-button">copy</button></pre> 7211cb0ef41Sopenharmony_ci<p>By default, Node.js will recursively search the current directory for 7221cb0ef41Sopenharmony_ciJavaScript source files matching a specific naming convention. Matching files 7231cb0ef41Sopenharmony_ciare executed as test files. More information on the expected test file naming 7241cb0ef41Sopenharmony_ciconvention and behavior can be found in the <a href="#test-runner-execution-model">test runner execution model</a> 7251cb0ef41Sopenharmony_cisection.</p> 7261cb0ef41Sopenharmony_ci<p>Alternatively, one or more paths can be provided as the final argument(s) to 7271cb0ef41Sopenharmony_cithe Node.js command, as shown below.</p> 7281cb0ef41Sopenharmony_ci<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> 7291cb0ef41Sopenharmony_ci<p>In this example, the test runner will execute the files <code>test1.js</code> and 7301cb0ef41Sopenharmony_ci<code>test2.mjs</code>. The test runner will also recursively search the 7311cb0ef41Sopenharmony_ci<code>custom_test_dir/</code> directory for test files to execute.</p> 7321cb0ef41Sopenharmony_ci<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> 7331cb0ef41Sopenharmony_ci<p>When searching for test files to execute, the test runner behaves as follows:</p> 7341cb0ef41Sopenharmony_ci<ul> 7351cb0ef41Sopenharmony_ci<li>Any files explicitly provided by the user are executed.</li> 7361cb0ef41Sopenharmony_ci<li>If the user did not explicitly specify any paths, the current working 7371cb0ef41Sopenharmony_cidirectory is recursively searched for files as specified in the following 7381cb0ef41Sopenharmony_cisteps.</li> 7391cb0ef41Sopenharmony_ci<li><code>node_modules</code> directories are skipped unless explicitly provided by the 7401cb0ef41Sopenharmony_ciuser.</li> 7411cb0ef41Sopenharmony_ci<li>If a directory named <code>test</code> is encountered, the test runner will search it 7421cb0ef41Sopenharmony_cirecursively for all all <code>.js</code>, <code>.cjs</code>, and <code>.mjs</code> files. All of these files 7431cb0ef41Sopenharmony_ciare treated as test files, and do not need to match the specific naming 7441cb0ef41Sopenharmony_ciconvention detailed below. This is to accommodate projects that place all of 7451cb0ef41Sopenharmony_citheir tests in a single <code>test</code> directory.</li> 7461cb0ef41Sopenharmony_ci<li>In all other directories, <code>.js</code>, <code>.cjs</code>, and <code>.mjs</code> files matching the 7471cb0ef41Sopenharmony_cifollowing patterns are treated as test files: 7481cb0ef41Sopenharmony_ci<ul> 7491cb0ef41Sopenharmony_ci<li><code>^test$</code> - Files whose basename is the string <code>'test'</code>. Examples: 7501cb0ef41Sopenharmony_ci<code>test.js</code>, <code>test.cjs</code>, <code>test.mjs</code>.</li> 7511cb0ef41Sopenharmony_ci<li><code>^test-.+</code> - Files whose basename starts with the string <code>'test-'</code> 7521cb0ef41Sopenharmony_cifollowed by one or more characters. Examples: <code>test-example.js</code>, 7531cb0ef41Sopenharmony_ci<code>test-another-example.mjs</code>.</li> 7541cb0ef41Sopenharmony_ci<li><code>.+[\.\-\_]test$</code> - Files whose basename ends with <code>.test</code>, <code>-test</code>, or 7551cb0ef41Sopenharmony_ci<code>_test</code>, preceded by one or more characters. Examples: <code>example.test.js</code>, 7561cb0ef41Sopenharmony_ci<code>example-test.cjs</code>, <code>example_test.mjs</code>.</li> 7571cb0ef41Sopenharmony_ci<li>Other file types understood by Node.js such as <code>.node</code> and <code>.json</code> are not 7581cb0ef41Sopenharmony_ciautomatically executed by the test runner, but are supported if explicitly 7591cb0ef41Sopenharmony_ciprovided on the command line.</li> 7601cb0ef41Sopenharmony_ci</ul> 7611cb0ef41Sopenharmony_ci</li> 7621cb0ef41Sopenharmony_ci</ul> 7631cb0ef41Sopenharmony_ci<p>Each matching test file is executed in a separate child process. The maximum 7641cb0ef41Sopenharmony_cinumber of child processes running at any time is controlled by the 7651cb0ef41Sopenharmony_ci<a href="cli.html#--test-concurrency"><code>--test-concurrency</code></a> flag. If the child process finishes with an exit code 7661cb0ef41Sopenharmony_ciof 0, the test is considered passing. Otherwise, the test is considered to be a 7671cb0ef41Sopenharmony_cifailure. Test files must be executable by Node.js, but are not required to use 7681cb0ef41Sopenharmony_cithe <code>node:test</code> module internally.</p> 7691cb0ef41Sopenharmony_ci<p>Each test file is executed as if it was a regular script. That is, if the test 7701cb0ef41Sopenharmony_cifile itself uses <code>node:test</code> to define tests, all of those tests will be 7711cb0ef41Sopenharmony_ciexecuted within a single application thread, regardless of the value of the 7721cb0ef41Sopenharmony_ci<code>concurrency</code> option of <a href="#testname-options-fn"><code>test()</code></a>.</p> 7731cb0ef41Sopenharmony_ci</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> 7741cb0ef41Sopenharmony_ci<p>When Node.js is started with the <a href="cli.html#--experimental-test-coverage"><code>--experimental-test-coverage</code></a> 7751cb0ef41Sopenharmony_cicommand-line flag, code coverage is collected and statistics are reported once 7761cb0ef41Sopenharmony_ciall tests have completed. If the <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a> environment variable is 7771cb0ef41Sopenharmony_ciused to specify a code coverage directory, the generated V8 coverage files are 7781cb0ef41Sopenharmony_ciwritten to that directory. Node.js core modules and files within 7791cb0ef41Sopenharmony_ci<code>node_modules/</code> directories are not included in the coverage report. If 7801cb0ef41Sopenharmony_cicoverage is enabled, the coverage report is sent to any <a href="#test-reporters">test reporters</a> via 7811cb0ef41Sopenharmony_cithe <code>'test:coverage'</code> event.</p> 7821cb0ef41Sopenharmony_ci<p>Coverage can be disabled on a series of lines using the following 7831cb0ef41Sopenharmony_cicomment syntax:</p> 7841cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">/* node:coverage disable */</span> 7851cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) { 7861cb0ef41Sopenharmony_ci <span class="hljs-comment">// Code in this branch will never be executed, but the lines are ignored for</span> 7871cb0ef41Sopenharmony_ci <span class="hljs-comment">// coverage purposes. All lines following the 'disable' comment are ignored</span> 7881cb0ef41Sopenharmony_ci <span class="hljs-comment">// until a corresponding 'enable' comment is encountered.</span> 7891cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>); 7901cb0ef41Sopenharmony_ci} 7911cb0ef41Sopenharmony_ci<span class="hljs-comment">/* node:coverage enable */</span></code> <button class="copy-button">copy</button></pre> 7921cb0ef41Sopenharmony_ci<p>Coverage can also be disabled for a specified number of lines. After the 7931cb0ef41Sopenharmony_cispecified number of lines, coverage will be automatically reenabled. If the 7941cb0ef41Sopenharmony_cinumber of lines is not explicitly provided, a single line is ignored.</p> 7951cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">/* node:coverage ignore next */</span> 7961cb0ef41Sopenharmony_ci<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>); } 7971cb0ef41Sopenharmony_ci 7981cb0ef41Sopenharmony_ci<span class="hljs-comment">/* node:coverage ignore next 3 */</span> 7991cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (anAlwaysFalseCondition) { 8001cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this is never executed'</span>); 8011cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 8021cb0ef41Sopenharmony_ci<p>The test runner's code coverage functionality has the following limitations, 8031cb0ef41Sopenharmony_ciwhich will be addressed in a future Node.js release:</p> 8041cb0ef41Sopenharmony_ci<ul> 8051cb0ef41Sopenharmony_ci<li>Source maps are not supported.</li> 8061cb0ef41Sopenharmony_ci<li>Excluding specific files or directories from the coverage report is not 8071cb0ef41Sopenharmony_cisupported.</li> 8081cb0ef41Sopenharmony_ci</ul> 8091cb0ef41Sopenharmony_ci</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> 8101cb0ef41Sopenharmony_ci<p>The <code>node:test</code> module supports mocking during testing via a top-level <code>mock</code> 8111cb0ef41Sopenharmony_ciobject. The following example creates a spy on a function that adds two numbers 8121cb0ef41Sopenharmony_citogether. The spy is then used to assert that the function was called as 8131cb0ef41Sopenharmony_ciexpected.</p> 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ci<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>; 8161cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { mock, test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ci<span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on a function'</span>, <span class="hljs-function">() =></span> { 8191cb0ef41Sopenharmony_ci <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> { 8201cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> a + b; 8211cb0ef41Sopenharmony_ci }); 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ci 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>); 8241cb0ef41Sopenharmony_ci 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>); 8251cb0ef41Sopenharmony_ci 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>); 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ci <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>]; 8281cb0ef41Sopenharmony_ci 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>]); 8291cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">7</span>); 8301cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>); 8311cb0ef41Sopenharmony_ci 8321cb0ef41Sopenharmony_ci <span class="hljs-comment">// Reset the globally tracked mocks.</span> 8331cb0ef41Sopenharmony_ci mock.<span class="hljs-title function_">reset</span>(); 8341cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>; 8351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 8361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { mock, test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 8371cb0ef41Sopenharmony_ci 8381cb0ef41Sopenharmony_ci<span class="hljs-title function_">test</span>(<span class="hljs-string">'spies on a function'</span>, <span class="hljs-function">() =></span> { 8391cb0ef41Sopenharmony_ci <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> { 8401cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> a + b; 8411cb0ef41Sopenharmony_ci }); 8421cb0ef41Sopenharmony_ci 8431cb0ef41Sopenharmony_ci 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>); 8441cb0ef41Sopenharmony_ci 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>); 8451cb0ef41Sopenharmony_ci 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>); 8461cb0ef41Sopenharmony_ci 8471cb0ef41Sopenharmony_ci <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>]; 8481cb0ef41Sopenharmony_ci 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>]); 8491cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">7</span>); 8501cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>); 8511cb0ef41Sopenharmony_ci 8521cb0ef41Sopenharmony_ci <span class="hljs-comment">// Reset the globally tracked mocks.</span> 8531cb0ef41Sopenharmony_ci mock.<span class="hljs-title function_">reset</span>(); 8541cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 8551cb0ef41Sopenharmony_ci<p>The same mocking functionality is also exposed on the <a href="#class-testcontext"><code>TestContext</code></a> object 8561cb0ef41Sopenharmony_ciof each test. The following example creates a spy on an object method using the 8571cb0ef41Sopenharmony_ciAPI exposed on the <code>TestContext</code>. The benefit of mocking via the test context is 8581cb0ef41Sopenharmony_cithat the test runner will automatically restore all mocked functionality once 8591cb0ef41Sopenharmony_cithe test finishes.</p> 8601cb0ef41Sopenharmony_ci<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> { 8611cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> number = { 8621cb0ef41Sopenharmony_ci <span class="hljs-attr">value</span>: <span class="hljs-number">5</span>, 8631cb0ef41Sopenharmony_ci <span class="hljs-title function_">add</span>(<span class="hljs-params">a</span>) { 8641cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> + a; 8651cb0ef41Sopenharmony_ci }, 8661cb0ef41Sopenharmony_ci }; 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ci t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">method</span>(number, <span class="hljs-string">'add'</span>); 8691cb0ef41Sopenharmony_ci 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>); 8701cb0ef41Sopenharmony_ci 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>); 8711cb0ef41Sopenharmony_ci 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>); 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ci <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>]; 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>]); 8761cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">8</span>); 8771cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">target</span>, <span class="hljs-literal">undefined</span>); 8781cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">this</span>, number); 8791cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 8801cb0ef41Sopenharmony_ci<h4>Timers<span><a class="mark" href="#timers" id="timers">#</a></span><a aria-hidden="true" class="legacy" id="test_timers"></a></h4> 8811cb0ef41Sopenharmony_ci<p>Mocking timers is a technique commonly used in software testing to simulate and 8821cb0ef41Sopenharmony_cicontrol the behavior of timers, such as <code>setInterval</code> and <code>setTimeout</code>, 8831cb0ef41Sopenharmony_ciwithout actually waiting for the specified time intervals.</p> 8841cb0ef41Sopenharmony_ci<p>Refer to the <a href="#class-mocktimers"><code>MockTimers</code></a> class for a full list of methods and features.</p> 8851cb0ef41Sopenharmony_ci<p>This allows developers to write more reliable and 8861cb0ef41Sopenharmony_cipredictable tests for time-dependent functionality.</p> 8871cb0ef41Sopenharmony_ci<p>The example below shows how to mock <code>setTimeout</code>. 8881cb0ef41Sopenharmony_ciUsing <code>.enable(['setTimeout']);</code> 8891cb0ef41Sopenharmony_ciit will mock the <code>setTimeout</code> functions in the <a href="./timers.html">node:timers</a> and 8901cb0ef41Sopenharmony_ci<a href="./timers.html#timers-promises-api">node:timers/promises</a> modules, 8911cb0ef41Sopenharmony_cias well as from the Node.js global context.</p> 8921cb0ef41Sopenharmony_ci<p><strong>Note:</strong> Destructuring functions such as 8931cb0ef41Sopenharmony_ci<code>import { setTimeout } from 'node:timers'</code> 8941cb0ef41Sopenharmony_ciis currently not supported by this API.</p> 8951cb0ef41Sopenharmony_ci<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>; 8961cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { mock, test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ci<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> { 8991cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = mock.<span class="hljs-title function_">fn</span>(); 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 9021cb0ef41Sopenharmony_ci mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 9031cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 9041cb0ef41Sopenharmony_ci 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>); 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 9071cb0ef41Sopenharmony_ci mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>); 9081cb0ef41Sopenharmony_ci 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>); 9091cb0ef41Sopenharmony_ci 9101cb0ef41Sopenharmony_ci <span class="hljs-comment">// Reset the globally tracked mocks.</span> 9111cb0ef41Sopenharmony_ci mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>(); 9121cb0ef41Sopenharmony_ci 9131cb0ef41Sopenharmony_ci <span class="hljs-comment">// If you call reset mock instance, it will also reset timers instance</span> 9141cb0ef41Sopenharmony_ci mock.<span class="hljs-title function_">reset</span>(); 9151cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9161cb0ef41Sopenharmony_ci<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>); 9171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { mock, test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 9181cb0ef41Sopenharmony_ci 9191cb0ef41Sopenharmony_ci<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> { 9201cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = mock.<span class="hljs-title function_">fn</span>(); 9211cb0ef41Sopenharmony_ci 9221cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 9231cb0ef41Sopenharmony_ci mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">enable</span>([<span class="hljs-string">'setTimeout'</span>]); 9241cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 9251cb0ef41Sopenharmony_ci 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>); 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 9281cb0ef41Sopenharmony_ci mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(<span class="hljs-number">9999</span>); 9291cb0ef41Sopenharmony_ci 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>); 9301cb0ef41Sopenharmony_ci 9311cb0ef41Sopenharmony_ci <span class="hljs-comment">// Reset the globally tracked mocks.</span> 9321cb0ef41Sopenharmony_ci mock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>(); 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci <span class="hljs-comment">// If you call reset mock instance, it'll also reset timers instance</span> 9351cb0ef41Sopenharmony_ci mock.<span class="hljs-title function_">reset</span>(); 9361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9371cb0ef41Sopenharmony_ci<p>The same mocking functionality is also exposed in the mock property on the <a href="#class-testcontext"><code>TestContext</code></a> object 9381cb0ef41Sopenharmony_ciof each test. The benefit of mocking via the test context is 9391cb0ef41Sopenharmony_cithat the test runner will automatically restore all mocked timers 9401cb0ef41Sopenharmony_cifunctionality once the test finishes.</p> 9411cb0ef41Sopenharmony_ci<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>; 9421cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 9431cb0ef41Sopenharmony_ci 9441cb0ef41Sopenharmony_ci<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> { 9451cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 9461cb0ef41Sopenharmony_ci 9471cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 9481cb0ef41Sopenharmony_ci 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>]); 9491cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 9501cb0ef41Sopenharmony_ci 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>); 9511cb0ef41Sopenharmony_ci 9521cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 9531cb0ef41Sopenharmony_ci 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>); 9541cb0ef41Sopenharmony_ci 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>); 9551cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9561cb0ef41Sopenharmony_ci<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>); 9571cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 9581cb0ef41Sopenharmony_ci 9591cb0ef41Sopenharmony_ci<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> { 9601cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 9611cb0ef41Sopenharmony_ci 9621cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 9631cb0ef41Sopenharmony_ci 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>]); 9641cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 9651cb0ef41Sopenharmony_ci 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>); 9661cb0ef41Sopenharmony_ci 9671cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 9681cb0ef41Sopenharmony_ci 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>); 9691cb0ef41Sopenharmony_ci 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>); 9701cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9711cb0ef41Sopenharmony_ci</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> 9721cb0ef41Sopenharmony_ci<div class="api_metadata"> 9731cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9741cb0ef41Sopenharmony_ci<table> 9751cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9761cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 9771cb0ef41Sopenharmony_ci<td><p>Reporters are now exposed at <code>node:test/reporters</code>.</p></td></tr> 9781cb0ef41Sopenharmony_ci<tr><td>v18.15.0</td> 9791cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.15.0</span></p></td></tr> 9801cb0ef41Sopenharmony_ci</tbody></table> 9811cb0ef41Sopenharmony_ci</details> 9821cb0ef41Sopenharmony_ci</div> 9831cb0ef41Sopenharmony_ci<p>The <code>node:test</code> module supports passing <a href="cli.html#--test-reporter"><code>--test-reporter</code></a> 9841cb0ef41Sopenharmony_ciflags for the test runner to use a specific reporter.</p> 9851cb0ef41Sopenharmony_ci<p>The following built-reporters are supported:</p> 9861cb0ef41Sopenharmony_ci<ul> 9871cb0ef41Sopenharmony_ci<li> 9881cb0ef41Sopenharmony_ci<p><code>tap</code> 9891cb0ef41Sopenharmony_ciThe <code>tap</code> reporter outputs the test results in the <a href="https://testanything.org/">TAP</a> format.</p> 9901cb0ef41Sopenharmony_ci</li> 9911cb0ef41Sopenharmony_ci<li> 9921cb0ef41Sopenharmony_ci<p><code>spec</code> 9931cb0ef41Sopenharmony_ciThe <code>spec</code> reporter outputs the test results in a human-readable format.</p> 9941cb0ef41Sopenharmony_ci</li> 9951cb0ef41Sopenharmony_ci<li> 9961cb0ef41Sopenharmony_ci<p><code>dot</code> 9971cb0ef41Sopenharmony_ciThe <code>dot</code> reporter outputs the test results in a compact format, 9981cb0ef41Sopenharmony_ciwhere each passing test is represented by a <code>.</code>, 9991cb0ef41Sopenharmony_ciand each failing test is represented by a <code>X</code>.</p> 10001cb0ef41Sopenharmony_ci</li> 10011cb0ef41Sopenharmony_ci<li> 10021cb0ef41Sopenharmony_ci<p><code>junit</code> 10031cb0ef41Sopenharmony_ciThe junit reporter outputs test results in a jUnit XML format</p> 10041cb0ef41Sopenharmony_ci</li> 10051cb0ef41Sopenharmony_ci</ul> 10061cb0ef41Sopenharmony_ci<p>When <code>stdout</code> is a <a href="tty.html">TTY</a>, the <code>spec</code> reporter is used by default. 10071cb0ef41Sopenharmony_ciOtherwise, the <code>tap</code> reporter is used by default.</p> 10081cb0ef41Sopenharmony_ci<p>The reporters are available via the <code>node:test/reporters</code> module:</p> 10091cb0ef41Sopenharmony_ci 10101cb0ef41Sopenharmony_ci<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> 10111cb0ef41Sopenharmony_ci<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> 10121cb0ef41Sopenharmony_ci<p><a href="cli.html#--test-reporter"><code>--test-reporter</code></a> can be used to specify a path to custom reporter. 10131cb0ef41Sopenharmony_ciA custom reporter is a module that exports a value 10141cb0ef41Sopenharmony_ciaccepted by <a href="stream.html#streamcomposestreams">stream.compose</a>. 10151cb0ef41Sopenharmony_ciReporters should transform events emitted by a <a href="test.html#class-testsstream" class="type"><TestsStream></a></p> 10161cb0ef41Sopenharmony_ci<p>Example of a custom reporter using <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a>:</p> 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ci<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>; 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> customReporter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Transform</span>({ 10211cb0ef41Sopenharmony_ci <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>, 10221cb0ef41Sopenharmony_ci <span class="hljs-title function_">transform</span>(<span class="hljs-params">event, encoding, callback</span>) { 10231cb0ef41Sopenharmony_ci <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 10241cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 10251cb0ef41Sopenharmony_ci <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>); 10261cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10271cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 10281cb0ef41Sopenharmony_ci <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>); 10291cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10301cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 10311cb0ef41Sopenharmony_ci <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test watch queue drained'</span>); 10321cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10331cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 10341cb0ef41Sopenharmony_ci <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>); 10351cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10361cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 10371cb0ef41Sopenharmony_ci <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>); 10381cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10391cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 10401cb0ef41Sopenharmony_ci <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>); 10411cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10421cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 10431cb0ef41Sopenharmony_ci <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test plan'</span>); 10441cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10451cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 10461cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 10471cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 10481cb0ef41Sopenharmony_ci <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>); 10491cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10501cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 10511cb0ef41Sopenharmony_ci <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>; 10521cb0ef41Sopenharmony_ci <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>); 10531cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10541cb0ef41Sopenharmony_ci } 10551cb0ef41Sopenharmony_ci } 10561cb0ef41Sopenharmony_ci }, 10571cb0ef41Sopenharmony_ci}); 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ci<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>); 10601cb0ef41Sopenharmony_ci 10611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> customReporter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Transform</span>({ 10621cb0ef41Sopenharmony_ci <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>, 10631cb0ef41Sopenharmony_ci <span class="hljs-title function_">transform</span>(<span class="hljs-params">event, encoding, callback</span>) { 10641cb0ef41Sopenharmony_ci <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 10651cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 10661cb0ef41Sopenharmony_ci <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>); 10671cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10681cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 10691cb0ef41Sopenharmony_ci <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>); 10701cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10711cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 10721cb0ef41Sopenharmony_ci <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test watch queue drained'</span>); 10731cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10741cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 10751cb0ef41Sopenharmony_ci <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>); 10761cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10771cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 10781cb0ef41Sopenharmony_ci <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>); 10791cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10801cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 10811cb0ef41Sopenharmony_ci <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>); 10821cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10831cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 10841cb0ef41Sopenharmony_ci <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'test plan'</span>); 10851cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10861cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 10871cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 10881cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 10891cb0ef41Sopenharmony_ci <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>); 10901cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10911cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 10921cb0ef41Sopenharmony_ci <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>; 10931cb0ef41Sopenharmony_ci <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>); 10941cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 10951cb0ef41Sopenharmony_ci } 10961cb0ef41Sopenharmony_ci } 10971cb0ef41Sopenharmony_ci }, 10981cb0ef41Sopenharmony_ci}); 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ci<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = customReporter;</code><button class="copy-button">copy</button></pre> 11011cb0ef41Sopenharmony_ci<p>Example of a custom reporter using a generator function:</p> 11021cb0ef41Sopenharmony_ci 11031cb0ef41Sopenharmony_ci<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>) { 11041cb0ef41Sopenharmony_ci <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) { 11051cb0ef41Sopenharmony_ci <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 11061cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 11071cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued`</span>; 11081cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11091cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 11101cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued`</span>; 11111cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11121cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 11131cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">'test watch queue drained'</span>; 11141cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11151cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 11161cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started\n`</span>; 11171cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11181cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 11191cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed\n`</span>; 11201cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11211cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 11221cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed\n`</span>; 11231cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11241cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 11251cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">'test plan'</span>; 11261cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11271cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 11281cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 11291cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 11301cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`<span class="hljs-subst">${event.data.message}</span>\n`</span>; 11311cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11321cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 11331cb0ef41Sopenharmony_ci <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>; 11341cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>; 11351cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11361cb0ef41Sopenharmony_ci } 11371cb0ef41Sopenharmony_ci } 11381cb0ef41Sopenharmony_ci } 11391cb0ef41Sopenharmony_ci}</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>) { 11401cb0ef41Sopenharmony_ci <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) { 11411cb0ef41Sopenharmony_ci <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">type</span>) { 11421cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:dequeue'</span>: 11431cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> dequeued`</span>; 11441cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11451cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:enqueue'</span>: 11461cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> enqueued`</span>; 11471cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11481cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:watch:drained'</span>: 11491cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">'test watch queue drained'</span>; 11501cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11511cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:start'</span>: 11521cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> started\n`</span>; 11531cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11541cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:pass'</span>: 11551cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> passed\n`</span>; 11561cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11571cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:fail'</span>: 11581cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`test <span class="hljs-subst">${event.data.name}</span> failed\n`</span>; 11591cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11601cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:plan'</span>: 11611cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">'test plan\n'</span>; 11621cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11631cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:diagnostic'</span>: 11641cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stderr'</span>: 11651cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:stdout'</span>: 11661cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`<span class="hljs-subst">${event.data.message}</span>\n`</span>; 11671cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11681cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'test:coverage'</span>: { 11691cb0ef41Sopenharmony_ci <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>; 11701cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> <span class="hljs-string">`total line count: <span class="hljs-subst">${totalLineCount}</span>\n`</span>; 11711cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 11721cb0ef41Sopenharmony_ci } 11731cb0ef41Sopenharmony_ci } 11741cb0ef41Sopenharmony_ci } 11751cb0ef41Sopenharmony_ci};</code><button class="copy-button">copy</button></pre> 11761cb0ef41Sopenharmony_ci<p>The value provided to <code>--test-reporter</code> should be a string like one used in an 11771cb0ef41Sopenharmony_ci<code>import()</code> in JavaScript code.</p> 11781cb0ef41Sopenharmony_ci<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> 11791cb0ef41Sopenharmony_ci<p>The <a href="cli.html#--test-reporter"><code>--test-reporter</code></a> flag can be specified multiple times to report test 11801cb0ef41Sopenharmony_ciresults in several formats. In this situation 11811cb0ef41Sopenharmony_ciit is required to specify a destination for each reporter 11821cb0ef41Sopenharmony_ciusing <a href="cli.html#--test-reporter-destination"><code>--test-reporter-destination</code></a>. 11831cb0ef41Sopenharmony_ciDestination can be <code>stdout</code>, <code>stderr</code>, or a file path. 11841cb0ef41Sopenharmony_ciReporters and destinations are paired according 11851cb0ef41Sopenharmony_cito the order they were specified.</p> 11861cb0ef41Sopenharmony_ci<p>In the following example, the <code>spec</code> reporter will output to <code>stdout</code>, 11871cb0ef41Sopenharmony_ciand the <code>dot</code> reporter will output to <code>file.txt</code>:</p> 11881cb0ef41Sopenharmony_ci<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> 11891cb0ef41Sopenharmony_ci<p>When a single reporter is specified, the destination will default to <code>stdout</code>, 11901cb0ef41Sopenharmony_ciunless a destination is explicitly provided.</p> 11911cb0ef41Sopenharmony_ci</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> 11921cb0ef41Sopenharmony_ci<div class="api_metadata"> 11931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11941cb0ef41Sopenharmony_ci<table> 11951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11961cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 11971cb0ef41Sopenharmony_ci<td><p>Add a testNamePatterns option.</p></td></tr> 11981cb0ef41Sopenharmony_ci<tr><td>v18.9.0</td> 11991cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.9.0</span></p></td></tr> 12001cb0ef41Sopenharmony_ci</tbody></table> 12011cb0ef41Sopenharmony_ci</details> 12021cb0ef41Sopenharmony_ci</div> 12031cb0ef41Sopenharmony_ci<ul> 12041cb0ef41Sopenharmony_ci<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 12051cb0ef41Sopenharmony_ciproperties are supported: 12061cb0ef41Sopenharmony_ci<ul> 12071cb0ef41Sopenharmony_ci<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, 12081cb0ef41Sopenharmony_cithen that many test processes would run in parallel, where each process 12091cb0ef41Sopenharmony_cicorresponds to one test file. 12101cb0ef41Sopenharmony_ciIf <code>true</code>, it would run <code>os.availableParallelism() - 1</code> test files in 12111cb0ef41Sopenharmony_ciparallel. 12121cb0ef41Sopenharmony_ciIf <code>false</code>, it would only run one test file at a time. 12131cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 12141cb0ef41Sopenharmony_ci<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. 12151cb0ef41Sopenharmony_ci<strong>Default</strong> matching files from <a href="#test-runner-execution-model">test runner execution model</a>.</li> 12161cb0ef41Sopenharmony_ci<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. 12171cb0ef41Sopenharmony_ciThis can be a number, or a function that takes no arguments and returns a 12181cb0ef41Sopenharmony_cinumber. If a nullish value is provided, each process gets its own port, 12191cb0ef41Sopenharmony_ciincremented from the primary's <code>process.debugPort</code>. 12201cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>undefined</code>.</li> 12211cb0ef41Sopenharmony_ci<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 12221cb0ef41Sopenharmony_cihave the <code>only</code> option set</li> 12231cb0ef41Sopenharmony_ci<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 12241cb0ef41Sopenharmony_ciand can be used to setup listeners before any tests are run. 12251cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>undefined</code>.</li> 12261cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test execution.</li> 12271cb0ef41Sopenharmony_ci<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, 12281cb0ef41Sopenharmony_cithat can be used to only run tests whose name matches the provided pattern. 12291cb0ef41Sopenharmony_ciTest name patterns are interpreted as JavaScript regular expressions. 12301cb0ef41Sopenharmony_ciFor each test that is executed, any corresponding test hooks, such as 12311cb0ef41Sopenharmony_ci<code>beforeEach()</code>, are also run. 12321cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>undefined</code>.</li> 12331cb0ef41Sopenharmony_ci<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 12341cb0ef41Sopenharmony_cifail after. 12351cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 12361cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 12371cb0ef41Sopenharmony_ci<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> 12381cb0ef41Sopenharmony_ci<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>. 12391cb0ef41Sopenharmony_ci<ul> 12401cb0ef41Sopenharmony_ci<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> 12411cb0ef41Sopenharmony_cithat specifies the index of the shard to run. This option is <em>required</em>.</li> 12421cb0ef41Sopenharmony_ci<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 12431cb0ef41Sopenharmony_ciof shards to split the test files to. This option is <em>required</em>.</li> 12441cb0ef41Sopenharmony_ci</ul> 12451cb0ef41Sopenharmony_ci</li> 12461cb0ef41Sopenharmony_ci</ul> 12471cb0ef41Sopenharmony_ci</li> 12481cb0ef41Sopenharmony_ci<li>Returns: <a href="test.html#class-testsstream" class="type"><TestsStream></a></li> 12491cb0ef41Sopenharmony_ci</ul> 12501cb0ef41Sopenharmony_ci 12511cb0ef41Sopenharmony_ci<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>; 12521cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 12531cb0ef41Sopenharmony_ci 12541cb0ef41Sopenharmony_ci<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>)] }) 12551cb0ef41Sopenharmony_ci .<span class="hljs-title function_">compose</span>(tap) 12561cb0ef41Sopenharmony_ci .<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>); 12571cb0ef41Sopenharmony_ci 12581cb0ef41Sopenharmony_ci<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>)] }) 12591cb0ef41Sopenharmony_ci .<span class="hljs-title function_">compose</span>(tap) 12601cb0ef41Sopenharmony_ci .<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code><button class="copy-button">copy</button></pre> 12611cb0ef41Sopenharmony_ci</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> 12621cb0ef41Sopenharmony_ci<div class="api_metadata"> 12631cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12641cb0ef41Sopenharmony_ci<table> 12651cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12661cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 12671cb0ef41Sopenharmony_ci<td><p>Added the <code>skip</code>, <code>todo</code>, and <code>only</code> shorthands.</p></td></tr> 12681cb0ef41Sopenharmony_ci<tr><td>v18.8.0</td> 12691cb0ef41Sopenharmony_ci<td><p>Add a <code>signal</code> option.</p></td></tr> 12701cb0ef41Sopenharmony_ci<tr><td>v18.7.0</td> 12711cb0ef41Sopenharmony_ci<td><p>Add a <code>timeout</code> option.</p></td></tr> 12721cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 12731cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.0.0</span></p></td></tr> 12741cb0ef41Sopenharmony_ci</tbody></table> 12751cb0ef41Sopenharmony_ci</details> 12761cb0ef41Sopenharmony_ci</div> 12771cb0ef41Sopenharmony_ci<ul> 12781cb0ef41Sopenharmony_ci<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 12791cb0ef41Sopenharmony_ciresults. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if <code>fn</code> 12801cb0ef41Sopenharmony_cidoes not have a name.</li> 12811cb0ef41Sopenharmony_ci<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 12821cb0ef41Sopenharmony_ciproperties are supported: 12831cb0ef41Sopenharmony_ci<ul> 12841cb0ef41Sopenharmony_ci<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, 12851cb0ef41Sopenharmony_cithen that many tests would run in parallel within the application thread. 12861cb0ef41Sopenharmony_ciIf <code>true</code>, all scheduled asynchronous tests run concurrently within the 12871cb0ef41Sopenharmony_cithread. If <code>false</code>, only one test runs at a time. 12881cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 12891cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 12901cb0ef41Sopenharmony_ci<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 12911cb0ef41Sopenharmony_ci<code>only</code> tests, then this test will be run. Otherwise, the test is skipped. 12921cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 12931cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test.</li> 12941cb0ef41Sopenharmony_ci<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 12951cb0ef41Sopenharmony_ciprovided, that string is displayed in the test results as the reason for 12961cb0ef41Sopenharmony_ciskipping the test. <strong>Default:</strong> <code>false</code>.</li> 12971cb0ef41Sopenharmony_ci<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 12981cb0ef41Sopenharmony_ciis provided, that string is displayed in the test results as the reason why 12991cb0ef41Sopenharmony_cithe test is <code>TODO</code>. <strong>Default:</strong> <code>false</code>.</li> 13001cb0ef41Sopenharmony_ci<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. 13011cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 13021cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 13031cb0ef41Sopenharmony_ci</ul> 13041cb0ef41Sopenharmony_ci</li> 13051cb0ef41Sopenharmony_ci<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 13061cb0ef41Sopenharmony_cito this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the test uses callbacks, 13071cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 13081cb0ef41Sopenharmony_cifunction.</li> 13091cb0ef41Sopenharmony_ci<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 13101cb0ef41Sopenharmony_cithe test completes, or immediately if the test runs within <a href="#describename-options-fn"><code>describe()</code></a>.</li> 13111cb0ef41Sopenharmony_ci</ul> 13121cb0ef41Sopenharmony_ci<p>The <code>test()</code> function is the value imported from the <code>test</code> module. Each 13131cb0ef41Sopenharmony_ciinvocation of this function results in reporting the test to the <a href="test.html#class-testsstream" class="type"><TestsStream></a>.</p> 13141cb0ef41Sopenharmony_ci<p>The <code>TestContext</code> object passed to the <code>fn</code> argument can be used to perform 13151cb0ef41Sopenharmony_ciactions related to the current test. Examples include skipping the test, adding 13161cb0ef41Sopenharmony_ciadditional diagnostic information, or creating subtests.</p> 13171cb0ef41Sopenharmony_ci<p><code>test()</code> returns a <code>Promise</code> that resolves once the test completes. 13181cb0ef41Sopenharmony_ciif <code>test()</code> is called within a <code>describe()</code> block, it resolve immediately. 13191cb0ef41Sopenharmony_ciThe return value can usually be discarded for top level tests. 13201cb0ef41Sopenharmony_ciHowever, the return value from subtests should be used to prevent the parent 13211cb0ef41Sopenharmony_citest from finishing first and cancelling the subtest 13221cb0ef41Sopenharmony_cias shown in the following example.</p> 13231cb0ef41Sopenharmony_ci<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) => { 13241cb0ef41Sopenharmony_ci <span class="hljs-comment">// The setTimeout() in the following subtest would cause it to outlive its</span> 13251cb0ef41Sopenharmony_ci <span class="hljs-comment">// parent test if 'await' is removed on the next line. Once the parent test</span> 13261cb0ef41Sopenharmony_ci <span class="hljs-comment">// completes, it will cancel any outstanding subtests.</span> 13271cb0ef41Sopenharmony_ci <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) => { 13281cb0ef41Sopenharmony_ci <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> { 13291cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">1000</span>); 13301cb0ef41Sopenharmony_ci }); 13311cb0ef41Sopenharmony_ci }); 13321cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 13331cb0ef41Sopenharmony_ci<p>The <code>timeout</code> option can be used to fail the test if it takes longer than 13341cb0ef41Sopenharmony_ci<code>timeout</code> milliseconds to complete. However, it is not a reliable mechanism for 13351cb0ef41Sopenharmony_cicanceling tests because a running test might block the application thread and 13361cb0ef41Sopenharmony_cithus prevent the scheduled cancellation.</p> 13371cb0ef41Sopenharmony_ci</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> 13381cb0ef41Sopenharmony_ci<p>Shorthand for skipping a test, 13391cb0ef41Sopenharmony_cisame as <a href="#testname-options-fn"><code>test([name], { skip: true }[, fn])</code></a>.</p> 13401cb0ef41Sopenharmony_ci</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> 13411cb0ef41Sopenharmony_ci<p>Shorthand for marking a test as <code>TODO</code>, 13421cb0ef41Sopenharmony_cisame as <a href="#testname-options-fn"><code>test([name], { todo: true }[, fn])</code></a>.</p> 13431cb0ef41Sopenharmony_ci</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> 13441cb0ef41Sopenharmony_ci<p>Shorthand for marking a test as <code>only</code>, 13451cb0ef41Sopenharmony_cisame as <a href="#testname-options-fn"><code>test([name], { only: true }[, fn])</code></a>.</p> 13461cb0ef41Sopenharmony_ci</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> 13471cb0ef41Sopenharmony_ci<ul> 13481cb0ef41Sopenharmony_ci<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 13491cb0ef41Sopenharmony_ciresults. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if <code>fn</code> 13501cb0ef41Sopenharmony_cidoes not have a name.</li> 13511cb0ef41Sopenharmony_ci<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. 13521cb0ef41Sopenharmony_cisupports the same options as <code>test([name][, options][, fn])</code>.</li> 13531cb0ef41Sopenharmony_ci<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 13541cb0ef41Sopenharmony_cideclaring all subtests and subsuites. 13551cb0ef41Sopenharmony_ciThe first argument to this function is a <a href="#class-suitecontext"><code>SuiteContext</code></a> object. 13561cb0ef41Sopenharmony_ci<strong>Default:</strong> A no-op function.</li> 13571cb0ef41Sopenharmony_ci<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> 13581cb0ef41Sopenharmony_ci</ul> 13591cb0ef41Sopenharmony_ci<p>The <code>describe()</code> function imported from the <code>node:test</code> module. Each 13601cb0ef41Sopenharmony_ciinvocation of this function results in the creation of a Subtest. 13611cb0ef41Sopenharmony_ciAfter invocation of top level <code>describe</code> functions, 13621cb0ef41Sopenharmony_ciall top level tests and suites will execute.</p> 13631cb0ef41Sopenharmony_ci</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> 13641cb0ef41Sopenharmony_ci<p>Shorthand for skipping a suite, same as <a href="#describename-options-fn"><code>describe([name], { skip: true }[, fn])</code></a>.</p> 13651cb0ef41Sopenharmony_ci</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> 13661cb0ef41Sopenharmony_ci<p>Shorthand for marking a suite as <code>TODO</code>, same as 13671cb0ef41Sopenharmony_ci<a href="#describename-options-fn"><code>describe([name], { todo: true }[, fn])</code></a>.</p> 13681cb0ef41Sopenharmony_ci</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> 13691cb0ef41Sopenharmony_ci<div class="api_metadata"> 13701cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span> 13711cb0ef41Sopenharmony_ci</div> 13721cb0ef41Sopenharmony_ci<p>Shorthand for marking a suite as <code>only</code>, same as 13731cb0ef41Sopenharmony_ci<a href="#describename-options-fn"><code>describe([name], { only: true }[, fn])</code></a>.</p> 13741cb0ef41Sopenharmony_ci</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> 13751cb0ef41Sopenharmony_ci<div class="api_metadata"> 13761cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13771cb0ef41Sopenharmony_ci<table> 13781cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13791cb0ef41Sopenharmony_ci<tr><td>v18.16.0</td> 13801cb0ef41Sopenharmony_ci<td><p>Calling <code>it()</code> is now equivalent to calling <code>test()</code>.</p></td></tr> 13811cb0ef41Sopenharmony_ci<tr><td>v18.6.0, v16.17.0</td> 13821cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.6.0, v16.17.0</span></p></td></tr> 13831cb0ef41Sopenharmony_ci</tbody></table> 13841cb0ef41Sopenharmony_ci</details> 13851cb0ef41Sopenharmony_ci</div> 13861cb0ef41Sopenharmony_ci<p>Shorthand for <a href="#testname-options-fn"><code>test()</code></a>.</p> 13871cb0ef41Sopenharmony_ci<p>The <code>it()</code> function is imported from the <code>node:test</code> module.</p> 13881cb0ef41Sopenharmony_ci</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> 13891cb0ef41Sopenharmony_ci<p>Shorthand for skipping a test, 13901cb0ef41Sopenharmony_cisame as <a href="#testname-options-fn"><code>it([name], { skip: true }[, fn])</code></a>.</p> 13911cb0ef41Sopenharmony_ci</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> 13921cb0ef41Sopenharmony_ci<p>Shorthand for marking a test as <code>TODO</code>, 13931cb0ef41Sopenharmony_cisame as <a href="#testname-options-fn"><code>it([name], { todo: true }[, fn])</code></a>.</p> 13941cb0ef41Sopenharmony_ci</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> 13951cb0ef41Sopenharmony_ci<div class="api_metadata"> 13961cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span> 13971cb0ef41Sopenharmony_ci</div> 13981cb0ef41Sopenharmony_ci<p>Shorthand for marking a test as <code>only</code>, 13991cb0ef41Sopenharmony_cisame as <a href="#testname-options-fn"><code>it([name], { only: true }[, fn])</code></a>.</p> 14001cb0ef41Sopenharmony_ci</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> 14011cb0ef41Sopenharmony_ci<div class="api_metadata"> 14021cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 14031cb0ef41Sopenharmony_ci</div> 14041cb0ef41Sopenharmony_ci<ul> 14051cb0ef41Sopenharmony_ci<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. 14061cb0ef41Sopenharmony_ciIf the hook uses callbacks, 14071cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 14081cb0ef41Sopenharmony_cifunction.</li> 14091cb0ef41Sopenharmony_ci<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 14101cb0ef41Sopenharmony_ciproperties are supported: 14111cb0ef41Sopenharmony_ci<ul> 14121cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 14131cb0ef41Sopenharmony_ci<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. 14141cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 14151cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 14161cb0ef41Sopenharmony_ci</ul> 14171cb0ef41Sopenharmony_ci</li> 14181cb0ef41Sopenharmony_ci</ul> 14191cb0ef41Sopenharmony_ci<p>This function is used to create a hook running before running a suite.</p> 14201cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 14211cb0ef41Sopenharmony_ci <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>)); 14221cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 14231cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 14241cb0ef41Sopenharmony_ci }); 14251cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 14261cb0ef41Sopenharmony_ci</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> 14271cb0ef41Sopenharmony_ci<div class="api_metadata"> 14281cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 14291cb0ef41Sopenharmony_ci</div> 14301cb0ef41Sopenharmony_ci<ul> 14311cb0ef41Sopenharmony_ci<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. 14321cb0ef41Sopenharmony_ciIf the hook uses callbacks, 14331cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 14341cb0ef41Sopenharmony_cifunction.</li> 14351cb0ef41Sopenharmony_ci<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 14361cb0ef41Sopenharmony_ciproperties are supported: 14371cb0ef41Sopenharmony_ci<ul> 14381cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 14391cb0ef41Sopenharmony_ci<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. 14401cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 14411cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 14421cb0ef41Sopenharmony_ci</ul> 14431cb0ef41Sopenharmony_ci</li> 14441cb0ef41Sopenharmony_ci</ul> 14451cb0ef41Sopenharmony_ci<p>This function is used to create a hook running after running a suite.</p> 14461cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 14471cb0ef41Sopenharmony_ci <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>)); 14481cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 14491cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 14501cb0ef41Sopenharmony_ci }); 14511cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 14521cb0ef41Sopenharmony_ci</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> 14531cb0ef41Sopenharmony_ci<div class="api_metadata"> 14541cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 14551cb0ef41Sopenharmony_ci</div> 14561cb0ef41Sopenharmony_ci<ul> 14571cb0ef41Sopenharmony_ci<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. 14581cb0ef41Sopenharmony_ciIf the hook uses callbacks, 14591cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 14601cb0ef41Sopenharmony_cifunction.</li> 14611cb0ef41Sopenharmony_ci<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 14621cb0ef41Sopenharmony_ciproperties are supported: 14631cb0ef41Sopenharmony_ci<ul> 14641cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 14651cb0ef41Sopenharmony_ci<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. 14661cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 14671cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 14681cb0ef41Sopenharmony_ci</ul> 14691cb0ef41Sopenharmony_ci</li> 14701cb0ef41Sopenharmony_ci</ul> 14711cb0ef41Sopenharmony_ci<p>This function is used to create a hook running 14721cb0ef41Sopenharmony_cibefore each subtest of the current suite.</p> 14731cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 14741cb0ef41Sopenharmony_ci <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>)); 14751cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 14761cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 14771cb0ef41Sopenharmony_ci }); 14781cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 14791cb0ef41Sopenharmony_ci</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> 14801cb0ef41Sopenharmony_ci<div class="api_metadata"> 14811cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 14821cb0ef41Sopenharmony_ci</div> 14831cb0ef41Sopenharmony_ci<ul> 14841cb0ef41Sopenharmony_ci<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. 14851cb0ef41Sopenharmony_ciIf the hook uses callbacks, 14861cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 14871cb0ef41Sopenharmony_cifunction.</li> 14881cb0ef41Sopenharmony_ci<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 14891cb0ef41Sopenharmony_ciproperties are supported: 14901cb0ef41Sopenharmony_ci<ul> 14911cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 14921cb0ef41Sopenharmony_ci<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. 14931cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 14941cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 14951cb0ef41Sopenharmony_ci</ul> 14961cb0ef41Sopenharmony_ci</li> 14971cb0ef41Sopenharmony_ci</ul> 14981cb0ef41Sopenharmony_ci<p>This function is used to create a hook running 14991cb0ef41Sopenharmony_ciafter each subtest of the current test.</p> 15001cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title function_">describe</span>(<span class="hljs-string">'tests'</span>, <span class="hljs-keyword">async</span> () => { 15011cb0ef41Sopenharmony_ci <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>)); 15021cb0ef41Sopenharmony_ci <span class="hljs-title function_">it</span>(<span class="hljs-string">'is a subtest'</span>, <span class="hljs-function">() =></span> { 15031cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 15041cb0ef41Sopenharmony_ci }); 15051cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 15061cb0ef41Sopenharmony_ci</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> 15071cb0ef41Sopenharmony_ci<div class="api_metadata"> 15081cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 15091cb0ef41Sopenharmony_ci</div> 15101cb0ef41Sopenharmony_ci<p>The <code>MockFunctionContext</code> class is used to inspect or manipulate the behavior of 15111cb0ef41Sopenharmony_cimocks created via the <a href="#class-mocktracker"><code>MockTracker</code></a> APIs.</p> 15121cb0ef41Sopenharmony_ci<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> 15131cb0ef41Sopenharmony_ci<div class="api_metadata"> 15141cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 15151cb0ef41Sopenharmony_ci</div> 15161cb0ef41Sopenharmony_ci<ul> 15171cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 15181cb0ef41Sopenharmony_ci</ul> 15191cb0ef41Sopenharmony_ci<p>A getter that returns a copy of the internal array used to track calls to the 15201cb0ef41Sopenharmony_cimock. Each entry in the array is an object with the following properties.</p> 15211cb0ef41Sopenharmony_ci<ul> 15221cb0ef41Sopenharmony_ci<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> 15231cb0ef41Sopenharmony_ci<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 15241cb0ef41Sopenharmony_cithrown value. <strong>Default:</strong> <code>undefined</code>.</li> 15251cb0ef41Sopenharmony_ci<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> 15261cb0ef41Sopenharmony_ci<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 15271cb0ef41Sopenharmony_cicallsite of the mocked function invocation.</li> 15281cb0ef41Sopenharmony_ci<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 15291cb0ef41Sopenharmony_cifield contains the class being constructed. Otherwise this will be 15301cb0ef41Sopenharmony_ci<code>undefined</code>.</li> 15311cb0ef41Sopenharmony_ci<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> 15321cb0ef41Sopenharmony_ci</ul> 15331cb0ef41Sopenharmony_ci<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> 15341cb0ef41Sopenharmony_ci<div class="api_metadata"> 15351cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 15361cb0ef41Sopenharmony_ci</div> 15371cb0ef41Sopenharmony_ci<ul> 15381cb0ef41Sopenharmony_ci<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> 15391cb0ef41Sopenharmony_ci</ul> 15401cb0ef41Sopenharmony_ci<p>This function returns the number of times that this mock has been invoked. This 15411cb0ef41Sopenharmony_cifunction is more efficient than checking <code>ctx.calls.length</code> because <code>ctx.calls</code> 15421cb0ef41Sopenharmony_ciis a getter that creates a copy of the internal call tracking array.</p> 15431cb0ef41Sopenharmony_ci<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> 15441cb0ef41Sopenharmony_ci<div class="api_metadata"> 15451cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 15461cb0ef41Sopenharmony_ci</div> 15471cb0ef41Sopenharmony_ci<ul> 15481cb0ef41Sopenharmony_ci<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 15491cb0ef41Sopenharmony_cimock's new implementation.</li> 15501cb0ef41Sopenharmony_ci</ul> 15511cb0ef41Sopenharmony_ci<p>This function is used to change the behavior of an existing mock.</p> 15521cb0ef41Sopenharmony_ci<p>The following example creates a mock function using <code>t.mock.fn()</code>, calls the 15531cb0ef41Sopenharmony_cimock function, and then changes the mock implementation to a different function.</p> 15541cb0ef41Sopenharmony_ci<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> { 15551cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>; 15561cb0ef41Sopenharmony_ci 15571cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) { 15581cb0ef41Sopenharmony_ci cnt++; 15591cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> cnt; 15601cb0ef41Sopenharmony_ci } 15611cb0ef41Sopenharmony_ci 15621cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) { 15631cb0ef41Sopenharmony_ci cnt += <span class="hljs-number">2</span>; 15641cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> cnt; 15651cb0ef41Sopenharmony_ci } 15661cb0ef41Sopenharmony_ci 15671cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne); 15681cb0ef41Sopenharmony_ci 15691cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">1</span>); 15701cb0ef41Sopenharmony_ci fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">mockImplementation</span>(addTwo); 15711cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">3</span>); 15721cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">5</span>); 15731cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 15741cb0ef41Sopenharmony_ci<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> 15751cb0ef41Sopenharmony_ci<div class="api_metadata"> 15761cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 15771cb0ef41Sopenharmony_ci</div> 15781cb0ef41Sopenharmony_ci<ul> 15791cb0ef41Sopenharmony_ci<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 15801cb0ef41Sopenharmony_cimock's implementation for the invocation number specified by <code>onCall</code>.</li> 15811cb0ef41Sopenharmony_ci<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 15821cb0ef41Sopenharmony_cithe specified invocation has already occurred then an exception is thrown. 15831cb0ef41Sopenharmony_ci<strong>Default:</strong> The number of the next invocation.</li> 15841cb0ef41Sopenharmony_ci</ul> 15851cb0ef41Sopenharmony_ci<p>This function is used to change the behavior of an existing mock for a single 15861cb0ef41Sopenharmony_ciinvocation. Once invocation <code>onCall</code> has occurred, the mock will revert to 15871cb0ef41Sopenharmony_ciwhatever behavior it would have used had <code>mockImplementationOnce()</code> not been 15881cb0ef41Sopenharmony_cicalled.</p> 15891cb0ef41Sopenharmony_ci<p>The following example creates a mock function using <code>t.mock.fn()</code>, calls the 15901cb0ef41Sopenharmony_cimock function, changes the mock implementation to a different function for the 15911cb0ef41Sopenharmony_cinext invocation, and then resumes its previous behavior.</p> 15921cb0ef41Sopenharmony_ci<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> { 15931cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>; 15941cb0ef41Sopenharmony_ci 15951cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) { 15961cb0ef41Sopenharmony_ci cnt++; 15971cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> cnt; 15981cb0ef41Sopenharmony_ci } 15991cb0ef41Sopenharmony_ci 16001cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) { 16011cb0ef41Sopenharmony_ci cnt += <span class="hljs-number">2</span>; 16021cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> cnt; 16031cb0ef41Sopenharmony_ci } 16041cb0ef41Sopenharmony_ci 16051cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(addOne); 16061cb0ef41Sopenharmony_ci 16071cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">1</span>); 16081cb0ef41Sopenharmony_ci fn.<span class="hljs-property">mock</span>.<span class="hljs-title function_">mockImplementationOnce</span>(addTwo); 16091cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">3</span>); 16101cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">4</span>); 16111cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 16121cb0ef41Sopenharmony_ci<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> 16131cb0ef41Sopenharmony_ci<div class="api_metadata"> 16141cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 16151cb0ef41Sopenharmony_ci</div> 16161cb0ef41Sopenharmony_ci<p>Resets the call history of the mock function.</p> 16171cb0ef41Sopenharmony_ci<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> 16181cb0ef41Sopenharmony_ci<div class="api_metadata"> 16191cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 16201cb0ef41Sopenharmony_ci</div> 16211cb0ef41Sopenharmony_ci<p>Resets the implementation of the mock function to its original behavior. The 16221cb0ef41Sopenharmony_cimock can still be used after calling this function.</p> 16231cb0ef41Sopenharmony_ci</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> 16241cb0ef41Sopenharmony_ci<div class="api_metadata"> 16251cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 16261cb0ef41Sopenharmony_ci</div> 16271cb0ef41Sopenharmony_ci<p>The <code>MockTracker</code> class is used to manage mocking functionality. The test runner 16281cb0ef41Sopenharmony_cimodule provides a top level <code>mock</code> export which is a <code>MockTracker</code> instance. 16291cb0ef41Sopenharmony_ciEach test also provides its own <code>MockTracker</code> instance via the test context's 16301cb0ef41Sopenharmony_ci<code>mock</code> property.</p> 16311cb0ef41Sopenharmony_ci<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> 16321cb0ef41Sopenharmony_ci<div class="api_metadata"> 16331cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 16341cb0ef41Sopenharmony_ci</div> 16351cb0ef41Sopenharmony_ci<ul> 16361cb0ef41Sopenharmony_ci<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. 16371cb0ef41Sopenharmony_ci<strong>Default:</strong> A no-op function.</li> 16381cb0ef41Sopenharmony_ci<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 16391cb0ef41Sopenharmony_cimock implementation for <code>original</code>. This is useful for creating mocks that 16401cb0ef41Sopenharmony_ciexhibit one behavior for a specified number of calls and then restore the 16411cb0ef41Sopenharmony_cibehavior of <code>original</code>. <strong>Default:</strong> The function specified by <code>original</code>.</li> 16421cb0ef41Sopenharmony_ci<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 16431cb0ef41Sopenharmony_cifollowing properties are supported: 16441cb0ef41Sopenharmony_ci<ul> 16451cb0ef41Sopenharmony_ci<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 16461cb0ef41Sopenharmony_ci<code>implementation</code>. Once the mock function has been called <code>times</code> times, it 16471cb0ef41Sopenharmony_ciwill automatically restore the behavior of <code>original</code>. This value must be an 16481cb0ef41Sopenharmony_ciinteger greater than zero. <strong>Default:</strong> <code>Infinity</code>.</li> 16491cb0ef41Sopenharmony_ci</ul> 16501cb0ef41Sopenharmony_ci</li> 16511cb0ef41Sopenharmony_ci<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 16521cb0ef41Sopenharmony_ci<code>mock</code> property, which is an instance of <a href="#class-mockfunctioncontext"><code>MockFunctionContext</code></a>, and can 16531cb0ef41Sopenharmony_cibe used for inspecting and changing the behavior of the mocked function.</li> 16541cb0ef41Sopenharmony_ci</ul> 16551cb0ef41Sopenharmony_ci<p>This function is used to create a mock function.</p> 16561cb0ef41Sopenharmony_ci<p>The following example creates a mock function that increments a counter by one 16571cb0ef41Sopenharmony_cion each invocation. The <code>times</code> option is used to modify the mock behavior such 16581cb0ef41Sopenharmony_cithat the first two invocations add two to the counter instead of one.</p> 16591cb0ef41Sopenharmony_ci<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> { 16601cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> cnt = <span class="hljs-number">0</span>; 16611cb0ef41Sopenharmony_ci 16621cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">addOne</span>(<span class="hljs-params"></span>) { 16631cb0ef41Sopenharmony_ci cnt++; 16641cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> cnt; 16651cb0ef41Sopenharmony_ci } 16661cb0ef41Sopenharmony_ci 16671cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params"></span>) { 16681cb0ef41Sopenharmony_ci cnt += <span class="hljs-number">2</span>; 16691cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> cnt; 16701cb0ef41Sopenharmony_ci } 16711cb0ef41Sopenharmony_ci 16721cb0ef41Sopenharmony_ci <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> }); 16731cb0ef41Sopenharmony_ci 16741cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">2</span>); 16751cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">4</span>); 16761cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">5</span>); 16771cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">fn</span>(), <span class="hljs-number">6</span>); 16781cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 16791cb0ef41Sopenharmony_ci<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> 16801cb0ef41Sopenharmony_ci<div class="api_metadata"> 16811cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 16821cb0ef41Sopenharmony_ci</div> 16831cb0ef41Sopenharmony_ci<p>This function is syntax sugar for <a href="#mockmethodobject-methodname-implementation-options"><code>MockTracker.method</code></a> with <code>options.getter</code> 16841cb0ef41Sopenharmony_ciset to <code>true</code>.</p> 16851cb0ef41Sopenharmony_ci<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> 16861cb0ef41Sopenharmony_ci<div class="api_metadata"> 16871cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 16881cb0ef41Sopenharmony_ci</div> 16891cb0ef41Sopenharmony_ci<ul> 16901cb0ef41Sopenharmony_ci<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> 16911cb0ef41Sopenharmony_ci<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. 16921cb0ef41Sopenharmony_ciIf <code>object[methodName]</code> is not a function, an error is thrown.</li> 16931cb0ef41Sopenharmony_ci<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 16941cb0ef41Sopenharmony_cimock implementation for <code>object[methodName]</code>. <strong>Default:</strong> The original method 16951cb0ef41Sopenharmony_cispecified by <code>object[methodName]</code>.</li> 16961cb0ef41Sopenharmony_ci<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 16971cb0ef41Sopenharmony_cifollowing properties are supported: 16981cb0ef41Sopenharmony_ci<ul> 16991cb0ef41Sopenharmony_ci<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. 17001cb0ef41Sopenharmony_ciThis option cannot be used with the <code>setter</code> option. <strong>Default:</strong> false.</li> 17011cb0ef41Sopenharmony_ci<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. 17021cb0ef41Sopenharmony_ciThis option cannot be used with the <code>getter</code> option. <strong>Default:</strong> false.</li> 17031cb0ef41Sopenharmony_ci<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 17041cb0ef41Sopenharmony_ci<code>implementation</code>. Once the mocked method has been called <code>times</code> times, it 17051cb0ef41Sopenharmony_ciwill automatically restore the original behavior. This value must be an 17061cb0ef41Sopenharmony_ciinteger greater than zero. <strong>Default:</strong> <code>Infinity</code>.</li> 17071cb0ef41Sopenharmony_ci</ul> 17081cb0ef41Sopenharmony_ci</li> 17091cb0ef41Sopenharmony_ci<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 17101cb0ef41Sopenharmony_ci<code>mock</code> property, which is an instance of <a href="#class-mockfunctioncontext"><code>MockFunctionContext</code></a>, and can 17111cb0ef41Sopenharmony_cibe used for inspecting and changing the behavior of the mocked method.</li> 17121cb0ef41Sopenharmony_ci</ul> 17131cb0ef41Sopenharmony_ci<p>This function is used to create a mock on an existing object method. The 17141cb0ef41Sopenharmony_cifollowing example demonstrates how a mock is created on an existing object 17151cb0ef41Sopenharmony_cimethod.</p> 17161cb0ef41Sopenharmony_ci<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> { 17171cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> number = { 17181cb0ef41Sopenharmony_ci <span class="hljs-attr">value</span>: <span class="hljs-number">5</span>, 17191cb0ef41Sopenharmony_ci <span class="hljs-title function_">subtract</span>(<span class="hljs-params">a</span>) { 17201cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> - a; 17211cb0ef41Sopenharmony_ci }, 17221cb0ef41Sopenharmony_ci }; 17231cb0ef41Sopenharmony_ci 17241cb0ef41Sopenharmony_ci t.<span class="hljs-property">mock</span>.<span class="hljs-title function_">method</span>(number, <span class="hljs-string">'subtract'</span>); 17251cb0ef41Sopenharmony_ci 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>); 17261cb0ef41Sopenharmony_ci 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>); 17271cb0ef41Sopenharmony_ci 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>); 17281cb0ef41Sopenharmony_ci 17291cb0ef41Sopenharmony_ci <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>]; 17301cb0ef41Sopenharmony_ci 17311cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">deepStrictEqual</span>(call.<span class="hljs-property">arguments</span>, [<span class="hljs-number">3</span>]); 17321cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">result</span>, <span class="hljs-number">2</span>); 17331cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">error</span>, <span class="hljs-literal">undefined</span>); 17341cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">target</span>, <span class="hljs-literal">undefined</span>); 17351cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(call.<span class="hljs-property">this</span>, number); 17361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 17371cb0ef41Sopenharmony_ci<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> 17381cb0ef41Sopenharmony_ci<div class="api_metadata"> 17391cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 17401cb0ef41Sopenharmony_ci</div> 17411cb0ef41Sopenharmony_ci<p>This function restores the default behavior of all mocks that were previously 17421cb0ef41Sopenharmony_cicreated by this <code>MockTracker</code> and disassociates the mocks from the 17431cb0ef41Sopenharmony_ci<code>MockTracker</code> instance. Once disassociated, the mocks can still be used, but the 17441cb0ef41Sopenharmony_ci<code>MockTracker</code> instance can no longer be used to reset their behavior or 17451cb0ef41Sopenharmony_ciotherwise interact with them.</p> 17461cb0ef41Sopenharmony_ci<p>After each test completes, this function is called on the test context's 17471cb0ef41Sopenharmony_ci<code>MockTracker</code>. If the global <code>MockTracker</code> is used extensively, calling this 17481cb0ef41Sopenharmony_cifunction manually is recommended.</p> 17491cb0ef41Sopenharmony_ci<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> 17501cb0ef41Sopenharmony_ci<div class="api_metadata"> 17511cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 17521cb0ef41Sopenharmony_ci</div> 17531cb0ef41Sopenharmony_ci<p>This function restores the default behavior of all mocks that were previously 17541cb0ef41Sopenharmony_cicreated by this <code>MockTracker</code>. Unlike <code>mock.reset()</code>, <code>mock.restoreAll()</code> does 17551cb0ef41Sopenharmony_cinot disassociate the mocks from the <code>MockTracker</code> instance.</p> 17561cb0ef41Sopenharmony_ci<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> 17571cb0ef41Sopenharmony_ci<div class="api_metadata"> 17581cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 17591cb0ef41Sopenharmony_ci</div> 17601cb0ef41Sopenharmony_ci<p>This function is syntax sugar for <a href="#mockmethodobject-methodname-implementation-options"><code>MockTracker.method</code></a> with <code>options.setter</code> 17611cb0ef41Sopenharmony_ciset to <code>true</code>.</p> 17621cb0ef41Sopenharmony_ci</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> 17631cb0ef41Sopenharmony_ci<div class="api_metadata"> 17641cb0ef41Sopenharmony_ci<span>Added in: v18.19.0</span> 17651cb0ef41Sopenharmony_ci</div> 17661cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 17671cb0ef41Sopenharmony_ci<p>Mocking timers is a technique commonly used in software testing to simulate and 17681cb0ef41Sopenharmony_cicontrol the behavior of timers, such as <code>setInterval</code> and <code>setTimeout</code>, 17691cb0ef41Sopenharmony_ciwithout actually waiting for the specified time intervals.</p> 17701cb0ef41Sopenharmony_ci<p>The <a href="#class-mocktracker"><code>MockTracker</code></a> provides a top-level <code>timers</code> export 17711cb0ef41Sopenharmony_ciwhich is a <code>MockTimers</code> instance.</p> 17721cb0ef41Sopenharmony_ci<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> 17731cb0ef41Sopenharmony_ci<div class="api_metadata"> 17741cb0ef41Sopenharmony_ci<span>Added in: v18.19.0</span> 17751cb0ef41Sopenharmony_ci</div> 17761cb0ef41Sopenharmony_ci<p>Enables timer mocking for the specified timers.</p> 17771cb0ef41Sopenharmony_ci<ul> 17781cb0ef41Sopenharmony_ci<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. 17791cb0ef41Sopenharmony_ciThe currently supported timer values are <code>'setInterval'</code>, <code>'setTimeout'</code>, 17801cb0ef41Sopenharmony_ciand <code>'setImmediate'</code>. If no value is provided, all timers (<code>'setInterval'</code>, 17811cb0ef41Sopenharmony_ci<code>'clearInterval'</code>, <code>'setTimeout'</code>, <code>'clearTimeout'</code>, <code>'setImmediate'</code>, 17821cb0ef41Sopenharmony_ciand <code>'clearImmediate'</code>) will be mocked by default.</li> 17831cb0ef41Sopenharmony_ci</ul> 17841cb0ef41Sopenharmony_ci<p><strong>Note:</strong> When you enable mocking for a specific timer, its associated 17851cb0ef41Sopenharmony_ciclear function will also be implicitly mocked.</p> 17861cb0ef41Sopenharmony_ci<p>Example usage:</p> 17871cb0ef41Sopenharmony_ci 17881cb0ef41Sopenharmony_ci<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>; 17891cb0ef41Sopenharmony_cimock.<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>); 17901cb0ef41Sopenharmony_cimock.<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> 17911cb0ef41Sopenharmony_ci<p>The above example enables mocking for the <code>setInterval</code> timer and 17921cb0ef41Sopenharmony_ciimplicitly mocks the <code>clearInterval</code> function. Only the <code>setInterval</code> 17931cb0ef41Sopenharmony_ciand <code>clearInterval</code> functions from <a href="./timers.html">node:timers</a>, 17941cb0ef41Sopenharmony_ci<a href="./timers.html#timers-promises-api">node:timers/promises</a>, and 17951cb0ef41Sopenharmony_ci<code>globalThis</code> will be mocked.</p> 17961cb0ef41Sopenharmony_ci<p>Alternatively, if you call <code>mock.timers.enable()</code> without any parameters:</p> 17971cb0ef41Sopenharmony_ci<p>All timers (<code>'setInterval'</code>, <code>'clearInterval'</code>, <code>'setTimeout'</code>, and <code>'clearTimeout'</code>) 17981cb0ef41Sopenharmony_ciwill be mocked. The <code>setInterval</code>, <code>clearInterval</code>, <code>setTimeout</code>, and <code>clearTimeout</code> 17991cb0ef41Sopenharmony_cifunctions from <code>node:timers</code>, <code>node:timers/promises</code>, 18001cb0ef41Sopenharmony_ciand <code>globalThis</code> will be mocked.</p> 18011cb0ef41Sopenharmony_ci<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> 18021cb0ef41Sopenharmony_ci<div class="api_metadata"> 18031cb0ef41Sopenharmony_ci<span>Added in: v18.19.0</span> 18041cb0ef41Sopenharmony_ci</div> 18051cb0ef41Sopenharmony_ci<p>This function restores the default behavior of all mocks that were previously 18061cb0ef41Sopenharmony_cicreated by this <code>MockTimers</code> instance and disassociates the mocks 18071cb0ef41Sopenharmony_cifrom the <code>MockTracker</code> instance.</p> 18081cb0ef41Sopenharmony_ci<p><strong>Note:</strong> After each test completes, this function is called on 18091cb0ef41Sopenharmony_cithe test context's <code>MockTracker</code>.</p> 18101cb0ef41Sopenharmony_ci 18111cb0ef41Sopenharmony_ci<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>; 18121cb0ef41Sopenharmony_cimock.<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>); 18131cb0ef41Sopenharmony_cimock.<span class="hljs-property">timers</span>.<span class="hljs-title function_">reset</span>();</code><button class="copy-button">copy</button></pre> 18141cb0ef41Sopenharmony_ci<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> 18151cb0ef41Sopenharmony_ci<p>Calls <code>timers.reset()</code>.</p> 18161cb0ef41Sopenharmony_ci<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> 18171cb0ef41Sopenharmony_ci<div class="api_metadata"> 18181cb0ef41Sopenharmony_ci<span>Added in: v18.19.0</span> 18191cb0ef41Sopenharmony_ci</div> 18201cb0ef41Sopenharmony_ci<p>Advances time for all mocked timers.</p> 18211cb0ef41Sopenharmony_ci<ul> 18221cb0ef41Sopenharmony_ci<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, 18231cb0ef41Sopenharmony_cito advance the timers.</li> 18241cb0ef41Sopenharmony_ci</ul> 18251cb0ef41Sopenharmony_ci<p><strong>Note:</strong> This diverges from how <code>setTimeout</code> in Node.js behaves and accepts 18261cb0ef41Sopenharmony_cionly positive numbers. In Node.js, <code>setTimeout</code> with negative numbers is 18271cb0ef41Sopenharmony_cionly supported for web compatibility reasons.</p> 18281cb0ef41Sopenharmony_ci<p>The following example mocks a <code>setTimeout</code> function and 18291cb0ef41Sopenharmony_ciby using <code>.tick</code> advances in 18301cb0ef41Sopenharmony_citime triggering all pending timers.</p> 18311cb0ef41Sopenharmony_ci 18321cb0ef41Sopenharmony_ci<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>; 18331cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 18341cb0ef41Sopenharmony_ci 18351cb0ef41Sopenharmony_ci<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> { 18361cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 18371cb0ef41Sopenharmony_ci 18381cb0ef41Sopenharmony_ci 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>]); 18391cb0ef41Sopenharmony_ci 18401cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 18411cb0ef41Sopenharmony_ci 18421cb0ef41Sopenharmony_ci 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>); 18431cb0ef41Sopenharmony_ci 18441cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 18451cb0ef41Sopenharmony_ci 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>); 18461cb0ef41Sopenharmony_ci 18471cb0ef41Sopenharmony_ci 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>); 18481cb0ef41Sopenharmony_ci});</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>); 18491cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 18501cb0ef41Sopenharmony_ci 18511cb0ef41Sopenharmony_ci<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> { 18521cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 18531cb0ef41Sopenharmony_ci 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>]); 18541cb0ef41Sopenharmony_ci 18551cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 18561cb0ef41Sopenharmony_ci 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>); 18571cb0ef41Sopenharmony_ci 18581cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 18591cb0ef41Sopenharmony_ci 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>); 18601cb0ef41Sopenharmony_ci 18611cb0ef41Sopenharmony_ci 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>); 18621cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 18631cb0ef41Sopenharmony_ci<p>Alternativelly, the <code>.tick</code> function can be called many times</p> 18641cb0ef41Sopenharmony_ci 18651cb0ef41Sopenharmony_ci<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>; 18661cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 18671cb0ef41Sopenharmony_ci 18681cb0ef41Sopenharmony_ci<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> { 18691cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 18701cb0ef41Sopenharmony_ci 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>]); 18711cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> nineSecs = <span class="hljs-number">9000</span>; 18721cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, nineSecs); 18731cb0ef41Sopenharmony_ci 18741cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> twoSeconds = <span class="hljs-number">3000</span>; 18751cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 18761cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 18771cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 18781cb0ef41Sopenharmony_ci 18791cb0ef41Sopenharmony_ci 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>); 18801cb0ef41Sopenharmony_ci});</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>); 18811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 18821cb0ef41Sopenharmony_ci 18831cb0ef41Sopenharmony_ci<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> { 18841cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 18851cb0ef41Sopenharmony_ci 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>]); 18861cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> nineSecs = <span class="hljs-number">9000</span>; 18871cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(fn, nineSecs); 18881cb0ef41Sopenharmony_ci 18891cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> twoSeconds = <span class="hljs-number">3000</span>; 18901cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 18911cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 18921cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(twoSeconds); 18931cb0ef41Sopenharmony_ci 18941cb0ef41Sopenharmony_ci 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>); 18951cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 18961cb0ef41Sopenharmony_ci<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> 18971cb0ef41Sopenharmony_ci<p>As mentioned, all clear functions from timers (<code>clearTimeout</code> and <code>clearInterval</code>) 18981cb0ef41Sopenharmony_ciare implicity mocked. Take a look at this example using <code>setTimeout</code>:</p> 18991cb0ef41Sopenharmony_ci 19001cb0ef41Sopenharmony_ci<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>; 19011cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 19021cb0ef41Sopenharmony_ci 19031cb0ef41Sopenharmony_ci<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> { 19041cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19051cb0ef41Sopenharmony_ci 19061cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 19071cb0ef41Sopenharmony_ci 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>]); 19081cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> id = <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 19091cb0ef41Sopenharmony_ci 19101cb0ef41Sopenharmony_ci <span class="hljs-comment">// Implicity mocked as well</span> 19111cb0ef41Sopenharmony_ci <span class="hljs-built_in">clearTimeout</span>(id); 19121cb0ef41Sopenharmony_ci 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>); 19131cb0ef41Sopenharmony_ci 19141cb0ef41Sopenharmony_ci <span class="hljs-comment">// As that setTimeout was cleared the mock function will never be called</span> 19151cb0ef41Sopenharmony_ci 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>); 19161cb0ef41Sopenharmony_ci});</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>); 19171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 19181cb0ef41Sopenharmony_ci 19191cb0ef41Sopenharmony_ci<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> { 19201cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fn = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19211cb0ef41Sopenharmony_ci 19221cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 19231cb0ef41Sopenharmony_ci 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>]); 19241cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> id = <span class="hljs-built_in">setTimeout</span>(fn, <span class="hljs-number">9999</span>); 19251cb0ef41Sopenharmony_ci 19261cb0ef41Sopenharmony_ci <span class="hljs-comment">// Implicity mocked as well</span> 19271cb0ef41Sopenharmony_ci <span class="hljs-built_in">clearTimeout</span>(id); 19281cb0ef41Sopenharmony_ci 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>); 19291cb0ef41Sopenharmony_ci 19301cb0ef41Sopenharmony_ci <span class="hljs-comment">// As that setTimeout was cleared the mock function will never be called</span> 19311cb0ef41Sopenharmony_ci 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>); 19321cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 19331cb0ef41Sopenharmony_ci<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> 19341cb0ef41Sopenharmony_ci<p>Once you enable mocking timers, <a href="./timers.html">node:timers</a>, 19351cb0ef41Sopenharmony_ci<a href="./timers.html#timers-promises-api">node:timers/promises</a> modules, 19361cb0ef41Sopenharmony_ciand timers from the Node.js global context are enabled:</p> 19371cb0ef41Sopenharmony_ci<p><strong>Note:</strong> Destructuring functions such as 19381cb0ef41Sopenharmony_ci<code>import { setTimeout } from 'node:timers'</code> is currently 19391cb0ef41Sopenharmony_cinot supported by this API.</p> 19401cb0ef41Sopenharmony_ci 19411cb0ef41Sopenharmony_ci<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>; 19421cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 19431cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> nodeTimers <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers'</span>; 19441cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> nodeTimersPromises <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>; 19451cb0ef41Sopenharmony_ci 19461cb0ef41Sopenharmony_ci<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) => { 19471cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> globalTimeoutObjectSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19481cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> nodeTimerSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19491cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> nodeTimerPromiseSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19501cb0ef41Sopenharmony_ci 19511cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 19521cb0ef41Sopenharmony_ci 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>]); 19531cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(globalTimeoutObjectSpy, <span class="hljs-number">9999</span>); 19541cb0ef41Sopenharmony_ci nodeTimers.<span class="hljs-built_in">setTimeout</span>(nodeTimerSpy, <span class="hljs-number">9999</span>); 19551cb0ef41Sopenharmony_ci 19561cb0ef41Sopenharmony_ci <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); 19571cb0ef41Sopenharmony_ci 19581cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 19591cb0ef41Sopenharmony_ci 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>); 19601cb0ef41Sopenharmony_ci 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>); 19611cb0ef41Sopenharmony_ci 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>); 19621cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> promise; 19631cb0ef41Sopenharmony_ci 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>); 19641cb0ef41Sopenharmony_ci});</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>); 19651cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 19661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> nodeTimers = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers'</span>); 19671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> nodeTimersPromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 19681cb0ef41Sopenharmony_ci 19691cb0ef41Sopenharmony_ci<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) => { 19701cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> globalTimeoutObjectSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19711cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> nodeTimerSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19721cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> nodeTimerPromiseSpy = context.<span class="hljs-property">mock</span>.<span class="hljs-title function_">fn</span>(); 19731cb0ef41Sopenharmony_ci 19741cb0ef41Sopenharmony_ci <span class="hljs-comment">// Optionally choose what to mock</span> 19751cb0ef41Sopenharmony_ci 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>]); 19761cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(globalTimeoutObjectSpy, <span class="hljs-number">9999</span>); 19771cb0ef41Sopenharmony_ci nodeTimers.<span class="hljs-built_in">setTimeout</span>(nodeTimerSpy, <span class="hljs-number">9999</span>); 19781cb0ef41Sopenharmony_ci 19791cb0ef41Sopenharmony_ci <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); 19801cb0ef41Sopenharmony_ci 19811cb0ef41Sopenharmony_ci <span class="hljs-comment">// Advance in time</span> 19821cb0ef41Sopenharmony_ci 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>); 19831cb0ef41Sopenharmony_ci 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>); 19841cb0ef41Sopenharmony_ci 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>); 19851cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> promise; 19861cb0ef41Sopenharmony_ci 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>); 19871cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 19881cb0ef41Sopenharmony_ci<p>In Node.js, <code>setInterval</code> from <a href="./timers.html#timers-promises-api">node:timers/promises</a> 19891cb0ef41Sopenharmony_ciis an <code>AsyncGenerator</code> and is also supported by this API:</p> 19901cb0ef41Sopenharmony_ci 19911cb0ef41Sopenharmony_ci<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>; 19921cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 19931cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> nodeTimersPromises <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>; 19941cb0ef41Sopenharmony_ci<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) => { 19951cb0ef41Sopenharmony_ci 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>]); 19961cb0ef41Sopenharmony_ci 19971cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> expectedIterations = <span class="hljs-number">3</span>; 19981cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> interval = <span class="hljs-number">1000</span>; 19991cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> startedAt = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 20001cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { 20011cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> times = []; 20021cb0ef41Sopenharmony_ci <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)) { 20031cb0ef41Sopenharmony_ci times.<span class="hljs-title function_">push</span>(time); 20041cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (times.<span class="hljs-property">length</span> === expectedIterations) <span class="hljs-keyword">break</span>; 20051cb0ef41Sopenharmony_ci } 20061cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> times; 20071cb0ef41Sopenharmony_ci } 20081cb0ef41Sopenharmony_ci 20091cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> r = <span class="hljs-title function_">run</span>(); 20101cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 20111cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 20121cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 20131cb0ef41Sopenharmony_ci 20141cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> timeResults = <span class="hljs-keyword">await</span> r; 20151cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(timeResults.<span class="hljs-property">length</span>, expectedIterations); 20161cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> it = <span class="hljs-number">1</span>; it < expectedIterations; it++) { 20171cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(timeResults[it - <span class="hljs-number">1</span>], startedAt + (interval * it)); 20181cb0ef41Sopenharmony_ci } 20191cb0ef41Sopenharmony_ci});</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>); 20201cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 20211cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> nodeTimersPromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 20221cb0ef41Sopenharmony_ci<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) => { 20231cb0ef41Sopenharmony_ci 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>]); 20241cb0ef41Sopenharmony_ci 20251cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> expectedIterations = <span class="hljs-number">3</span>; 20261cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> interval = <span class="hljs-number">1000</span>; 20271cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> startedAt = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 20281cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { 20291cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> times = []; 20301cb0ef41Sopenharmony_ci <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)) { 20311cb0ef41Sopenharmony_ci times.<span class="hljs-title function_">push</span>(time); 20321cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (times.<span class="hljs-property">length</span> === expectedIterations) <span class="hljs-keyword">break</span>; 20331cb0ef41Sopenharmony_ci } 20341cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> times; 20351cb0ef41Sopenharmony_ci } 20361cb0ef41Sopenharmony_ci 20371cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> r = <span class="hljs-title function_">run</span>(); 20381cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 20391cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 20401cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">tick</span>(interval); 20411cb0ef41Sopenharmony_ci 20421cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> timeResults = <span class="hljs-keyword">await</span> r; 20431cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(timeResults.<span class="hljs-property">length</span>, expectedIterations); 20441cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> it = <span class="hljs-number">1</span>; it < expectedIterations; it++) { 20451cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(timeResults[it - <span class="hljs-number">1</span>], startedAt + (interval * it)); 20461cb0ef41Sopenharmony_ci } 20471cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 20481cb0ef41Sopenharmony_ci<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> 20491cb0ef41Sopenharmony_ci<div class="api_metadata"> 20501cb0ef41Sopenharmony_ci<span>Added in: v18.19.0</span> 20511cb0ef41Sopenharmony_ci</div> 20521cb0ef41Sopenharmony_ci<p>Triggers all pending mocked timers immediately.</p> 20531cb0ef41Sopenharmony_ci<p>The example below triggers all pending timers immediately, 20541cb0ef41Sopenharmony_cicausing them to execute without any delay.</p> 20551cb0ef41Sopenharmony_ci 20561cb0ef41Sopenharmony_ci<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>; 20571cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { test } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:test'</span>; 20581cb0ef41Sopenharmony_ci 20591cb0ef41Sopenharmony_ci<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> { 20601cb0ef41Sopenharmony_ci 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>]); 20611cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> results = []; 20621cb0ef41Sopenharmony_ci <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>); 20631cb0ef41Sopenharmony_ci 20641cb0ef41Sopenharmony_ci <span class="hljs-comment">// Notice that if both timers have the same timeout,</span> 20651cb0ef41Sopenharmony_ci <span class="hljs-comment">// the order of execution is guaranteed</span> 20661cb0ef41Sopenharmony_ci <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>); 20671cb0ef41Sopenharmony_ci <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>); 20681cb0ef41Sopenharmony_ci 20691cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []); 20701cb0ef41Sopenharmony_ci 20711cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>(); 20721cb0ef41Sopenharmony_ci 20731cb0ef41Sopenharmony_ci 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>]); 20741cb0ef41Sopenharmony_ci});</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>); 20751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { test } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:test'</span>); 20761cb0ef41Sopenharmony_ci 20771cb0ef41Sopenharmony_ci<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> { 20781cb0ef41Sopenharmony_ci 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>]); 20791cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> results = []; 20801cb0ef41Sopenharmony_ci <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>); 20811cb0ef41Sopenharmony_ci 20821cb0ef41Sopenharmony_ci <span class="hljs-comment">// Notice that if both timers have the same timeout,</span> 20831cb0ef41Sopenharmony_ci <span class="hljs-comment">// the order of execution is guaranteed</span> 20841cb0ef41Sopenharmony_ci <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>); 20851cb0ef41Sopenharmony_ci <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>); 20861cb0ef41Sopenharmony_ci 20871cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">deepStrictEqual</span>(results, []); 20881cb0ef41Sopenharmony_ci 20891cb0ef41Sopenharmony_ci context.<span class="hljs-property">mock</span>.<span class="hljs-property">timers</span>.<span class="hljs-title function_">runAll</span>(); 20901cb0ef41Sopenharmony_ci 20911cb0ef41Sopenharmony_ci 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>]); 20921cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 20931cb0ef41Sopenharmony_ci<p><strong>Note:</strong> The <code>runAll()</code> function is specifically designed for 20941cb0ef41Sopenharmony_citriggering timers in the context of timer mocking. 20951cb0ef41Sopenharmony_ciIt does not have any effect on real-time system 20961cb0ef41Sopenharmony_ciclocks or actual timers outside of the mocking environment.</p> 20971cb0ef41Sopenharmony_ci</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> 20981cb0ef41Sopenharmony_ci<div class="api_metadata"> 20991cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21001cb0ef41Sopenharmony_ci<table> 21011cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21021cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 21031cb0ef41Sopenharmony_ci<td><p>added type to test:pass and test:fail events for when the test is a suite.</p></td></tr> 21041cb0ef41Sopenharmony_ci<tr><td>v18.9.0, v16.19.0</td> 21051cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.9.0, v16.19.0</span></p></td></tr> 21061cb0ef41Sopenharmony_ci</tbody></table> 21071cb0ef41Sopenharmony_ci</details> 21081cb0ef41Sopenharmony_ci</div> 21091cb0ef41Sopenharmony_ci<ul> 21101cb0ef41Sopenharmony_ci<li>Extends <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 21111cb0ef41Sopenharmony_ci</ul> 21121cb0ef41Sopenharmony_ci<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> 21131cb0ef41Sopenharmony_ciobject, streaming a series of events representing the execution of the tests. 21141cb0ef41Sopenharmony_ci<code>TestsStream</code> will emit events, in the order of the tests definition</p> 21151cb0ef41Sopenharmony_ci<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> 21161cb0ef41Sopenharmony_ci<ul> 21171cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 21181cb0ef41Sopenharmony_ci<ul> 21191cb0ef41Sopenharmony_ci<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. 21201cb0ef41Sopenharmony_ci<ul> 21211cb0ef41Sopenharmony_ci<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 21221cb0ef41Sopenharmony_cireport is an object with the following schema: 21231cb0ef41Sopenharmony_ci<ul> 21241cb0ef41Sopenharmony_ci<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> 21251cb0ef41Sopenharmony_ci<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> 21261cb0ef41Sopenharmony_ci<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> 21271cb0ef41Sopenharmony_ci<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> 21281cb0ef41Sopenharmony_ci<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> 21291cb0ef41Sopenharmony_ci<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> 21301cb0ef41Sopenharmony_ci<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> 21311cb0ef41Sopenharmony_ci<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> 21321cb0ef41Sopenharmony_ci<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> 21331cb0ef41Sopenharmony_ci<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> 21341cb0ef41Sopenharmony_ci<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 21351cb0ef41Sopenharmony_cinumbers that are uncovered.</li> 21361cb0ef41Sopenharmony_ci</ul> 21371cb0ef41Sopenharmony_ci</li> 21381cb0ef41Sopenharmony_ci<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 21391cb0ef41Sopenharmony_cifiles. 21401cb0ef41Sopenharmony_ci<ul> 21411cb0ef41Sopenharmony_ci<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> 21421cb0ef41Sopenharmony_ci<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> 21431cb0ef41Sopenharmony_ci<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> 21441cb0ef41Sopenharmony_ci<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> 21451cb0ef41Sopenharmony_ci<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> 21461cb0ef41Sopenharmony_ci<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> 21471cb0ef41Sopenharmony_ci<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> 21481cb0ef41Sopenharmony_ci<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> 21491cb0ef41Sopenharmony_ci<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> 21501cb0ef41Sopenharmony_ci</ul> 21511cb0ef41Sopenharmony_ci</li> 21521cb0ef41Sopenharmony_ci<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 21531cb0ef41Sopenharmony_cibegan. This is useful for displaying relative path names in case the tests 21541cb0ef41Sopenharmony_cichanged the working directory of the Node.js process.</li> 21551cb0ef41Sopenharmony_ci</ul> 21561cb0ef41Sopenharmony_ci</li> 21571cb0ef41Sopenharmony_ci<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> 21581cb0ef41Sopenharmony_ci</ul> 21591cb0ef41Sopenharmony_ci</li> 21601cb0ef41Sopenharmony_ci</ul> 21611cb0ef41Sopenharmony_ci<p>Emitted when code coverage is enabled and all tests have completed.</p> 21621cb0ef41Sopenharmony_ci<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> 21631cb0ef41Sopenharmony_ci<ul> 21641cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 21651cb0ef41Sopenharmony_ci<ul> 21661cb0ef41Sopenharmony_ci<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 21671cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 21681cb0ef41Sopenharmony_ci<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, 21691cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 21701cb0ef41Sopenharmony_ci<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 21711cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 21721cb0ef41Sopenharmony_ci<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> 21731cb0ef41Sopenharmony_ci<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> 21741cb0ef41Sopenharmony_ci</ul> 21751cb0ef41Sopenharmony_ci</li> 21761cb0ef41Sopenharmony_ci</ul> 21771cb0ef41Sopenharmony_ci<p>Emitted when a test is dequeued, right before it is executed.</p> 21781cb0ef41Sopenharmony_ci<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> 21791cb0ef41Sopenharmony_ci<ul> 21801cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 21811cb0ef41Sopenharmony_ci<ul> 21821cb0ef41Sopenharmony_ci<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 21831cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 21841cb0ef41Sopenharmony_ci<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, 21851cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 21861cb0ef41Sopenharmony_ci<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 21871cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 21881cb0ef41Sopenharmony_ci<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> 21891cb0ef41Sopenharmony_ci<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> 21901cb0ef41Sopenharmony_ci</ul> 21911cb0ef41Sopenharmony_ci</li> 21921cb0ef41Sopenharmony_ci</ul> 21931cb0ef41Sopenharmony_ci<p>Emitted when <a href="#contextdiagnosticmessage"><code>context.diagnostic</code></a> is called.</p> 21941cb0ef41Sopenharmony_ci<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> 21951cb0ef41Sopenharmony_ci<ul> 21961cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 21971cb0ef41Sopenharmony_ci<ul> 21981cb0ef41Sopenharmony_ci<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 21991cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22001cb0ef41Sopenharmony_ci<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, 22011cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 22021cb0ef41Sopenharmony_ci<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 22031cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22041cb0ef41Sopenharmony_ci<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> 22051cb0ef41Sopenharmony_ci<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> 22061cb0ef41Sopenharmony_ci</ul> 22071cb0ef41Sopenharmony_ci</li> 22081cb0ef41Sopenharmony_ci</ul> 22091cb0ef41Sopenharmony_ci<p>Emitted when a test is enqueued for execution.</p> 22101cb0ef41Sopenharmony_ci<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> 22111cb0ef41Sopenharmony_ci<ul> 22121cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 22131cb0ef41Sopenharmony_ci<ul> 22141cb0ef41Sopenharmony_ci<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 22151cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22161cb0ef41Sopenharmony_ci<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. 22171cb0ef41Sopenharmony_ci<ul> 22181cb0ef41Sopenharmony_ci<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> 22191cb0ef41Sopenharmony_ci<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. 22201cb0ef41Sopenharmony_ci<ul> 22211cb0ef41Sopenharmony_ci<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> 22221cb0ef41Sopenharmony_ci</ul> 22231cb0ef41Sopenharmony_ci</li> 22241cb0ef41Sopenharmony_ci<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 22251cb0ef41Sopenharmony_cithis is a suite.</li> 22261cb0ef41Sopenharmony_ci</ul> 22271cb0ef41Sopenharmony_ci</li> 22281cb0ef41Sopenharmony_ci<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, 22291cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 22301cb0ef41Sopenharmony_ci<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 22311cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22321cb0ef41Sopenharmony_ci<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> 22331cb0ef41Sopenharmony_ci<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> 22341cb0ef41Sopenharmony_ci<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> 22351cb0ef41Sopenharmony_ci<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> 22361cb0ef41Sopenharmony_ci<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> 22371cb0ef41Sopenharmony_ci</ul> 22381cb0ef41Sopenharmony_ci</li> 22391cb0ef41Sopenharmony_ci</ul> 22401cb0ef41Sopenharmony_ci<p>Emitted when a test fails.</p> 22411cb0ef41Sopenharmony_ci<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> 22421cb0ef41Sopenharmony_ci<ul> 22431cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 22441cb0ef41Sopenharmony_ci<ul> 22451cb0ef41Sopenharmony_ci<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 22461cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22471cb0ef41Sopenharmony_ci<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. 22481cb0ef41Sopenharmony_ci<ul> 22491cb0ef41Sopenharmony_ci<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> 22501cb0ef41Sopenharmony_ci<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 22511cb0ef41Sopenharmony_cithis is a suite.</li> 22521cb0ef41Sopenharmony_ci</ul> 22531cb0ef41Sopenharmony_ci</li> 22541cb0ef41Sopenharmony_ci<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, 22551cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 22561cb0ef41Sopenharmony_ci<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 22571cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22581cb0ef41Sopenharmony_ci<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> 22591cb0ef41Sopenharmony_ci<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> 22601cb0ef41Sopenharmony_ci<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> 22611cb0ef41Sopenharmony_ci<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> 22621cb0ef41Sopenharmony_ci<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> 22631cb0ef41Sopenharmony_ci</ul> 22641cb0ef41Sopenharmony_ci</li> 22651cb0ef41Sopenharmony_ci</ul> 22661cb0ef41Sopenharmony_ci<p>Emitted when a test passes.</p> 22671cb0ef41Sopenharmony_ci<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> 22681cb0ef41Sopenharmony_ci<ul> 22691cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 22701cb0ef41Sopenharmony_ci<ul> 22711cb0ef41Sopenharmony_ci<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 22721cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22731cb0ef41Sopenharmony_ci<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, 22741cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 22751cb0ef41Sopenharmony_ci<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 22761cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22771cb0ef41Sopenharmony_ci<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> 22781cb0ef41Sopenharmony_ci<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> 22791cb0ef41Sopenharmony_ci</ul> 22801cb0ef41Sopenharmony_ci</li> 22811cb0ef41Sopenharmony_ci</ul> 22821cb0ef41Sopenharmony_ci<p>Emitted when all subtests have completed for a given test.</p> 22831cb0ef41Sopenharmony_ci<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> 22841cb0ef41Sopenharmony_ci<ul> 22851cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 22861cb0ef41Sopenharmony_ci<ul> 22871cb0ef41Sopenharmony_ci<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 22881cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22891cb0ef41Sopenharmony_ci<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, 22901cb0ef41Sopenharmony_ci<code>undefined</code> if test was run through the REPL.</li> 22911cb0ef41Sopenharmony_ci<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 22921cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 22931cb0ef41Sopenharmony_ci<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> 22941cb0ef41Sopenharmony_ci<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> 22951cb0ef41Sopenharmony_ci</ul> 22961cb0ef41Sopenharmony_ci</li> 22971cb0ef41Sopenharmony_ci</ul> 22981cb0ef41Sopenharmony_ci<p>Emitted when a test starts reporting its own and its subtests status. 22991cb0ef41Sopenharmony_ciThis event is guaranteed to be emitted in the same order as the tests are 23001cb0ef41Sopenharmony_cidefined.</p> 23011cb0ef41Sopenharmony_ci<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> 23021cb0ef41Sopenharmony_ci<ul> 23031cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 23041cb0ef41Sopenharmony_ci<ul> 23051cb0ef41Sopenharmony_ci<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 23061cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 23071cb0ef41Sopenharmony_ci<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> 23081cb0ef41Sopenharmony_ci<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 23091cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 23101cb0ef41Sopenharmony_ci<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> 23111cb0ef41Sopenharmony_ci</ul> 23121cb0ef41Sopenharmony_ci</li> 23131cb0ef41Sopenharmony_ci</ul> 23141cb0ef41Sopenharmony_ci<p>Emitted when a running test writes to <code>stderr</code>. 23151cb0ef41Sopenharmony_ciThis event is only emitted if <code>--test</code> flag is passed.</p> 23161cb0ef41Sopenharmony_ci<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> 23171cb0ef41Sopenharmony_ci<ul> 23181cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 23191cb0ef41Sopenharmony_ci<ul> 23201cb0ef41Sopenharmony_ci<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 23211cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 23221cb0ef41Sopenharmony_ci<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> 23231cb0ef41Sopenharmony_ci<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 23241cb0ef41Sopenharmony_ci<code>undefined</code> if the test was run through the REPL.</li> 23251cb0ef41Sopenharmony_ci<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> 23261cb0ef41Sopenharmony_ci</ul> 23271cb0ef41Sopenharmony_ci</li> 23281cb0ef41Sopenharmony_ci</ul> 23291cb0ef41Sopenharmony_ci<p>Emitted when a running test writes to <code>stdout</code>. 23301cb0ef41Sopenharmony_ciThis event is only emitted if <code>--test</code> flag is passed.</p> 23311cb0ef41Sopenharmony_ci<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> 23321cb0ef41Sopenharmony_ci<p>Emitted when no more tests are queued for execution in watch mode.</p> 23331cb0ef41Sopenharmony_ci</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> 23341cb0ef41Sopenharmony_ci<div class="api_metadata"> 23351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 23361cb0ef41Sopenharmony_ci<table> 23371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 23381cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 23391cb0ef41Sopenharmony_ci<td><p>The <code>before</code> function was added to TestContext.</p></td></tr> 23401cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 23411cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.0.0</span></p></td></tr> 23421cb0ef41Sopenharmony_ci</tbody></table> 23431cb0ef41Sopenharmony_ci</details> 23441cb0ef41Sopenharmony_ci</div> 23451cb0ef41Sopenharmony_ci<p>An instance of <code>TestContext</code> is passed to each test function in order to 23461cb0ef41Sopenharmony_ciinteract with the test runner. However, the <code>TestContext</code> constructor is not 23471cb0ef41Sopenharmony_ciexposed as part of the API.</p> 23481cb0ef41Sopenharmony_ci<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> 23491cb0ef41Sopenharmony_ci<div class="api_metadata"> 23501cb0ef41Sopenharmony_ci<span>Added in: v18.17.0</span> 23511cb0ef41Sopenharmony_ci</div> 23521cb0ef41Sopenharmony_ci<ul> 23531cb0ef41Sopenharmony_ci<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 23541cb0ef41Sopenharmony_cito this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 23551cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 23561cb0ef41Sopenharmony_cifunction.</li> 23571cb0ef41Sopenharmony_ci<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 23581cb0ef41Sopenharmony_ciproperties are supported: 23591cb0ef41Sopenharmony_ci<ul> 23601cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 23611cb0ef41Sopenharmony_ci<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. 23621cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 23631cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 23641cb0ef41Sopenharmony_ci</ul> 23651cb0ef41Sopenharmony_ci</li> 23661cb0ef41Sopenharmony_ci</ul> 23671cb0ef41Sopenharmony_ci<p>This function is used to create a hook running before 23681cb0ef41Sopenharmony_cisubtest of the current test.</p> 23691cb0ef41Sopenharmony_ci<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> 23701cb0ef41Sopenharmony_ci<div class="api_metadata"> 23711cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 23721cb0ef41Sopenharmony_ci</div> 23731cb0ef41Sopenharmony_ci<ul> 23741cb0ef41Sopenharmony_ci<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 23751cb0ef41Sopenharmony_cito this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 23761cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 23771cb0ef41Sopenharmony_cifunction.</li> 23781cb0ef41Sopenharmony_ci<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 23791cb0ef41Sopenharmony_ciproperties are supported: 23801cb0ef41Sopenharmony_ci<ul> 23811cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 23821cb0ef41Sopenharmony_ci<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. 23831cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 23841cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 23851cb0ef41Sopenharmony_ci</ul> 23861cb0ef41Sopenharmony_ci</li> 23871cb0ef41Sopenharmony_ci</ul> 23881cb0ef41Sopenharmony_ci<p>This function is used to create a hook running 23891cb0ef41Sopenharmony_cibefore each subtest of the current test.</p> 23901cb0ef41Sopenharmony_ci<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) => { 23911cb0ef41Sopenharmony_ci 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>)); 23921cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>( 23931cb0ef41Sopenharmony_ci <span class="hljs-string">'This is a subtest'</span>, 23941cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> { 23951cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 23961cb0ef41Sopenharmony_ci }, 23971cb0ef41Sopenharmony_ci ); 23981cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 23991cb0ef41Sopenharmony_ci<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> 24001cb0ef41Sopenharmony_ci<div class="api_metadata"> 24011cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 24021cb0ef41Sopenharmony_ci</div> 24031cb0ef41Sopenharmony_ci<ul> 24041cb0ef41Sopenharmony_ci<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 24051cb0ef41Sopenharmony_cito this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 24061cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 24071cb0ef41Sopenharmony_cifunction.</li> 24081cb0ef41Sopenharmony_ci<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 24091cb0ef41Sopenharmony_ciproperties are supported: 24101cb0ef41Sopenharmony_ci<ul> 24111cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 24121cb0ef41Sopenharmony_ci<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. 24131cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 24141cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 24151cb0ef41Sopenharmony_ci</ul> 24161cb0ef41Sopenharmony_ci</li> 24171cb0ef41Sopenharmony_ci</ul> 24181cb0ef41Sopenharmony_ci<p>This function is used to create a hook that runs after the current test 24191cb0ef41Sopenharmony_cifinishes.</p> 24201cb0ef41Sopenharmony_ci<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) => { 24211cb0ef41Sopenharmony_ci 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>)); 24221cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 24231cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 24241cb0ef41Sopenharmony_ci<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> 24251cb0ef41Sopenharmony_ci<div class="api_metadata"> 24261cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 24271cb0ef41Sopenharmony_ci</div> 24281cb0ef41Sopenharmony_ci<ul> 24291cb0ef41Sopenharmony_ci<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 24301cb0ef41Sopenharmony_cito this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the hook uses callbacks, 24311cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 24321cb0ef41Sopenharmony_cifunction.</li> 24331cb0ef41Sopenharmony_ci<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 24341cb0ef41Sopenharmony_ciproperties are supported: 24351cb0ef41Sopenharmony_ci<ul> 24361cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress hook.</li> 24371cb0ef41Sopenharmony_ci<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. 24381cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 24391cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 24401cb0ef41Sopenharmony_ci</ul> 24411cb0ef41Sopenharmony_ci</li> 24421cb0ef41Sopenharmony_ci</ul> 24431cb0ef41Sopenharmony_ci<p>This function is used to create a hook running 24441cb0ef41Sopenharmony_ciafter each subtest of the current test.</p> 24451cb0ef41Sopenharmony_ci<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) => { 24461cb0ef41Sopenharmony_ci 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>)); 24471cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>( 24481cb0ef41Sopenharmony_ci <span class="hljs-string">'This is a subtest'</span>, 24491cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> { 24501cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 24511cb0ef41Sopenharmony_ci }, 24521cb0ef41Sopenharmony_ci ); 24531cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 24541cb0ef41Sopenharmony_ci<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> 24551cb0ef41Sopenharmony_ci<div class="api_metadata"> 24561cb0ef41Sopenharmony_ci<span>Added in: v18.0.0</span> 24571cb0ef41Sopenharmony_ci</div> 24581cb0ef41Sopenharmony_ci<ul> 24591cb0ef41Sopenharmony_ci<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> 24601cb0ef41Sopenharmony_ci</ul> 24611cb0ef41Sopenharmony_ci<p>This function is used to write diagnostics to the output. Any diagnostic 24621cb0ef41Sopenharmony_ciinformation is included at the end of the test's results. This function does 24631cb0ef41Sopenharmony_cinot return a value.</p> 24641cb0ef41Sopenharmony_ci<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> { 24651cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">diagnostic</span>(<span class="hljs-string">'A diagnostic message'</span>); 24661cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 24671cb0ef41Sopenharmony_ci<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> 24681cb0ef41Sopenharmony_ci<div class="api_metadata"> 24691cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 24701cb0ef41Sopenharmony_ci</div> 24711cb0ef41Sopenharmony_ci<p>The name of the test.</p> 24721cb0ef41Sopenharmony_ci<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> 24731cb0ef41Sopenharmony_ci<div class="api_metadata"> 24741cb0ef41Sopenharmony_ci<span>Added in: v18.0.0</span> 24751cb0ef41Sopenharmony_ci</div> 24761cb0ef41Sopenharmony_ci<ul> 24771cb0ef41Sopenharmony_ci<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> 24781cb0ef41Sopenharmony_ci</ul> 24791cb0ef41Sopenharmony_ci<p>If <code>shouldRunOnlyTests</code> is truthy, the test context will only run tests that 24801cb0ef41Sopenharmony_cihave the <code>only</code> option set. Otherwise, all tests are run. If Node.js was not 24811cb0ef41Sopenharmony_cistarted with the <a href="cli.html#--test-only"><code>--test-only</code></a> command-line option, this function is a 24821cb0ef41Sopenharmony_cino-op.</p> 24831cb0ef41Sopenharmony_ci<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> { 24841cb0ef41Sopenharmony_ci <span class="hljs-comment">// The test context can be set to run subtests with the 'only' option.</span> 24851cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">runOnly</span>(<span class="hljs-literal">true</span>); 24861cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">all</span>([ 24871cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">test</span>(<span class="hljs-string">'this subtest is now skipped'</span>), 24881cb0ef41Sopenharmony_ci 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> }), 24891cb0ef41Sopenharmony_ci ]); 24901cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 24911cb0ef41Sopenharmony_ci<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> 24921cb0ef41Sopenharmony_ci<div class="api_metadata"> 24931cb0ef41Sopenharmony_ci<span>Added in: v18.7.0</span> 24941cb0ef41Sopenharmony_ci</div> 24951cb0ef41Sopenharmony_ci<ul> 24961cb0ef41Sopenharmony_ci<li><a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to abort test subtasks when the test has been 24971cb0ef41Sopenharmony_ciaborted.</li> 24981cb0ef41Sopenharmony_ci</ul> 24991cb0ef41Sopenharmony_ci<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) => { 25001cb0ef41Sopenharmony_ci <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> }); 25011cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 25021cb0ef41Sopenharmony_ci<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> 25031cb0ef41Sopenharmony_ci<div class="api_metadata"> 25041cb0ef41Sopenharmony_ci<span>Added in: v18.0.0</span> 25051cb0ef41Sopenharmony_ci</div> 25061cb0ef41Sopenharmony_ci<ul> 25071cb0ef41Sopenharmony_ci<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> 25081cb0ef41Sopenharmony_ci</ul> 25091cb0ef41Sopenharmony_ci<p>This function causes the test's output to indicate the test as skipped. If 25101cb0ef41Sopenharmony_ci<code>message</code> is provided, it is included in the output. Calling <code>skip()</code> does 25111cb0ef41Sopenharmony_cinot terminate execution of the test function. This function does not return a 25121cb0ef41Sopenharmony_civalue.</p> 25131cb0ef41Sopenharmony_ci<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> { 25141cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make sure to return here as well if the test contains additional logic.</span> 25151cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">skip</span>(<span class="hljs-string">'this is skipped'</span>); 25161cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 25171cb0ef41Sopenharmony_ci<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> 25181cb0ef41Sopenharmony_ci<div class="api_metadata"> 25191cb0ef41Sopenharmony_ci<span>Added in: v18.0.0</span> 25201cb0ef41Sopenharmony_ci</div> 25211cb0ef41Sopenharmony_ci<ul> 25221cb0ef41Sopenharmony_ci<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> 25231cb0ef41Sopenharmony_ci</ul> 25241cb0ef41Sopenharmony_ci<p>This function adds a <code>TODO</code> directive to the test's output. If <code>message</code> is 25251cb0ef41Sopenharmony_ciprovided, it is included in the output. Calling <code>todo()</code> does not terminate 25261cb0ef41Sopenharmony_ciexecution of the test function. This function does not return a value.</p> 25271cb0ef41Sopenharmony_ci<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> { 25281cb0ef41Sopenharmony_ci <span class="hljs-comment">// This test is marked as `TODO`</span> 25291cb0ef41Sopenharmony_ci t.<span class="hljs-title function_">todo</span>(<span class="hljs-string">'this is a todo'</span>); 25301cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 25311cb0ef41Sopenharmony_ci<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> 25321cb0ef41Sopenharmony_ci<div class="api_metadata"> 25331cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 25341cb0ef41Sopenharmony_ci<table> 25351cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 25361cb0ef41Sopenharmony_ci<tr><td>v18.8.0</td> 25371cb0ef41Sopenharmony_ci<td><p>Add a <code>signal</code> option.</p></td></tr> 25381cb0ef41Sopenharmony_ci<tr><td>v18.7.0</td> 25391cb0ef41Sopenharmony_ci<td><p>Add a <code>timeout</code> option.</p></td></tr> 25401cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 25411cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.0.0</span></p></td></tr> 25421cb0ef41Sopenharmony_ci</tbody></table> 25431cb0ef41Sopenharmony_ci</details> 25441cb0ef41Sopenharmony_ci</div> 25451cb0ef41Sopenharmony_ci<ul> 25461cb0ef41Sopenharmony_ci<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 25471cb0ef41Sopenharmony_citest results. <strong>Default:</strong> The <code>name</code> property of <code>fn</code>, or <code>'<anonymous>'</code> if 25481cb0ef41Sopenharmony_ci<code>fn</code> does not have a name.</li> 25491cb0ef41Sopenharmony_ci<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 25501cb0ef41Sopenharmony_ciproperties are supported: 25511cb0ef41Sopenharmony_ci<ul> 25521cb0ef41Sopenharmony_ci<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, 25531cb0ef41Sopenharmony_cithen that many tests would run in parallel within the application thread. 25541cb0ef41Sopenharmony_ciIf <code>true</code>, it would run all subtests in parallel. 25551cb0ef41Sopenharmony_ciIf <code>false</code>, it would only run one test at a time. 25561cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 25571cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>null</code>.</li> 25581cb0ef41Sopenharmony_ci<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 25591cb0ef41Sopenharmony_ci<code>only</code> tests, then this test will be run. Otherwise, the test is skipped. 25601cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 25611cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows aborting an in-progress test.</li> 25621cb0ef41Sopenharmony_ci<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 25631cb0ef41Sopenharmony_ciprovided, that string is displayed in the test results as the reason for 25641cb0ef41Sopenharmony_ciskipping the test. <strong>Default:</strong> <code>false</code>.</li> 25651cb0ef41Sopenharmony_ci<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 25661cb0ef41Sopenharmony_ciis provided, that string is displayed in the test results as the reason why 25671cb0ef41Sopenharmony_cithe test is <code>TODO</code>. <strong>Default:</strong> <code>false</code>.</li> 25681cb0ef41Sopenharmony_ci<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. 25691cb0ef41Sopenharmony_ciIf unspecified, subtests inherit this value from their parent. 25701cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 25711cb0ef41Sopenharmony_ci</ul> 25721cb0ef41Sopenharmony_ci</li> 25731cb0ef41Sopenharmony_ci<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 25741cb0ef41Sopenharmony_cito this function is a <a href="#class-testcontext"><code>TestContext</code></a> object. If the test uses callbacks, 25751cb0ef41Sopenharmony_cithe callback function is passed as the second argument. <strong>Default:</strong> A no-op 25761cb0ef41Sopenharmony_cifunction.</li> 25771cb0ef41Sopenharmony_ci<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> 25781cb0ef41Sopenharmony_ci</ul> 25791cb0ef41Sopenharmony_ci<p>This function is used to create subtests under the current test. This function 25801cb0ef41Sopenharmony_cibehaves in the same fashion as the top level <a href="#testname-options-fn"><code>test()</code></a> function.</p> 25811cb0ef41Sopenharmony_ci<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) => { 25821cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> t.<span class="hljs-title function_">test</span>( 25831cb0ef41Sopenharmony_ci <span class="hljs-string">'This is a subtest'</span>, 25841cb0ef41Sopenharmony_ci { <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> }, 25851cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">t</span>) =></span> { 25861cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ok</span>(<span class="hljs-string">'some relevant assertion here'</span>); 25871cb0ef41Sopenharmony_ci }, 25881cb0ef41Sopenharmony_ci ); 25891cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 25901cb0ef41Sopenharmony_ci</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> 25911cb0ef41Sopenharmony_ci<div class="api_metadata"> 25921cb0ef41Sopenharmony_ci<span>Added in: v18.7.0</span> 25931cb0ef41Sopenharmony_ci</div> 25941cb0ef41Sopenharmony_ci<p>An instance of <code>SuiteContext</code> is passed to each suite function in order to 25951cb0ef41Sopenharmony_ciinteract with the test runner. However, the <code>SuiteContext</code> constructor is not 25961cb0ef41Sopenharmony_ciexposed as part of the API.</p> 25971cb0ef41Sopenharmony_ci<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> 25981cb0ef41Sopenharmony_ci<div class="api_metadata"> 25991cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 26001cb0ef41Sopenharmony_ci</div> 26011cb0ef41Sopenharmony_ci<p>The name of the suite.</p> 26021cb0ef41Sopenharmony_ci<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> 26031cb0ef41Sopenharmony_ci<div class="api_metadata"> 26041cb0ef41Sopenharmony_ci<span>Added in: v18.7.0</span> 26051cb0ef41Sopenharmony_ci</div> 26061cb0ef41Sopenharmony_ci<ul> 26071cb0ef41Sopenharmony_ci<li><a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to abort test subtasks when the test has been 26081cb0ef41Sopenharmony_ciaborted.</li> 26091cb0ef41Sopenharmony_ci</ul></section> 26101cb0ef41Sopenharmony_ci <!-- API END --> 26111cb0ef41Sopenharmony_ci </div> 26121cb0ef41Sopenharmony_ci </div> 26131cb0ef41Sopenharmony_ci </div> 26141cb0ef41Sopenharmony_ci</body> 26151cb0ef41Sopenharmony_ci</html> 2616