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>Assert | 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/assert.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <style>@media(max-width:598px){.with-47-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:526px){.with-38-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:622px){.with-50-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:542px){.with-40-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-assert"> 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 active">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">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="assert" 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_2"><a href="#assert">Assert</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#strict-assertion-mode">Strict assertion mode</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#legacy-assertion-mode">Legacy assertion mode</a></li> 1271cb0ef41Sopenharmony_ci<li><a href="#class-assertassertionerror">Class: assert.AssertionError</a> 1281cb0ef41Sopenharmony_ci<ul> 1291cb0ef41Sopenharmony_ci<li><a href="#new-assertassertionerroroptions"><code>new assert.AssertionError(options)</code></a></li> 1301cb0ef41Sopenharmony_ci</ul> 1311cb0ef41Sopenharmony_ci</li> 1321cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-assertcalltracker">Class: <code>assert.CallTracker</code></a></span> 1331cb0ef41Sopenharmony_ci<ul> 1341cb0ef41Sopenharmony_ci<li><a href="#new-assertcalltracker"><code>new assert.CallTracker()</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#trackercallsfn-exact"><code>tracker.calls([fn][, exact])</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#trackergetcallsfn"><code>tracker.getCalls(fn)</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#trackerreport"><code>tracker.report()</code></a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#trackerresetfn"><code>tracker.reset([fn])</code></a></li> 1391cb0ef41Sopenharmony_ci<li><a href="#trackerverify"><code>tracker.verify()</code></a></li> 1401cb0ef41Sopenharmony_ci</ul> 1411cb0ef41Sopenharmony_ci</li> 1421cb0ef41Sopenharmony_ci<li><a href="#assertvalue-message"><code>assert(value[, message])</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual(actual, expected[, message])</code></a> 1441cb0ef41Sopenharmony_ci<ul> 1451cb0ef41Sopenharmony_ci<li><a href="#comparison-details">Comparison details</a></li> 1461cb0ef41Sopenharmony_ci</ul> 1471cb0ef41Sopenharmony_ci</li> 1481cb0ef41Sopenharmony_ci<li><a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual(actual, expected[, message])</code></a> 1491cb0ef41Sopenharmony_ci<ul> 1501cb0ef41Sopenharmony_ci<li><a href="#comparison-details_1">Comparison details</a></li> 1511cb0ef41Sopenharmony_ci</ul> 1521cb0ef41Sopenharmony_ci</li> 1531cb0ef41Sopenharmony_ci<li><a href="#assertdoesnotmatchstring-regexp-message"><code>assert.doesNotMatch(string, regexp[, message])</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#assertdoesnotrejectasyncfn-error-message"><code>assert.doesNotReject(asyncFn[, error][, message])</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#assertdoesnotthrowfn-error-message"><code>assert.doesNotThrow(fn[, error][, message])</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#assertequalactual-expected-message"><code>assert.equal(actual, expected[, message])</code></a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#assertfailmessage"><code>assert.fail([message])</code></a></li> 1581cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#assertfailactual-expected-message-operator-stackstartfn"><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code></a></span></li> 1591cb0ef41Sopenharmony_ci<li><a href="#assertiferrorvalue"><code>assert.ifError(value)</code></a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#assertmatchstring-regexp-message"><code>assert.match(string, regexp[, message])</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#assertnotdeepequalactual-expected-message"><code>assert.notDeepEqual(actual, expected[, message])</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual(actual, expected[, message])</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#assertnotequalactual-expected-message"><code>assert.notEqual(actual, expected[, message])</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual(actual, expected[, message])</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#assertokvalue-message"><code>assert.ok(value[, message])</code></a></li> 1661cb0ef41Sopenharmony_ci<li><a href="#assertrejectsasyncfn-error-message"><code>assert.rejects(asyncFn[, error][, message])</code></a></li> 1671cb0ef41Sopenharmony_ci<li><a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual(actual, expected[, message])</code></a></li> 1681cb0ef41Sopenharmony_ci<li><a href="#assertthrowsfn-error-message"><code>assert.throws(fn[, error][, message])</code></a></li> 1691cb0ef41Sopenharmony_ci</ul> 1701cb0ef41Sopenharmony_ci</li> 1711cb0ef41Sopenharmony_ci</ul></div></div> 1721cb0ef41Sopenharmony_ci </li> 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci <li class="picker-header"> 1761cb0ef41Sopenharmony_ci <a href="#"> 1771cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1781cb0ef41Sopenharmony_ci Index 1791cb0ef41Sopenharmony_ci </a> 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci <div class="picker"><ul> 1821cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 1831cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci <li> 1861cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 1871cb0ef41Sopenharmony_ci </li> 1881cb0ef41Sopenharmony_ci </ul> 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci<hr class="line"> 1911cb0ef41Sopenharmony_ci<ul> 1921cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert active">Assertion testing</a></li> 1931cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 1941cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 1951cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 1961cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 1971cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 1981cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 1991cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2001cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2011cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2021cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2031cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2041cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2051cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2061cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2071cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2081cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2091cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2101cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2111cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2121cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2131cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2141cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2151cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2161cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2171cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2181cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2191cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2201cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2211cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2221cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2231cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2241cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2251cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2261cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2271cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2281cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2291cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2301cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2321cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2331cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2341cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2351cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2361cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2371cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2381cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2391cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2401cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2411cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2421cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2431cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 2441cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2451cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2461cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2471cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2481cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2491cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2501cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 2511cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 2521cb0ef41Sopenharmony_ci</ul> 2531cb0ef41Sopenharmony_ci<hr class="line"> 2541cb0ef41Sopenharmony_ci<ul> 2551cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 2561cb0ef41Sopenharmony_ci</ul></div> 2571cb0ef41Sopenharmony_ci </li> 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci <li class="picker-header"> 2611cb0ef41Sopenharmony_ci <a href="#"> 2621cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2631cb0ef41Sopenharmony_ci Other versions 2641cb0ef41Sopenharmony_ci </a> 2651cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/assert.html">21.x</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/assert.html">20.x <b>LTS</b></a></li> 2671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/assert.html">19.x</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/assert.html">18.x <b>LTS</b></a></li> 2691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/assert.html">17.x</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/assert.html">16.x</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/assert.html">15.x</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/assert.html">14.x</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/assert.html">13.x</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/assert.html">12.x</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/assert.html">11.x</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/assert.html">10.x</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/assert.html">9.x</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/assert.html">8.x</a></li> 2791cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/assert.html">7.x</a></li> 2801cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/assert.html">6.x</a></li> 2811cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/assert.html">5.x</a></li> 2821cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/assert.html">4.x</a></li> 2831cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/assert.html">0.12.x</a></li> 2841cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/assert.html">0.10.x</a></li></ol></div> 2851cb0ef41Sopenharmony_ci </li> 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci <li class="picker-header"> 2881cb0ef41Sopenharmony_ci <a href="#"> 2891cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2901cb0ef41Sopenharmony_ci Options 2911cb0ef41Sopenharmony_ci </a> 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ci <div class="picker"> 2941cb0ef41Sopenharmony_ci <ul> 2951cb0ef41Sopenharmony_ci <li> 2961cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 2971cb0ef41Sopenharmony_ci </li> 2981cb0ef41Sopenharmony_ci <li> 2991cb0ef41Sopenharmony_ci <a href="assert.json">View as JSON</a> 3001cb0ef41Sopenharmony_ci </li> 3011cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/assert.md">Edit on GitHub</a></li> 3021cb0ef41Sopenharmony_ci </ul> 3031cb0ef41Sopenharmony_ci </div> 3041cb0ef41Sopenharmony_ci </li> 3051cb0ef41Sopenharmony_ci </ul> 3061cb0ef41Sopenharmony_ci </div> 3071cb0ef41Sopenharmony_ci <hr> 3081cb0ef41Sopenharmony_ci </header> 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3111cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#assert">Assert</a></span> 3121cb0ef41Sopenharmony_ci<ul> 3131cb0ef41Sopenharmony_ci<li><a href="#strict-assertion-mode">Strict assertion mode</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="#legacy-assertion-mode">Legacy assertion mode</a></li> 3151cb0ef41Sopenharmony_ci<li><a href="#class-assertassertionerror">Class: assert.AssertionError</a> 3161cb0ef41Sopenharmony_ci<ul> 3171cb0ef41Sopenharmony_ci<li><a href="#new-assertassertionerroroptions"><code>new assert.AssertionError(options)</code></a></li> 3181cb0ef41Sopenharmony_ci</ul> 3191cb0ef41Sopenharmony_ci</li> 3201cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-assertcalltracker">Class: <code>assert.CallTracker</code></a></span> 3211cb0ef41Sopenharmony_ci<ul> 3221cb0ef41Sopenharmony_ci<li><a href="#new-assertcalltracker"><code>new assert.CallTracker()</code></a></li> 3231cb0ef41Sopenharmony_ci<li><a href="#trackercallsfn-exact"><code>tracker.calls([fn][, exact])</code></a></li> 3241cb0ef41Sopenharmony_ci<li><a href="#trackergetcallsfn"><code>tracker.getCalls(fn)</code></a></li> 3251cb0ef41Sopenharmony_ci<li><a href="#trackerreport"><code>tracker.report()</code></a></li> 3261cb0ef41Sopenharmony_ci<li><a href="#trackerresetfn"><code>tracker.reset([fn])</code></a></li> 3271cb0ef41Sopenharmony_ci<li><a href="#trackerverify"><code>tracker.verify()</code></a></li> 3281cb0ef41Sopenharmony_ci</ul> 3291cb0ef41Sopenharmony_ci</li> 3301cb0ef41Sopenharmony_ci<li><a href="#assertvalue-message"><code>assert(value[, message])</code></a></li> 3311cb0ef41Sopenharmony_ci<li><a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual(actual, expected[, message])</code></a> 3321cb0ef41Sopenharmony_ci<ul> 3331cb0ef41Sopenharmony_ci<li><a href="#comparison-details">Comparison details</a></li> 3341cb0ef41Sopenharmony_ci</ul> 3351cb0ef41Sopenharmony_ci</li> 3361cb0ef41Sopenharmony_ci<li><a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual(actual, expected[, message])</code></a> 3371cb0ef41Sopenharmony_ci<ul> 3381cb0ef41Sopenharmony_ci<li><a href="#comparison-details_1">Comparison details</a></li> 3391cb0ef41Sopenharmony_ci</ul> 3401cb0ef41Sopenharmony_ci</li> 3411cb0ef41Sopenharmony_ci<li><a href="#assertdoesnotmatchstring-regexp-message"><code>assert.doesNotMatch(string, regexp[, message])</code></a></li> 3421cb0ef41Sopenharmony_ci<li><a href="#assertdoesnotrejectasyncfn-error-message"><code>assert.doesNotReject(asyncFn[, error][, message])</code></a></li> 3431cb0ef41Sopenharmony_ci<li><a href="#assertdoesnotthrowfn-error-message"><code>assert.doesNotThrow(fn[, error][, message])</code></a></li> 3441cb0ef41Sopenharmony_ci<li><a href="#assertequalactual-expected-message"><code>assert.equal(actual, expected[, message])</code></a></li> 3451cb0ef41Sopenharmony_ci<li><a href="#assertfailmessage"><code>assert.fail([message])</code></a></li> 3461cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#assertfailactual-expected-message-operator-stackstartfn"><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code></a></span></li> 3471cb0ef41Sopenharmony_ci<li><a href="#assertiferrorvalue"><code>assert.ifError(value)</code></a></li> 3481cb0ef41Sopenharmony_ci<li><a href="#assertmatchstring-regexp-message"><code>assert.match(string, regexp[, message])</code></a></li> 3491cb0ef41Sopenharmony_ci<li><a href="#assertnotdeepequalactual-expected-message"><code>assert.notDeepEqual(actual, expected[, message])</code></a></li> 3501cb0ef41Sopenharmony_ci<li><a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual(actual, expected[, message])</code></a></li> 3511cb0ef41Sopenharmony_ci<li><a href="#assertnotequalactual-expected-message"><code>assert.notEqual(actual, expected[, message])</code></a></li> 3521cb0ef41Sopenharmony_ci<li><a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual(actual, expected[, message])</code></a></li> 3531cb0ef41Sopenharmony_ci<li><a href="#assertokvalue-message"><code>assert.ok(value[, message])</code></a></li> 3541cb0ef41Sopenharmony_ci<li><a href="#assertrejectsasyncfn-error-message"><code>assert.rejects(asyncFn[, error][, message])</code></a></li> 3551cb0ef41Sopenharmony_ci<li><a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual(actual, expected[, message])</code></a></li> 3561cb0ef41Sopenharmony_ci<li><a href="#assertthrowsfn-error-message"><code>assert.throws(fn[, error][, message])</code></a></li> 3571cb0ef41Sopenharmony_ci</ul> 3581cb0ef41Sopenharmony_ci</li> 3591cb0ef41Sopenharmony_ci</ul></details> 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci <div id="apicontent"> 3621cb0ef41Sopenharmony_ci <h2>Assert<span><a class="mark" href="#assert" id="assert">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert"></a></h2> 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 3651cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/assert.js">lib/assert.js</a></p> 3661cb0ef41Sopenharmony_ci<p>The <code>node:assert</code> module provides a set of assertion functions for verifying 3671cb0ef41Sopenharmony_ciinvariants.</p> 3681cb0ef41Sopenharmony_ci<section><h3>Strict assertion mode<span><a class="mark" href="#strict-assertion-mode" id="strict-assertion-mode">#</a></span><a aria-hidden="true" class="legacy" id="assert_strict_assertion_mode"></a></h3> 3691cb0ef41Sopenharmony_ci<div class="api_metadata"> 3701cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 3711cb0ef41Sopenharmony_ci<table> 3721cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 3731cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 3741cb0ef41Sopenharmony_ci<td><p>Exposed as <code>require('node:assert/strict')</code>.</p></td></tr> 3751cb0ef41Sopenharmony_ci<tr><td>v13.9.0, v12.16.2</td> 3761cb0ef41Sopenharmony_ci<td><p>Changed "strict mode" to "strict assertion mode" and "legacy mode" to "legacy assertion mode" to avoid confusion with the more usual meaning of "strict mode".</p></td></tr> 3771cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td> 3781cb0ef41Sopenharmony_ci<td><p>Added error diffs to the strict assertion mode.</p></td></tr> 3791cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td> 3801cb0ef41Sopenharmony_ci<td><p>Added strict assertion mode to the assert module.</p></td></tr> 3811cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td> 3821cb0ef41Sopenharmony_ci<td><p><span>Added in: v9.9.0</span></p></td></tr> 3831cb0ef41Sopenharmony_ci</tbody></table> 3841cb0ef41Sopenharmony_ci</details> 3851cb0ef41Sopenharmony_ci</div> 3861cb0ef41Sopenharmony_ci<p>In strict assertion mode, non-strict methods behave like their corresponding 3871cb0ef41Sopenharmony_cistrict methods. For example, <a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a> will behave like 3881cb0ef41Sopenharmony_ci<a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a>.</p> 3891cb0ef41Sopenharmony_ci<p>In strict assertion mode, error messages for objects display a diff. In legacy 3901cb0ef41Sopenharmony_ciassertion mode, error messages for objects display the objects, often truncated.</p> 3911cb0ef41Sopenharmony_ci<p>To use strict assertion mode:</p> 3921cb0ef41Sopenharmony_ci 3931cb0ef41Sopenharmony_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> { strict <span class="hljs-keyword">as</span> assert } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>).<span class="hljs-property">strict</span>;</code><button class="copy-button">copy</button></pre> 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);</code><button class="copy-button">copy</button></pre> 3961cb0ef41Sopenharmony_ci<p>Example error diff:</p> 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_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> { strict <span class="hljs-keyword">as</span> assert } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>; 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>([[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]], <span class="hljs-number">4</span>, <span class="hljs-number">5</span>], [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'3'</span>]], <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]); 4011cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 4021cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected ... Lines skipped</span> 4031cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 4041cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 4051cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 4061cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span> 4071cb0ef41Sopenharmony_ci<span class="hljs-comment">// 2,</span> 4081cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 3</span> 4091cb0ef41Sopenharmony_ci<span class="hljs-comment">// - '3'</span> 4101cb0ef41Sopenharmony_ci<span class="hljs-comment">// ],</span> 4111cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span> 4121cb0ef41Sopenharmony_ci<span class="hljs-comment">// 5</span> 4131cb0ef41Sopenharmony_ci<span class="hljs-comment">// ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>([[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]], <span class="hljs-number">4</span>, <span class="hljs-number">5</span>], [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'3'</span>]], <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]); 4161cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 4171cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected ... Lines skipped</span> 4181cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 4191cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 4201cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 4211cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span> 4221cb0ef41Sopenharmony_ci<span class="hljs-comment">// 2,</span> 4231cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 3</span> 4241cb0ef41Sopenharmony_ci<span class="hljs-comment">// - '3'</span> 4251cb0ef41Sopenharmony_ci<span class="hljs-comment">// ],</span> 4261cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span> 4271cb0ef41Sopenharmony_ci<span class="hljs-comment">// 5</span> 4281cb0ef41Sopenharmony_ci<span class="hljs-comment">// ]</span></code><button class="copy-button">copy</button></pre> 4291cb0ef41Sopenharmony_ci<p>To deactivate the colors, use the <code>NO_COLOR</code> or <code>NODE_DISABLE_COLORS</code> 4301cb0ef41Sopenharmony_cienvironment variables. This will also deactivate the colors in the REPL. For 4311cb0ef41Sopenharmony_cimore on color support in terminal environments, read the tty 4321cb0ef41Sopenharmony_ci<a href="tty.html#writestreamgetcolordepthenv"><code>getColorDepth()</code></a> documentation.</p> 4331cb0ef41Sopenharmony_ci</section><section><h3>Legacy assertion mode<span><a class="mark" href="#legacy-assertion-mode" id="legacy-assertion-mode">#</a></span><a aria-hidden="true" class="legacy" id="assert_legacy_assertion_mode"></a></h3> 4341cb0ef41Sopenharmony_ci<p>Legacy assertion mode uses the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a> in:</p> 4351cb0ef41Sopenharmony_ci<ul> 4361cb0ef41Sopenharmony_ci<li><a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a></li> 4371cb0ef41Sopenharmony_ci<li><a href="#assertequalactual-expected-message"><code>assert.equal()</code></a></li> 4381cb0ef41Sopenharmony_ci<li><a href="#assertnotdeepequalactual-expected-message"><code>assert.notDeepEqual()</code></a></li> 4391cb0ef41Sopenharmony_ci<li><a href="#assertnotequalactual-expected-message"><code>assert.notEqual()</code></a></li> 4401cb0ef41Sopenharmony_ci</ul> 4411cb0ef41Sopenharmony_ci<p>To use legacy assertion mode:</p> 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_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>;</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>);</code><button class="copy-button">copy</button></pre> 4441cb0ef41Sopenharmony_ci<p>Legacy assertion mode may have surprising results, especially when using 4451cb0ef41Sopenharmony_ci<a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a>:</p> 4461cb0ef41Sopenharmony_ci<pre><code class="language-js cjs"><span class="hljs-comment">// WARNING: This does not throw an AssertionError in legacy assertion mode!</span> 4471cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-regexp">/a/gi</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>());</code> <button class="copy-button">copy</button></pre> 4481cb0ef41Sopenharmony_ci</section><section><h3>Class: assert.AssertionError<a class="srclink" href="https://github.com/nodejs/node/blob/1d526a47af29d59ad196c4e05606b0ec6f8090d9/lib/assert.js#L178">[src]</a><span><a class="mark" href="#class-assertassertionerror" id="class-assertassertionerror">#</a></span><a aria-hidden="true" class="legacy" id="assert_class_assert_assertionerror"></a></h3> 4491cb0ef41Sopenharmony_ci<ul> 4501cb0ef41Sopenharmony_ci<li>Extends: <a href="errors.html#class-error" class="type"><errors.Error></a></li> 4511cb0ef41Sopenharmony_ci</ul> 4521cb0ef41Sopenharmony_ci<p>Indicates the failure of an assertion. All errors thrown by the <code>node:assert</code> 4531cb0ef41Sopenharmony_cimodule will be instances of the <code>AssertionError</code> class.</p> 4541cb0ef41Sopenharmony_ci<h4><code>new assert.AssertionError(options)</code><span><a class="mark" href="#new-assertassertionerroroptions" id="new-assertassertionerroroptions">#</a></span><a aria-hidden="true" class="legacy" id="assert_new_assert_assertionerror_options"></a></h4> 4551cb0ef41Sopenharmony_ci<div class="api_metadata"> 4561cb0ef41Sopenharmony_ci<span>Added in: v0.1.21</span> 4571cb0ef41Sopenharmony_ci</div> 4581cb0ef41Sopenharmony_ci<ul> 4591cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4601cb0ef41Sopenharmony_ci<ul> 4611cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If provided, the error message is set to this value.</li> 4621cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The <code>actual</code> property on the error instance.</li> 4631cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The <code>expected</code> property on the error instance.</li> 4641cb0ef41Sopenharmony_ci<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <code>operator</code> property on the error instance.</li> 4651cb0ef41Sopenharmony_ci<li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> If provided, the generated stack trace omits 4661cb0ef41Sopenharmony_ciframes before this function.</li> 4671cb0ef41Sopenharmony_ci</ul> 4681cb0ef41Sopenharmony_ci</li> 4691cb0ef41Sopenharmony_ci</ul> 4701cb0ef41Sopenharmony_ci<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p> 4711cb0ef41Sopenharmony_ci<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>) 4721cb0ef41Sopenharmony_ciand:</p> 4731cb0ef41Sopenharmony_ci<ul> 4741cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Set to the <code>actual</code> argument for methods such as 4751cb0ef41Sopenharmony_ci<a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a>.</li> 4761cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Set to the <code>expected</code> value for methods such as 4771cb0ef41Sopenharmony_ci<a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a>.</li> 4781cb0ef41Sopenharmony_ci<li><code>generatedMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates if the message was auto-generated 4791cb0ef41Sopenharmony_ci(<code>true</code>) or not.</li> 4801cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Value is always <code>ERR_ASSERTION</code> to show that the error is an 4811cb0ef41Sopenharmony_ciassertion error.</li> 4821cb0ef41Sopenharmony_ci<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Set to the passed in operator value.</li> 4831cb0ef41Sopenharmony_ci</ul> 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_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>; 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span> 4881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">AssertionError</span>({ 4891cb0ef41Sopenharmony_ci <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>, 4901cb0ef41Sopenharmony_ci <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>, 4911cb0ef41Sopenharmony_ci <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>, 4921cb0ef41Sopenharmony_ci}); 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Verify error output:</span> 4951cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 4961cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 4971cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 4981cb0ef41Sopenharmony_ci <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> assert.<span class="hljs-property">AssertionError</span>); 4991cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, message); 5001cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'AssertionError'</span>); 5011cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actual</span>, <span class="hljs-number">1</span>); 5021cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expected</span>, <span class="hljs-number">2</span>); 5031cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code</span>, <span class="hljs-string">'ERR_ASSERTION'</span>); 5041cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">operator</span>, <span class="hljs-string">'strictEqual'</span>); 5051cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generatedMessage</span>, <span class="hljs-literal">true</span>); 5061cb0ef41Sopenharmony_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>); 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span> 5091cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">AssertionError</span>({ 5101cb0ef41Sopenharmony_ci <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>, 5111cb0ef41Sopenharmony_ci <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>, 5121cb0ef41Sopenharmony_ci <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>, 5131cb0ef41Sopenharmony_ci}); 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci<span class="hljs-comment">// Verify error output:</span> 5161cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</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} <span class="hljs-keyword">catch</span> (err) { 5191cb0ef41Sopenharmony_ci <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> assert.<span class="hljs-property">AssertionError</span>); 5201cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, message); 5211cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'AssertionError'</span>); 5221cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actual</span>, <span class="hljs-number">1</span>); 5231cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expected</span>, <span class="hljs-number">2</span>); 5241cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code</span>, <span class="hljs-string">'ERR_ASSERTION'</span>); 5251cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">operator</span>, <span class="hljs-string">'strictEqual'</span>); 5261cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generatedMessage</span>, <span class="hljs-literal">true</span>); 5271cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 5281cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>assert.CallTracker</code><span><a class="mark" href="#class-assertcalltracker" id="class-assertcalltracker">#</a></span><a aria-hidden="true" class="legacy" id="assert_class_assert_calltracker"></a></h3> 5291cb0ef41Sopenharmony_ci<div class="api_metadata"> 5301cb0ef41Sopenharmony_ci<span>Added in: v14.2.0, v12.19.0</span> 5311cb0ef41Sopenharmony_ci</div> 5321cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 5331cb0ef41Sopenharmony_ci<p>This feature is currently experimental and behavior might still change.</p> 5341cb0ef41Sopenharmony_ci<h4><code>new assert.CallTracker()</code><span><a class="mark" href="#new-assertcalltracker" id="new-assertcalltracker">#</a></span><a aria-hidden="true" class="legacy" id="assert_new_assert_calltracker"></a></h4> 5351cb0ef41Sopenharmony_ci<div class="api_metadata"> 5361cb0ef41Sopenharmony_ci<span>Added in: v14.2.0, v12.19.0</span> 5371cb0ef41Sopenharmony_ci</div> 5381cb0ef41Sopenharmony_ci<p>Creates a new <a href="#class-assertcalltracker"><code>CallTracker</code></a> object which can be used to track if functions 5391cb0ef41Sopenharmony_ciwere called a specific number of times. The <code>tracker.verify()</code> must be called 5401cb0ef41Sopenharmony_cifor the verification to take place. The usual pattern would be to call it in a 5411cb0ef41Sopenharmony_ci<a href="process.html#event-exit"><code>process.on('exit')</code></a> handler.</p> 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_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>; 5441cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 5491cb0ef41Sopenharmony_ci 5501cb0ef41Sopenharmony_ci<span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</span> 5511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">1</span>); 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(); 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ci<span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions have</span> 5561cb0ef41Sopenharmony_ci<span class="hljs-comment">// been called exact times.</span> 5571cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 5581cb0ef41Sopenharmony_ci tracker.<span class="hljs-title function_">verify</span>(); 5591cb0ef41Sopenharmony_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>); 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci<span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</span> 5661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">1</span>); 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(); 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions have</span> 5711cb0ef41Sopenharmony_ci<span class="hljs-comment">// been called exact times.</span> 5721cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 5731cb0ef41Sopenharmony_ci tracker.<span class="hljs-title function_">verify</span>(); 5741cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 5751cb0ef41Sopenharmony_ci<h4><code>tracker.calls([fn][, exact])</code><span><a class="mark" href="#trackercallsfn-exact" id="trackercallsfn-exact">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_calls_fn_exact"></a></h4> 5761cb0ef41Sopenharmony_ci<div class="api_metadata"> 5771cb0ef41Sopenharmony_ci<span>Added in: v14.2.0, v12.19.0</span> 5781cb0ef41Sopenharmony_ci</div> 5791cb0ef41Sopenharmony_ci<ul> 5801cb0ef41Sopenharmony_ci<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <strong>Default:</strong> A no-op function.</li> 5811cb0ef41Sopenharmony_ci<li><code>exact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>1</code>.</li> 5821cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> that wraps <code>fn</code>.</li> 5831cb0ef41Sopenharmony_ci</ul> 5841cb0ef41Sopenharmony_ci<p>The wrapper function is expected to be called exactly <code>exact</code> times. If the 5851cb0ef41Sopenharmony_cifunction has not been called exactly <code>exact</code> times when 5861cb0ef41Sopenharmony_ci<a href="#trackerverify"><code>tracker.verify()</code></a> is called, then <a href="#trackerverify"><code>tracker.verify()</code></a> will throw an 5871cb0ef41Sopenharmony_cierror.</p> 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_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>; 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 5921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 5971cb0ef41Sopenharmony_ci<span class="hljs-comment">// before tracker.verify().</span> 5981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);</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>); 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 6011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 6061cb0ef41Sopenharmony_ci<span class="hljs-comment">// before tracker.verify().</span> 6071cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);</code><button class="copy-button">copy</button></pre> 6081cb0ef41Sopenharmony_ci<h4><code>tracker.getCalls(fn)</code><span><a class="mark" href="#trackergetcallsfn" id="trackergetcallsfn">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_getcalls_fn"></a></h4> 6091cb0ef41Sopenharmony_ci<div class="api_metadata"> 6101cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 6111cb0ef41Sopenharmony_ci</div> 6121cb0ef41Sopenharmony_ci<ul> 6131cb0ef41Sopenharmony_ci<li> 6141cb0ef41Sopenharmony_ci<p><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>.</p> 6151cb0ef41Sopenharmony_ci</li> 6161cb0ef41Sopenharmony_ci<li> 6171cb0ef41Sopenharmony_ci<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> with all the calls to a tracked function.</p> 6181cb0ef41Sopenharmony_ci</li> 6191cb0ef41Sopenharmony_ci<li> 6201cb0ef41Sopenharmony_ci<p>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 6211cb0ef41Sopenharmony_ci<ul> 6221cb0ef41Sopenharmony_ci<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 6231cb0ef41Sopenharmony_ci<li><code>arguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> the arguments passed to the tracked function</li> 6241cb0ef41Sopenharmony_ci</ul> 6251cb0ef41Sopenharmony_ci</li> 6261cb0ef41Sopenharmony_ci</ul> 6271cb0ef41Sopenharmony_ci 6281cb0ef41Sopenharmony_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>; 6291cb0ef41Sopenharmony_ci 6301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 6311cb0ef41Sopenharmony_ci 6321cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 6331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func); 6341cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>); 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc), 6371cb0ef41Sopenharmony_ci [{ <span class="hljs-attr">thisArg</span>: <span class="hljs-literal">undefined</span>, <span class="hljs-attr">arguments</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] }]);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 6401cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 6431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func); 6441cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>); 6451cb0ef41Sopenharmony_ci 6461cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc), 6471cb0ef41Sopenharmony_ci [{ <span class="hljs-attr">thisArg</span>: <span class="hljs-literal">undefined</span>, <span class="hljs-attr">arguments</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] }]);</code><button class="copy-button">copy</button></pre> 6481cb0ef41Sopenharmony_ci<h4><code>tracker.report()</code><span><a class="mark" href="#trackerreport" id="trackerreport">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_report"></a></h4> 6491cb0ef41Sopenharmony_ci<div class="api_metadata"> 6501cb0ef41Sopenharmony_ci<span>Added in: v14.2.0, v12.19.0</span> 6511cb0ef41Sopenharmony_ci</div> 6521cb0ef41Sopenharmony_ci<ul> 6531cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> of objects containing information about the wrapper functions 6541cb0ef41Sopenharmony_cireturned by <a href="#trackercallsfn-exact"><code>tracker.calls()</code></a>.</li> 6551cb0ef41Sopenharmony_ci<li>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6561cb0ef41Sopenharmony_ci<ul> 6571cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6581cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The actual number of times the function was called.</li> 6591cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of times the function was expected to be 6601cb0ef41Sopenharmony_cicalled.</li> 6611cb0ef41Sopenharmony_ci<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the function that is wrapped.</li> 6621cb0ef41Sopenharmony_ci<li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A stack trace of the function.</li> 6631cb0ef41Sopenharmony_ci</ul> 6641cb0ef41Sopenharmony_ci</li> 6651cb0ef41Sopenharmony_ci</ul> 6661cb0ef41Sopenharmony_ci<p>The arrays contains information about the expected and actual number of calls of 6671cb0ef41Sopenharmony_cithe functions that have not been called the expected number of times.</p> 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_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>; 6701cb0ef41Sopenharmony_ci 6711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 6721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 6731cb0ef41Sopenharmony_ci 6741cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 6751cb0ef41Sopenharmony_ci 6761cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 6771cb0ef41Sopenharmony_ci<span class="hljs-comment">// before tracker.verify().</span> 6781cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>); 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns an array containing information on callsfunc()</span> 6811cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(tracker.<span class="hljs-title function_">report</span>()); 6821cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 6831cb0ef41Sopenharmony_ci<span class="hljs-comment">// {</span> 6841cb0ef41Sopenharmony_ci<span class="hljs-comment">// message: 'Expected the func function to be executed 2 time(s) but was</span> 6851cb0ef41Sopenharmony_ci<span class="hljs-comment">// executed 0 time(s).',</span> 6861cb0ef41Sopenharmony_ci<span class="hljs-comment">// actual: 0,</span> 6871cb0ef41Sopenharmony_ci<span class="hljs-comment">// expected: 2,</span> 6881cb0ef41Sopenharmony_ci<span class="hljs-comment">// operator: 'func',</span> 6891cb0ef41Sopenharmony_ci<span class="hljs-comment">// stack: stack trace</span> 6901cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span> 6911cb0ef41Sopenharmony_ci<span class="hljs-comment">// ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 6921cb0ef41Sopenharmony_ci 6931cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 6941cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 6971cb0ef41Sopenharmony_ci 6981cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 6991cb0ef41Sopenharmony_ci<span class="hljs-comment">// before tracker.verify().</span> 7001cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>); 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns an array containing information on callsfunc()</span> 7031cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(tracker.<span class="hljs-title function_">report</span>()); 7041cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 7051cb0ef41Sopenharmony_ci<span class="hljs-comment">// {</span> 7061cb0ef41Sopenharmony_ci<span class="hljs-comment">// message: 'Expected the func function to be executed 2 time(s) but was</span> 7071cb0ef41Sopenharmony_ci<span class="hljs-comment">// executed 0 time(s).',</span> 7081cb0ef41Sopenharmony_ci<span class="hljs-comment">// actual: 0,</span> 7091cb0ef41Sopenharmony_ci<span class="hljs-comment">// expected: 2,</span> 7101cb0ef41Sopenharmony_ci<span class="hljs-comment">// operator: 'func',</span> 7111cb0ef41Sopenharmony_ci<span class="hljs-comment">// stack: stack trace</span> 7121cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span> 7131cb0ef41Sopenharmony_ci<span class="hljs-comment">// ]</span></code><button class="copy-button">copy</button></pre> 7141cb0ef41Sopenharmony_ci<h4><code>tracker.reset([fn])</code><span><a class="mark" href="#trackerresetfn" id="trackerresetfn">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_reset_fn"></a></h4> 7151cb0ef41Sopenharmony_ci<div class="api_metadata"> 7161cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 7171cb0ef41Sopenharmony_ci</div> 7181cb0ef41Sopenharmony_ci<ul> 7191cb0ef41Sopenharmony_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 tracked function to reset.</li> 7201cb0ef41Sopenharmony_ci</ul> 7211cb0ef41Sopenharmony_ci<p>Reset calls of the call tracker. 7221cb0ef41Sopenharmony_ciIf a tracked function is passed as an argument, the calls will be reset for it. 7231cb0ef41Sopenharmony_ciIf no arguments are passed, all tracked functions will be reset</p> 7241cb0ef41Sopenharmony_ci 7251cb0ef41Sopenharmony_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>; 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 7281cb0ef41Sopenharmony_ci 7291cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 7301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func); 7311cb0ef41Sopenharmony_ci 7321cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(); 7331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Tracker was called once</span> 7341cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">1</span>); 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_citracker.<span class="hljs-title function_">reset</span>(callsfunc); 7371cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">0</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 7381cb0ef41Sopenharmony_ci 7391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 7401cb0ef41Sopenharmony_ci 7411cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 7421cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func); 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(); 7451cb0ef41Sopenharmony_ci<span class="hljs-comment">// Tracker was called once</span> 7461cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">1</span>); 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_citracker.<span class="hljs-title function_">reset</span>(callsfunc); 7491cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">0</span>);</code><button class="copy-button">copy</button></pre> 7501cb0ef41Sopenharmony_ci<h4><code>tracker.verify()</code><span><a class="mark" href="#trackerverify" id="trackerverify">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_verify"></a></h4> 7511cb0ef41Sopenharmony_ci<div class="api_metadata"> 7521cb0ef41Sopenharmony_ci<span>Added in: v14.2.0, v12.19.0</span> 7531cb0ef41Sopenharmony_ci</div> 7541cb0ef41Sopenharmony_ci<p>Iterates through the list of functions passed to 7551cb0ef41Sopenharmony_ci<a href="#trackercallsfn-exact"><code>tracker.calls()</code></a> and will throw an error for functions that 7561cb0ef41Sopenharmony_cihave not been called the expected number of times.</p> 7571cb0ef41Sopenharmony_ci 7581cb0ef41Sopenharmony_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>; 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 7611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 7641cb0ef41Sopenharmony_ci 7651cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 7661cb0ef41Sopenharmony_ci<span class="hljs-comment">// before tracker.verify().</span> 7671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>); 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(); 7701cb0ef41Sopenharmony_ci 7711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span> 7721cb0ef41Sopenharmony_citracker.<span class="hljs-title function_">verify</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates call tracker.</span> 7751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 7761cb0ef41Sopenharmony_ci 7771cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 7781cb0ef41Sopenharmony_ci 7791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 7801cb0ef41Sopenharmony_ci<span class="hljs-comment">// before tracker.verify().</span> 7811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>); 7821cb0ef41Sopenharmony_ci 7831cb0ef41Sopenharmony_ci<span class="hljs-title function_">callsfunc</span>(); 7841cb0ef41Sopenharmony_ci 7851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span> 7861cb0ef41Sopenharmony_citracker.<span class="hljs-title function_">verify</span>();</code><button class="copy-button">copy</button></pre> 7871cb0ef41Sopenharmony_ci</section><section><h3><code>assert(value[, message])</code><span><a class="mark" href="#assertvalue-message" id="assertvalue-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_value_message"></a></h3> 7881cb0ef41Sopenharmony_ci<div class="api_metadata"> 7891cb0ef41Sopenharmony_ci<span>Added in: v0.5.9</span> 7901cb0ef41Sopenharmony_ci</div> 7911cb0ef41Sopenharmony_ci<ul> 7921cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The input that is checked for being truthy.</li> 7931cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 7941cb0ef41Sopenharmony_ci</ul> 7951cb0ef41Sopenharmony_ci<p>An alias of <a href="#assertokvalue-message"><code>assert.ok()</code></a>.</p> 7961cb0ef41Sopenharmony_ci</section><section><h3><code>assert.deepEqual(actual, expected[, message])</code><span><a class="mark" href="#assertdeepequalactual-expected-message" id="assertdeepequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_deepequal_actual_expected_message"></a></h3> 7971cb0ef41Sopenharmony_ci<div class="api_metadata"> 7981cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7991cb0ef41Sopenharmony_ci<table> 8001cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8011cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 8021cb0ef41Sopenharmony_ci<td><p>Regular expressions lastIndex property is now compared as well.</p></td></tr> 8031cb0ef41Sopenharmony_ci<tr><td>v16.0.0, v14.18.0</td> 8041cb0ef41Sopenharmony_ci<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 8051cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 8061cb0ef41Sopenharmony_ci<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr> 8071cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 8081cb0ef41Sopenharmony_ci<td><p>The type tags are now properly compared and there are a couple minor comparison adjustments to make the check less surprising.</p></td></tr> 8091cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 8101cb0ef41Sopenharmony_ci<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 8111cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 8121cb0ef41Sopenharmony_ci<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 8131cb0ef41Sopenharmony_ci<tr><td>v6.4.0, v4.7.1</td> 8141cb0ef41Sopenharmony_ci<td><p>Typed array slices are handled correctly now.</p></td></tr> 8151cb0ef41Sopenharmony_ci<tr><td>v6.1.0, v4.5.0</td> 8161cb0ef41Sopenharmony_ci<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 8171cb0ef41Sopenharmony_ci<tr><td>v5.10.1, v4.4.3</td> 8181cb0ef41Sopenharmony_ci<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 8191cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 8201cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 8211cb0ef41Sopenharmony_ci</tbody></table> 8221cb0ef41Sopenharmony_ci</details> 8231cb0ef41Sopenharmony_ci</div> 8241cb0ef41Sopenharmony_ci<ul> 8251cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 8261cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 8271cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 8281cb0ef41Sopenharmony_ci</ul> 8291cb0ef41Sopenharmony_ci<p><strong>Strict assertion mode</strong></p> 8301cb0ef41Sopenharmony_ci<p>An alias of <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a>.</p> 8311cb0ef41Sopenharmony_ci<p><strong>Legacy assertion mode</strong></p> 8321cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a> instead.</div><p></p> 8331cb0ef41Sopenharmony_ci<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. Consider 8341cb0ef41Sopenharmony_ciusing <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a> instead. <a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a> can have 8351cb0ef41Sopenharmony_cisurprising results.</p> 8361cb0ef41Sopenharmony_ci<p><em>Deep equality</em> means that the enumerable "own" properties of child objects 8371cb0ef41Sopenharmony_ciare also recursively evaluated by the following rules.</p> 8381cb0ef41Sopenharmony_ci<h4>Comparison details<span><a class="mark" href="#comparison-details" id="comparison-details">#</a></span><a aria-hidden="true" class="legacy" id="assert_comparison_details"></a></h4> 8391cb0ef41Sopenharmony_ci<ul> 8401cb0ef41Sopenharmony_ci<li>Primitive values are compared with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a>, 8411cb0ef41Sopenharmony_ciwith the exception of <code>NaN</code>. It is treated as being identical in case 8421cb0ef41Sopenharmony_ciboth sides are <code>NaN</code>.</li> 8431cb0ef41Sopenharmony_ci<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li> 8441cb0ef41Sopenharmony_ci<li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable "own" properties</a> are considered.</li> 8451cb0ef41Sopenharmony_ci<li><a href="errors.html#class-error"><code>Error</code></a> names and messages are always compared, even if these are not 8461cb0ef41Sopenharmony_cienumerable properties.</li> 8471cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript">Object wrappers</a> are compared both as objects and unwrapped values.</li> 8481cb0ef41Sopenharmony_ci<li><code>Object</code> properties are compared unordered.</li> 8491cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> items are compared unordered.</li> 8501cb0ef41Sopenharmony_ci<li>Recursion stops when both sides differ or both sides encounter a circular 8511cb0ef41Sopenharmony_cireference.</li> 8521cb0ef41Sopenharmony_ci<li>Implementation does not test the <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots"><code>[[Prototype]]</code></a> of 8531cb0ef41Sopenharmony_ciobjects.</li> 8541cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"><code>Symbol</code></a> properties are not compared.</li> 8551cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> comparison does not rely on their values.</li> 8561cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> lastIndex, flags, and source are always compared, even if these 8571cb0ef41Sopenharmony_ciare not enumerable properties.</li> 8581cb0ef41Sopenharmony_ci</ul> 8591cb0ef41Sopenharmony_ci<p>The following example does not throw an <a href="#class-assertassertionerror"><code>AssertionError</code></a> because the 8601cb0ef41Sopenharmony_ciprimitives are compared using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a>.</p> 8611cb0ef41Sopenharmony_ci 8621cb0ef41Sopenharmony_ci<pre class="with-50-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>; 8631cb0ef41Sopenharmony_ci<span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span> 8641cb0ef41Sopenharmony_ci 8651cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</span>, <span class="hljs-literal">false</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 8661cb0ef41Sopenharmony_ci<span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span> 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</span>, <span class="hljs-literal">false</span>);</code><button class="copy-button">copy</button></pre> 8691cb0ef41Sopenharmony_ci<p>"Deep" equality means that the enumerable "own" properties of child objects 8701cb0ef41Sopenharmony_ciare evaluated also:</p> 8711cb0ef41Sopenharmony_ci 8721cb0ef41Sopenharmony_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>; 8731cb0ef41Sopenharmony_ci 8741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj1 = { 8751cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 8761cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 8771cb0ef41Sopenharmony_ci }, 8781cb0ef41Sopenharmony_ci}; 8791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = { 8801cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 8811cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>, 8821cb0ef41Sopenharmony_ci }, 8831cb0ef41Sopenharmony_ci}; 8841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj3 = { 8851cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 8861cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 8871cb0ef41Sopenharmony_ci }, 8881cb0ef41Sopenharmony_ci}; 8891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1); 8901cb0ef41Sopenharmony_ci 8911cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1); 8921cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Values of b are different:</span> 8951cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2); 8961cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span> 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3); 8991cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prototypes are ignored:</span> 9021cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4); 9031cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 9041cb0ef41Sopenharmony_ci 9051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj1 = { 9061cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 9071cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 9081cb0ef41Sopenharmony_ci }, 9091cb0ef41Sopenharmony_ci}; 9101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = { 9111cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 9121cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>, 9131cb0ef41Sopenharmony_ci }, 9141cb0ef41Sopenharmony_ci}; 9151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj3 = { 9161cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 9171cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 9181cb0ef41Sopenharmony_ci }, 9191cb0ef41Sopenharmony_ci}; 9201cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1); 9211cb0ef41Sopenharmony_ci 9221cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1); 9231cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 9241cb0ef41Sopenharmony_ci 9251cb0ef41Sopenharmony_ci<span class="hljs-comment">// Values of b are different:</span> 9261cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2); 9271cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span> 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3); 9301cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 9311cb0ef41Sopenharmony_ci 9321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prototypes are ignored:</span> 9331cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4); 9341cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code><button class="copy-button">copy</button></pre> 9351cb0ef41Sopenharmony_ci<p>If the values are not equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 9361cb0ef41Sopenharmony_ciproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code> 9371cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the <code>message</code> 9381cb0ef41Sopenharmony_ciparameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 9391cb0ef41Sopenharmony_ci<a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 9401cb0ef41Sopenharmony_ci</section><section><h3><code>assert.deepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertdeepstrictequalactual-expected-message" id="assertdeepstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_deepstrictequal_actual_expected_message"></a></h3> 9411cb0ef41Sopenharmony_ci<div class="api_metadata"> 9421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9431cb0ef41Sopenharmony_ci<table> 9441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9451cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 9461cb0ef41Sopenharmony_ci<td><p>Regular expressions lastIndex property is now compared as well.</p></td></tr> 9471cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 9481cb0ef41Sopenharmony_ci<td><p>Enumerable symbol properties are now compared.</p></td></tr> 9491cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 9501cb0ef41Sopenharmony_ci<td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-samevaluezero">SameValueZero</a> comparison.</p></td></tr> 9511cb0ef41Sopenharmony_ci<tr><td>v8.5.0</td> 9521cb0ef41Sopenharmony_ci<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 9531cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 9541cb0ef41Sopenharmony_ci<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 9551cb0ef41Sopenharmony_ci<tr><td>v6.1.0</td> 9561cb0ef41Sopenharmony_ci<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 9571cb0ef41Sopenharmony_ci<tr><td>v6.4.0, v4.7.1</td> 9581cb0ef41Sopenharmony_ci<td><p>Typed array slices are handled correctly now.</p></td></tr> 9591cb0ef41Sopenharmony_ci<tr><td>v5.10.1, v4.4.3</td> 9601cb0ef41Sopenharmony_ci<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 9611cb0ef41Sopenharmony_ci<tr><td>v1.2.0</td> 9621cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.2.0</span></p></td></tr> 9631cb0ef41Sopenharmony_ci</tbody></table> 9641cb0ef41Sopenharmony_ci</details> 9651cb0ef41Sopenharmony_ci</div> 9661cb0ef41Sopenharmony_ci<ul> 9671cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 9681cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 9691cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 9701cb0ef41Sopenharmony_ci</ul> 9711cb0ef41Sopenharmony_ci<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. 9721cb0ef41Sopenharmony_ci"Deep" equality means that the enumerable "own" properties of child objects 9731cb0ef41Sopenharmony_ciare recursively evaluated also by the following rules.</p> 9741cb0ef41Sopenharmony_ci<h4>Comparison details<span><a class="mark" href="#comparison-details_1" id="comparison-details_1">#</a></span><a aria-hidden="true" class="legacy" id="assert_comparison_details_1"></a></h4> 9751cb0ef41Sopenharmony_ci<ul> 9761cb0ef41Sopenharmony_ci<li>Primitive values are compared using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</li> 9771cb0ef41Sopenharmony_ci<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li> 9781cb0ef41Sopenharmony_ci<li><a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots"><code>[[Prototype]]</code></a> of objects are compared using 9791cb0ef41Sopenharmony_cithe <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality"><code>===</code> operator</a>.</li> 9801cb0ef41Sopenharmony_ci<li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable "own" properties</a> are considered.</li> 9811cb0ef41Sopenharmony_ci<li><a href="errors.html#class-error"><code>Error</code></a> names and messages are always compared, even if these are not 9821cb0ef41Sopenharmony_cienumerable properties.</li> 9831cb0ef41Sopenharmony_ci<li>Enumerable own <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"><code>Symbol</code></a> properties are compared as well.</li> 9841cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript">Object wrappers</a> are compared both as objects and unwrapped values.</li> 9851cb0ef41Sopenharmony_ci<li><code>Object</code> properties are compared unordered.</li> 9861cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> items are compared unordered.</li> 9871cb0ef41Sopenharmony_ci<li>Recursion stops when both sides differ or both sides encounter a circular 9881cb0ef41Sopenharmony_cireference.</li> 9891cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> comparison does not rely on their values. See 9901cb0ef41Sopenharmony_cibelow for further details.</li> 9911cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> lastIndex, flags, and source are always compared, even if these 9921cb0ef41Sopenharmony_ciare not enumerable properties.</li> 9931cb0ef41Sopenharmony_ci</ul> 9941cb0ef41Sopenharmony_ci 9951cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 9961cb0ef41Sopenharmony_ci 9971cb0ef41Sopenharmony_ci<span class="hljs-comment">// This fails because 1 !== '1'.</span> 9981cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'1'</span> }); 9991cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10001cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10011cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10021cb0ef41Sopenharmony_ci<span class="hljs-comment">// {</span> 10031cb0ef41Sopenharmony_ci<span class="hljs-comment">// + a: 1</span> 10041cb0ef41Sopenharmony_ci<span class="hljs-comment">// - a: '1'</span> 10051cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span> 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci<span class="hljs-comment">// The following objects don't have own properties</span> 10081cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(); 10091cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> object = {}; 10101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fakeDate = {}; 10111cb0ef41Sopenharmony_ci<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">setPrototypeOf</span>(fakeDate, <span class="hljs-title class_">Date</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>); 10121cb0ef41Sopenharmony_ci 10131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different [[Prototype]]:</span> 10141cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate); 10151cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10161cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10171cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10181cb0ef41Sopenharmony_ci<span class="hljs-comment">// + {}</span> 10191cb0ef41Sopenharmony_ci<span class="hljs-comment">// - Date {}</span> 10201cb0ef41Sopenharmony_ci 10211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different type tags:</span> 10221cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate); 10231cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10241cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10251cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10261cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span> 10271cb0ef41Sopenharmony_ci<span class="hljs-comment">// - Date {}</span> 10281cb0ef41Sopenharmony_ci 10291cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>); 10301cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK because Object.is(NaN, NaN) is true.</span> 10311cb0ef41Sopenharmony_ci 10321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different unwrapped numbers:</span> 10331cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Number</span>(<span class="hljs-number">1</span>), <span class="hljs-keyword">new</span> <span class="hljs-title class_">Number</span>(<span class="hljs-number">2</span>)); 10341cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10351cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10361cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10371cb0ef41Sopenharmony_ci<span class="hljs-comment">// + [Number: 1]</span> 10381cb0ef41Sopenharmony_ci<span class="hljs-comment">// - [Number: 2]</span> 10391cb0ef41Sopenharmony_ci 10401cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'foo'</span>), <span class="hljs-title class_">Object</span>(<span class="hljs-string">'foo'</span>)); 10411cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</span> 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>); 10441cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 10451cb0ef41Sopenharmony_ci 10461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different zeros:</span> 10471cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>); 10481cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10491cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10501cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10511cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 0</span> 10521cb0ef41Sopenharmony_ci<span class="hljs-comment">// - -0</span> 10531cb0ef41Sopenharmony_ci 10541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>(); 10551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>(); 10561cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol1]: <span class="hljs-number">1</span> }); 10571cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, because it is the same symbol on both objects.</span> 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol2]: <span class="hljs-number">1</span> }); 10601cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:</span> 10611cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10621cb0ef41Sopenharmony_ci<span class="hljs-comment">// {</span> 10631cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Symbol()]: 1</span> 10641cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span> 10651cb0ef41Sopenharmony_ci 10661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>(); 10671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>([[{}, {}]]); 10681cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>(); 10691cb0ef41Sopenharmony_ciweakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>; 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2); 10721cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, because it is impossible to compare the entries</span> 10731cb0ef41Sopenharmony_ci 10741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:</span> 10751cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3); 10761cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10771cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10781cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10791cb0ef41Sopenharmony_ci<span class="hljs-comment">// WeakMap {</span> 10801cb0ef41Sopenharmony_ci<span class="hljs-comment">// + [items unknown]</span> 10811cb0ef41Sopenharmony_ci<span class="hljs-comment">// - [items unknown],</span> 10821cb0ef41Sopenharmony_ci<span class="hljs-comment">// - unequal: true</span> 10831cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 10841cb0ef41Sopenharmony_ci 10851cb0ef41Sopenharmony_ci<span class="hljs-comment">// This fails because 1 !== '1'.</span> 10861cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'1'</span> }); 10871cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 10881cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 10891cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 10901cb0ef41Sopenharmony_ci<span class="hljs-comment">// {</span> 10911cb0ef41Sopenharmony_ci<span class="hljs-comment">// + a: 1</span> 10921cb0ef41Sopenharmony_ci<span class="hljs-comment">// - a: '1'</span> 10931cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span> 10941cb0ef41Sopenharmony_ci 10951cb0ef41Sopenharmony_ci<span class="hljs-comment">// The following objects don't have own properties</span> 10961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(); 10971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> object = {}; 10981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fakeDate = {}; 10991cb0ef41Sopenharmony_ci<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">setPrototypeOf</span>(fakeDate, <span class="hljs-title class_">Date</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>); 11001cb0ef41Sopenharmony_ci 11011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different [[Prototype]]:</span> 11021cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate); 11031cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 11041cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 11051cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 11061cb0ef41Sopenharmony_ci<span class="hljs-comment">// + {}</span> 11071cb0ef41Sopenharmony_ci<span class="hljs-comment">// - Date {}</span> 11081cb0ef41Sopenharmony_ci 11091cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different type tags:</span> 11101cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate); 11111cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 11121cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 11131cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 11141cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span> 11151cb0ef41Sopenharmony_ci<span class="hljs-comment">// - Date {}</span> 11161cb0ef41Sopenharmony_ci 11171cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>); 11181cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK because Object.is(NaN, NaN) is true.</span> 11191cb0ef41Sopenharmony_ci 11201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different unwrapped numbers:</span> 11211cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Number</span>(<span class="hljs-number">1</span>), <span class="hljs-keyword">new</span> <span class="hljs-title class_">Number</span>(<span class="hljs-number">2</span>)); 11221cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 11231cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 11241cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 11251cb0ef41Sopenharmony_ci<span class="hljs-comment">// + [Number: 1]</span> 11261cb0ef41Sopenharmony_ci<span class="hljs-comment">// - [Number: 2]</span> 11271cb0ef41Sopenharmony_ci 11281cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'foo'</span>), <span class="hljs-title class_">Object</span>(<span class="hljs-string">'foo'</span>)); 11291cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</span> 11301cb0ef41Sopenharmony_ci 11311cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>); 11321cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 11331cb0ef41Sopenharmony_ci 11341cb0ef41Sopenharmony_ci<span class="hljs-comment">// Different zeros:</span> 11351cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>); 11361cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 11371cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 11381cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 11391cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 0</span> 11401cb0ef41Sopenharmony_ci<span class="hljs-comment">// - -0</span> 11411cb0ef41Sopenharmony_ci 11421cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>(); 11431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>(); 11441cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol1]: <span class="hljs-number">1</span> }); 11451cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, because it is the same symbol on both objects.</span> 11461cb0ef41Sopenharmony_ci 11471cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol2]: <span class="hljs-number">1</span> }); 11481cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:</span> 11491cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 11501cb0ef41Sopenharmony_ci<span class="hljs-comment">// {</span> 11511cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Symbol()]: 1</span> 11521cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span> 11531cb0ef41Sopenharmony_ci 11541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>(); 11551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>([[{}, {}]]); 11561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>(); 11571cb0ef41Sopenharmony_ciweakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>; 11581cb0ef41Sopenharmony_ci 11591cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2); 11601cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, because it is impossible to compare the entries</span> 11611cb0ef41Sopenharmony_ci 11621cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:</span> 11631cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3); 11641cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 11651cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 11661cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 11671cb0ef41Sopenharmony_ci<span class="hljs-comment">// WeakMap {</span> 11681cb0ef41Sopenharmony_ci<span class="hljs-comment">// + [items unknown]</span> 11691cb0ef41Sopenharmony_ci<span class="hljs-comment">// - [items unknown],</span> 11701cb0ef41Sopenharmony_ci<span class="hljs-comment">// - unequal: true</span> 11711cb0ef41Sopenharmony_ci<span class="hljs-comment">// }</span></code><button class="copy-button">copy</button></pre> 11721cb0ef41Sopenharmony_ci<p>If the values are not equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 11731cb0ef41Sopenharmony_ciproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code> 11741cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the <code>message</code> 11751cb0ef41Sopenharmony_ciparameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 11761cb0ef41Sopenharmony_ci<code>AssertionError</code>.</p> 11771cb0ef41Sopenharmony_ci</section><section><h3><code>assert.doesNotMatch(string, regexp[, message])</code><span><a class="mark" href="#assertdoesnotmatchstring-regexp-message" id="assertdoesnotmatchstring-regexp-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_doesnotmatch_string_regexp_message"></a></h3> 11781cb0ef41Sopenharmony_ci<div class="api_metadata"> 11791cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11801cb0ef41Sopenharmony_ci<table> 11811cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11821cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 11831cb0ef41Sopenharmony_ci<td><p>This API is no longer experimental.</p></td></tr> 11841cb0ef41Sopenharmony_ci<tr><td>v13.6.0, v12.16.0</td> 11851cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.6.0, v12.16.0</span></p></td></tr> 11861cb0ef41Sopenharmony_ci</tbody></table> 11871cb0ef41Sopenharmony_ci</details> 11881cb0ef41Sopenharmony_ci</div> 11891cb0ef41Sopenharmony_ci<ul> 11901cb0ef41Sopenharmony_ci<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11911cb0ef41Sopenharmony_ci<li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a></li> 11921cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 11931cb0ef41Sopenharmony_ci</ul> 11941cb0ef41Sopenharmony_ci<p>Expects the <code>string</code> input not to match the regular expression.</p> 11951cb0ef41Sopenharmony_ci 11961cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 11971cb0ef41Sopenharmony_ci 11981cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/fail/</span>); 11991cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...</span> 12001cb0ef41Sopenharmony_ci 12011cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>); 12021cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span> 12031cb0ef41Sopenharmony_ci 12041cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/different/</span>); 12051cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/fail/</span>); 12081cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...</span> 12091cb0ef41Sopenharmony_ci 12101cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>); 12111cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span> 12121cb0ef41Sopenharmony_ci 12131cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/different/</span>); 12141cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 12151cb0ef41Sopenharmony_ci<p>If the values do match, or if the <code>string</code> argument is of another type than 12161cb0ef41Sopenharmony_ci<code>string</code>, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal 12171cb0ef41Sopenharmony_cito the value of the <code>message</code> parameter. If the <code>message</code> parameter is 12181cb0ef41Sopenharmony_ciundefined, a default error message is assigned. If the <code>message</code> parameter is an 12191cb0ef41Sopenharmony_ciinstance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 12201cb0ef41Sopenharmony_ci<a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 12211cb0ef41Sopenharmony_ci</section><section><h3><code>assert.doesNotReject(asyncFn[, error][, message])</code><span><a class="mark" href="#assertdoesnotrejectasyncfn-error-message" id="assertdoesnotrejectasyncfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_doesnotreject_asyncfn_error_message"></a></h3> 12221cb0ef41Sopenharmony_ci<div class="api_metadata"> 12231cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 12241cb0ef41Sopenharmony_ci</div> 12251cb0ef41Sopenharmony_ci<ul> 12261cb0ef41Sopenharmony_ci<li><code>asyncFn</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/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 12271cb0ef41Sopenharmony_ci<li><code>error</code> <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/Function" class="type"><Function></a></li> 12281cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12291cb0ef41Sopenharmony_ci</ul> 12301cb0ef41Sopenharmony_ci<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately 12311cb0ef41Sopenharmony_cicalls the function and awaits the returned promise to complete. It will then 12321cb0ef41Sopenharmony_cicheck that the promise is not rejected.</p> 12331cb0ef41Sopenharmony_ci<p>If <code>asyncFn</code> is a function and it throws an error synchronously, 12341cb0ef41Sopenharmony_ci<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If 12351cb0ef41Sopenharmony_cithe function does not return a promise, <code>assert.doesNotReject()</code> will return a 12361cb0ef41Sopenharmony_cirejected <code>Promise</code> with an <a href="errors.html#err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases 12371cb0ef41Sopenharmony_cithe error handler is skipped.</p> 12381cb0ef41Sopenharmony_ci<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little 12391cb0ef41Sopenharmony_cibenefit in catching a rejection and then rejecting it again. Instead, consider 12401cb0ef41Sopenharmony_ciadding a comment next to the specific code path that should not reject and keep 12411cb0ef41Sopenharmony_cierror messages as expressive as possible.</p> 12421cb0ef41Sopenharmony_ci<p>If specified, <code>error</code> can be a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>, or a validation 12431cb0ef41Sopenharmony_cifunction. See <a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a> for more details.</p> 12441cb0ef41Sopenharmony_ci<p>Besides the async nature to await the completion behaves identically to 12451cb0ef41Sopenharmony_ci<a href="#assertdoesnotthrowfn-error-message"><code>assert.doesNotThrow()</code></a>.</p> 12461cb0ef41Sopenharmony_ci 12471cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 12481cb0ef41Sopenharmony_ci 12491cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</span>( 12501cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> () => { 12511cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 12521cb0ef41Sopenharmony_ci }, 12531cb0ef41Sopenharmony_ci <span class="hljs-title class_">SyntaxError</span>, 12541cb0ef41Sopenharmony_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/strict'</span>); 12551cb0ef41Sopenharmony_ci 12561cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => { 12571cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</span>( 12581cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> () => { 12591cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 12601cb0ef41Sopenharmony_ci }, 12611cb0ef41Sopenharmony_ci <span class="hljs-title class_">SyntaxError</span>, 12621cb0ef41Sopenharmony_ci ); 12631cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre> 12641cb0ef41Sopenharmony_ci 12651cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 12661cb0ef41Sopenharmony_ci 12671cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotReject</span>(<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>))) 12681cb0ef41Sopenharmony_ci .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 12691cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 12701cb0ef41Sopenharmony_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/strict'</span>); 12711cb0ef41Sopenharmony_ci 12721cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotReject</span>(<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>))) 12731cb0ef41Sopenharmony_ci .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 12741cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 12751cb0ef41Sopenharmony_ci });</code><button class="copy-button">copy</button></pre> 12761cb0ef41Sopenharmony_ci</section><section><h3><code>assert.doesNotThrow(fn[, error][, message])</code><span><a class="mark" href="#assertdoesnotthrowfn-error-message" id="assertdoesnotthrowfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_doesnotthrow_fn_error_message"></a></h3> 12771cb0ef41Sopenharmony_ci<div class="api_metadata"> 12781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12791cb0ef41Sopenharmony_ci<table> 12801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12811cb0ef41Sopenharmony_ci<tr><td>v5.11.0, v4.4.5</td> 12821cb0ef41Sopenharmony_ci<td><p>The <code>message</code> parameter is respected now.</p></td></tr> 12831cb0ef41Sopenharmony_ci<tr><td>v4.2.0</td> 12841cb0ef41Sopenharmony_ci<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr> 12851cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 12861cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 12871cb0ef41Sopenharmony_ci</tbody></table> 12881cb0ef41Sopenharmony_ci</details> 12891cb0ef41Sopenharmony_ci</div> 12901cb0ef41Sopenharmony_ci<ul> 12911cb0ef41Sopenharmony_ci<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 12921cb0ef41Sopenharmony_ci<li><code>error</code> <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/Function" class="type"><Function></a></li> 12931cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12941cb0ef41Sopenharmony_ci</ul> 12951cb0ef41Sopenharmony_ci<p>Asserts that the function <code>fn</code> does not throw an error.</p> 12961cb0ef41Sopenharmony_ci<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there 12971cb0ef41Sopenharmony_ciis no benefit in catching an error and then rethrowing it. Instead, consider 12981cb0ef41Sopenharmony_ciadding a comment next to the specific code path that should not throw and keep 12991cb0ef41Sopenharmony_cierror messages as expressive as possible.</p> 13001cb0ef41Sopenharmony_ci<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code> 13011cb0ef41Sopenharmony_cifunction.</p> 13021cb0ef41Sopenharmony_ci<p>If an error is thrown and it is the same type as that specified by the <code>error</code> 13031cb0ef41Sopenharmony_ciparameter, then an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown. If the error is of a 13041cb0ef41Sopenharmony_cidifferent type, or if the <code>error</code> parameter is undefined, the error is 13051cb0ef41Sopenharmony_cipropagated back to the caller.</p> 13061cb0ef41Sopenharmony_ci<p>If specified, <code>error</code> can be a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>, or a validation 13071cb0ef41Sopenharmony_cifunction. See <a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a> for more details.</p> 13081cb0ef41Sopenharmony_ci<p>The following, for instance, will throw the <a href="errors.html#class-typeerror"><code>TypeError</code></a> because there is no 13091cb0ef41Sopenharmony_cimatching error type in the assertion:</p> 13101cb0ef41Sopenharmony_ci 13111cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 13121cb0ef41Sopenharmony_ci 13131cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotThrow</span>( 13141cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 13151cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 13161cb0ef41Sopenharmony_ci }, 13171cb0ef41Sopenharmony_ci <span class="hljs-title class_">SyntaxError</span>, 13181cb0ef41Sopenharmony_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/strict'</span>); 13191cb0ef41Sopenharmony_ci 13201cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotThrow</span>( 13211cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 13221cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 13231cb0ef41Sopenharmony_ci }, 13241cb0ef41Sopenharmony_ci <span class="hljs-title class_">SyntaxError</span>, 13251cb0ef41Sopenharmony_ci);</code><button class="copy-button">copy</button></pre> 13261cb0ef41Sopenharmony_ci<p>However, the following will result in an <a href="#class-assertassertionerror"><code>AssertionError</code></a> with the message 13271cb0ef41Sopenharmony_ci'Got unwanted exception...':</p> 13281cb0ef41Sopenharmony_ci 13291cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 13301cb0ef41Sopenharmony_ci 13311cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotThrow</span>( 13321cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 13331cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 13341cb0ef41Sopenharmony_ci }, 13351cb0ef41Sopenharmony_ci <span class="hljs-title class_">TypeError</span>, 13361cb0ef41Sopenharmony_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/strict'</span>); 13371cb0ef41Sopenharmony_ci 13381cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotThrow</span>( 13391cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 13401cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 13411cb0ef41Sopenharmony_ci }, 13421cb0ef41Sopenharmony_ci <span class="hljs-title class_">TypeError</span>, 13431cb0ef41Sopenharmony_ci);</code><button class="copy-button">copy</button></pre> 13441cb0ef41Sopenharmony_ci<p>If an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown and a value is provided for the <code>message</code> 13451cb0ef41Sopenharmony_ciparameter, the value of <code>message</code> will be appended to the <a href="#class-assertassertionerror"><code>AssertionError</code></a> 13461cb0ef41Sopenharmony_cimessage:</p> 13471cb0ef41Sopenharmony_ci 13481cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 13491cb0ef41Sopenharmony_ci 13501cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotThrow</span>( 13511cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 13521cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 13531cb0ef41Sopenharmony_ci }, 13541cb0ef41Sopenharmony_ci <span class="hljs-regexp">/Wrong value/</span>, 13551cb0ef41Sopenharmony_ci <span class="hljs-string">'Whoops'</span>, 13561cb0ef41Sopenharmony_ci); 13571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 13581cb0ef41Sopenharmony_ci 13591cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">doesNotThrow</span>( 13601cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 13611cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 13621cb0ef41Sopenharmony_ci }, 13631cb0ef41Sopenharmony_ci <span class="hljs-regexp">/Wrong value/</span>, 13641cb0ef41Sopenharmony_ci <span class="hljs-string">'Whoops'</span>, 13651cb0ef41Sopenharmony_ci); 13661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><button class="copy-button">copy</button></pre> 13671cb0ef41Sopenharmony_ci</section><section><h3><code>assert.equal(actual, expected[, message])</code><span><a class="mark" href="#assertequalactual-expected-message" id="assertequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_equal_actual_expected_message"></a></h3> 13681cb0ef41Sopenharmony_ci<div class="api_metadata"> 13691cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13701cb0ef41Sopenharmony_ci<table> 13711cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13721cb0ef41Sopenharmony_ci<tr><td>v16.0.0, v14.18.0</td> 13731cb0ef41Sopenharmony_ci<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 13741cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 13751cb0ef41Sopenharmony_ci<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr> 13761cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 13771cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 13781cb0ef41Sopenharmony_ci</tbody></table> 13791cb0ef41Sopenharmony_ci</details> 13801cb0ef41Sopenharmony_ci</div> 13811cb0ef41Sopenharmony_ci<ul> 13821cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 13831cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 13841cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 13851cb0ef41Sopenharmony_ci</ul> 13861cb0ef41Sopenharmony_ci<p><strong>Strict assertion mode</strong></p> 13871cb0ef41Sopenharmony_ci<p>An alias of <a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a>.</p> 13881cb0ef41Sopenharmony_ci<p><strong>Legacy assertion mode</strong></p> 13891cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a> instead.</div><p></p> 13901cb0ef41Sopenharmony_ci<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters 13911cb0ef41Sopenharmony_ciusing the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a>. <code>NaN</code> is specially handled 13921cb0ef41Sopenharmony_ciand treated as being identical if both sides are <code>NaN</code>.</p> 13931cb0ef41Sopenharmony_ci 13941cb0ef41Sopenharmony_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>; 13951cb0ef41Sopenharmony_ci 13961cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 13971cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, 1 == 1</span> 13981cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 13991cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, 1 == '1'</span> 14001cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>); 14011cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 14021cb0ef41Sopenharmony_ci 14031cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 14041cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: 1 == 2</span> 14051cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>({ <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> } }, { <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> } }); 14061cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 14071cb0ef41Sopenharmony_ci 14081cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 14091cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, 1 == 1</span> 14101cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 14111cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK, 1 == '1'</span> 14121cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>); 14131cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 14141cb0ef41Sopenharmony_ci 14151cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 14161cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: 1 == 2</span> 14171cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">equal</span>({ <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> } }, { <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> } }); 14181cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code><button class="copy-button">copy</button></pre> 14191cb0ef41Sopenharmony_ci<p>If the values are not equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 14201cb0ef41Sopenharmony_ciproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code> 14211cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the <code>message</code> 14221cb0ef41Sopenharmony_ciparameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 14231cb0ef41Sopenharmony_ci<code>AssertionError</code>.</p> 14241cb0ef41Sopenharmony_ci</section><section><h3><code>assert.fail([message])</code><span><a class="mark" href="#assertfailmessage" id="assertfailmessage">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_fail_message"></a></h3> 14251cb0ef41Sopenharmony_ci<div class="api_metadata"> 14261cb0ef41Sopenharmony_ci<span>Added in: v0.1.21</span> 14271cb0ef41Sopenharmony_ci</div> 14281cb0ef41Sopenharmony_ci<ul> 14291cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a> <strong>Default:</strong> <code>'Failed'</code></li> 14301cb0ef41Sopenharmony_ci</ul> 14311cb0ef41Sopenharmony_ci<p>Throws an <a href="#class-assertassertionerror"><code>AssertionError</code></a> with the provided error message or a default 14321cb0ef41Sopenharmony_cierror message. If the <code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then 14331cb0ef41Sopenharmony_ciit will be thrown instead of the <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 14361cb0ef41Sopenharmony_ci 14371cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(); 14381cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span> 14391cb0ef41Sopenharmony_ci 14401cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>); 14411cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span> 14421cb0ef41Sopenharmony_ci 14431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'need array'</span>)); 14441cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError: need array</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 14451cb0ef41Sopenharmony_ci 14461cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(); 14471cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span> 14481cb0ef41Sopenharmony_ci 14491cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>); 14501cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span> 14511cb0ef41Sopenharmony_ci 14521cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'need array'</span>)); 14531cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError: need array</span></code><button class="copy-button">copy</button></pre> 14541cb0ef41Sopenharmony_ci<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated. 14551cb0ef41Sopenharmony_ciSee below for further details.</p> 14561cb0ef41Sopenharmony_ci</section><section><h3><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code><span><a class="mark" href="#assertfailactual-expected-message-operator-stackstartfn" id="assertfailactual-expected-message-operator-stackstartfn">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_fail_actual_expected_message_operator_stackstartfn"></a></h3> 14571cb0ef41Sopenharmony_ci<div class="api_metadata"> 14581cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14591cb0ef41Sopenharmony_ci<table> 14601cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14611cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 14621cb0ef41Sopenharmony_ci<td><p>Calling <code>assert.fail()</code> with more than one argument is deprecated and emits a warning.</p></td></tr> 14631cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 14641cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 14651cb0ef41Sopenharmony_ci</tbody></table> 14661cb0ef41Sopenharmony_ci</details> 14671cb0ef41Sopenharmony_ci</div> 14681cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>assert.fail([message])</code> or other assert 14691cb0ef41Sopenharmony_cifunctions instead.</div><p></p> 14701cb0ef41Sopenharmony_ci<ul> 14711cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 14721cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 14731cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 14741cb0ef41Sopenharmony_ci<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'!='</code></li> 14751cb0ef41Sopenharmony_ci<li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <strong>Default:</strong> <code>assert.fail</code></li> 14761cb0ef41Sopenharmony_ci</ul> 14771cb0ef41Sopenharmony_ci<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and 14781cb0ef41Sopenharmony_ci<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and 14791cb0ef41Sopenharmony_ci<code>expected</code> arguments are provided, <code>operator</code> will default to <code>'!='</code>. If 14801cb0ef41Sopenharmony_ci<code>message</code> is provided as third argument it will be used as the error message and 14811cb0ef41Sopenharmony_cithe other arguments will be stored as properties on the thrown object. If 14821cb0ef41Sopenharmony_ci<code>stackStartFn</code> is provided, all stack frames above that function will be 14831cb0ef41Sopenharmony_ciremoved from stacktrace (see <a href="errors.html#errorcapturestacktracetargetobject-constructoropt"><code>Error.captureStackTrace</code></a>). If no arguments are 14841cb0ef41Sopenharmony_cigiven, the default message <code>Failed</code> will be used.</p> 14851cb0ef41Sopenharmony_ci 14861cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 14871cb0ef41Sopenharmony_ci 14881cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>); 14891cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span> 14901cb0ef41Sopenharmony_ci 14911cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'>'</span>); 14921cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span> 14931cb0ef41Sopenharmony_ci 14941cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'fail'</span>); 14951cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span> 14961cb0ef41Sopenharmony_ci 14971cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'whoops'</span>, <span class="hljs-string">'>'</span>); 14981cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span> 14991cb0ef41Sopenharmony_ci 15001cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'need array'</span>)); 15011cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError: need array</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 15021cb0ef41Sopenharmony_ci 15031cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>); 15041cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span> 15051cb0ef41Sopenharmony_ci 15061cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'>'</span>); 15071cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span> 15081cb0ef41Sopenharmony_ci 15091cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'fail'</span>); 15101cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span> 15111cb0ef41Sopenharmony_ci 15121cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'whoops'</span>, <span class="hljs-string">'>'</span>); 15131cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span> 15141cb0ef41Sopenharmony_ci 15151cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'need array'</span>)); 15161cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError: need array</span></code><button class="copy-button">copy</button></pre> 15171cb0ef41Sopenharmony_ci<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no 15181cb0ef41Sopenharmony_ciinfluence on the error message.</p> 15191cb0ef41Sopenharmony_ci<p>Example use of <code>stackStartFn</code> for truncating the exception's stacktrace:</p> 15201cb0ef41Sopenharmony_ci 15211cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 15221cb0ef41Sopenharmony_ci 15231cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">suppressFrame</span>(<span class="hljs-params"></span>) { 15241cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'!=='</span>, suppressFrame); 15251cb0ef41Sopenharmony_ci} 15261cb0ef41Sopenharmony_ci<span class="hljs-title function_">suppressFrame</span>(); 15271cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span> 15281cb0ef41Sopenharmony_ci<span class="hljs-comment">// at repl:1:1</span> 15291cb0ef41Sopenharmony_ci<span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span> 15301cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 15311cb0ef41Sopenharmony_ci 15321cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">suppressFrame</span>(<span class="hljs-params"></span>) { 15331cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'!=='</span>, suppressFrame); 15341cb0ef41Sopenharmony_ci} 15351cb0ef41Sopenharmony_ci<span class="hljs-title function_">suppressFrame</span>(); 15361cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span> 15371cb0ef41Sopenharmony_ci<span class="hljs-comment">// at repl:1:1</span> 15381cb0ef41Sopenharmony_ci<span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span> 15391cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span></code><button class="copy-button">copy</button></pre> 15401cb0ef41Sopenharmony_ci</section><section><h3><code>assert.ifError(value)</code><span><a class="mark" href="#assertiferrorvalue" id="assertiferrorvalue">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_iferror_value"></a></h3> 15411cb0ef41Sopenharmony_ci<div class="api_metadata"> 15421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 15431cb0ef41Sopenharmony_ci<table> 15441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 15451cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 15461cb0ef41Sopenharmony_ci<td><p>Instead of throwing the original error it is now wrapped into an [<code>AssertionError</code>][] that contains the full stack trace.</p></td></tr> 15471cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 15481cb0ef41Sopenharmony_ci<td><p>Value may now only be <code>undefined</code> or <code>null</code>. Before all falsy values were handled the same as <code>null</code> and did not throw.</p></td></tr> 15491cb0ef41Sopenharmony_ci<tr><td>v0.1.97</td> 15501cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.97</span></p></td></tr> 15511cb0ef41Sopenharmony_ci</tbody></table> 15521cb0ef41Sopenharmony_ci</details> 15531cb0ef41Sopenharmony_ci</div> 15541cb0ef41Sopenharmony_ci<ul> 15551cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 15561cb0ef41Sopenharmony_ci</ul> 15571cb0ef41Sopenharmony_ci<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when 15581cb0ef41Sopenharmony_citesting the <code>error</code> argument in callbacks. The stack trace contains all frames 15591cb0ef41Sopenharmony_cifrom the error passed to <code>ifError()</code> including the potential new frames for 15601cb0ef41Sopenharmony_ci<code>ifError()</code> itself.</p> 15611cb0ef41Sopenharmony_ci 15621cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 15631cb0ef41Sopenharmony_ci 15641cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>); 15651cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 15661cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>); 15671cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</span> 15681cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>); 15691cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'</span> 15701cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>()); 15711cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error</span> 15721cb0ef41Sopenharmony_ci 15731cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create some random error frames.</span> 15741cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> err; 15751cb0ef41Sopenharmony_ci(<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<span class="hljs-params"></span>) { 15761cb0ef41Sopenharmony_ci err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'test error'</span>); 15771cb0ef41Sopenharmony_ci})(); 15781cb0ef41Sopenharmony_ci 15791cb0ef41Sopenharmony_ci(<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(<span class="hljs-params"></span>) { 15801cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ifError</span>(err); 15811cb0ef41Sopenharmony_ci})(); 15821cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error</span> 15831cb0ef41Sopenharmony_ci<span class="hljs-comment">// at ifErrorFrame</span> 15841cb0ef41Sopenharmony_ci<span class="hljs-comment">// at errorFrame</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 15851cb0ef41Sopenharmony_ci 15861cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>); 15871cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 15881cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>); 15891cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</span> 15901cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>); 15911cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'</span> 15921cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>()); 15931cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error</span> 15941cb0ef41Sopenharmony_ci 15951cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create some random error frames.</span> 15961cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> err; 15971cb0ef41Sopenharmony_ci(<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<span class="hljs-params"></span>) { 15981cb0ef41Sopenharmony_ci err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'test error'</span>); 15991cb0ef41Sopenharmony_ci})(); 16001cb0ef41Sopenharmony_ci 16011cb0ef41Sopenharmony_ci(<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(<span class="hljs-params"></span>) { 16021cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">ifError</span>(err); 16031cb0ef41Sopenharmony_ci})(); 16041cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error</span> 16051cb0ef41Sopenharmony_ci<span class="hljs-comment">// at ifErrorFrame</span> 16061cb0ef41Sopenharmony_ci<span class="hljs-comment">// at errorFrame</span></code><button class="copy-button">copy</button></pre> 16071cb0ef41Sopenharmony_ci</section><section><h3><code>assert.match(string, regexp[, message])</code><span><a class="mark" href="#assertmatchstring-regexp-message" id="assertmatchstring-regexp-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_match_string_regexp_message"></a></h3> 16081cb0ef41Sopenharmony_ci<div class="api_metadata"> 16091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 16101cb0ef41Sopenharmony_ci<table> 16111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 16121cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 16131cb0ef41Sopenharmony_ci<td><p>This API is no longer experimental.</p></td></tr> 16141cb0ef41Sopenharmony_ci<tr><td>v13.6.0, v12.16.0</td> 16151cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.6.0, v12.16.0</span></p></td></tr> 16161cb0ef41Sopenharmony_ci</tbody></table> 16171cb0ef41Sopenharmony_ci</details> 16181cb0ef41Sopenharmony_ci</div> 16191cb0ef41Sopenharmony_ci<ul> 16201cb0ef41Sopenharmony_ci<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 16211cb0ef41Sopenharmony_ci<li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a></li> 16221cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 16231cb0ef41Sopenharmony_ci</ul> 16241cb0ef41Sopenharmony_ci<p>Expects the <code>string</code> input to match the regular expression.</p> 16251cb0ef41Sopenharmony_ci 16261cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 16271cb0ef41Sopenharmony_ci 16281cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/pass/</span>); 16291cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular ...</span> 16301cb0ef41Sopenharmony_ci 16311cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>); 16321cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span> 16331cb0ef41Sopenharmony_ci 16341cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/pass/</span>); 16351cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 16361cb0ef41Sopenharmony_ci 16371cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/pass/</span>); 16381cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular ...</span> 16391cb0ef41Sopenharmony_ci 16401cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>); 16411cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span> 16421cb0ef41Sopenharmony_ci 16431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/pass/</span>); 16441cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 16451cb0ef41Sopenharmony_ci<p>If the values do not match, or if the <code>string</code> argument is of another type than 16461cb0ef41Sopenharmony_ci<code>string</code>, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal 16471cb0ef41Sopenharmony_cito the value of the <code>message</code> parameter. If the <code>message</code> parameter is 16481cb0ef41Sopenharmony_ciundefined, a default error message is assigned. If the <code>message</code> parameter is an 16491cb0ef41Sopenharmony_ciinstance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 16501cb0ef41Sopenharmony_ci<a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 16511cb0ef41Sopenharmony_ci</section><section><h3><code>assert.notDeepEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotdeepequalactual-expected-message" id="assertnotdeepequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notdeepequal_actual_expected_message"></a></h3> 16521cb0ef41Sopenharmony_ci<div class="api_metadata"> 16531cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 16541cb0ef41Sopenharmony_ci<table> 16551cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 16561cb0ef41Sopenharmony_ci<tr><td>v16.0.0, v14.18.0</td> 16571cb0ef41Sopenharmony_ci<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 16581cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 16591cb0ef41Sopenharmony_ci<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr> 16601cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 16611cb0ef41Sopenharmony_ci<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 16621cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 16631cb0ef41Sopenharmony_ci<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 16641cb0ef41Sopenharmony_ci<tr><td>v6.4.0, v4.7.1</td> 16651cb0ef41Sopenharmony_ci<td><p>Typed array slices are handled correctly now.</p></td></tr> 16661cb0ef41Sopenharmony_ci<tr><td>v6.1.0, v4.5.0</td> 16671cb0ef41Sopenharmony_ci<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 16681cb0ef41Sopenharmony_ci<tr><td>v5.10.1, v4.4.3</td> 16691cb0ef41Sopenharmony_ci<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 16701cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 16711cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 16721cb0ef41Sopenharmony_ci</tbody></table> 16731cb0ef41Sopenharmony_ci</details> 16741cb0ef41Sopenharmony_ci</div> 16751cb0ef41Sopenharmony_ci<ul> 16761cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 16771cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 16781cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 16791cb0ef41Sopenharmony_ci</ul> 16801cb0ef41Sopenharmony_ci<p><strong>Strict assertion mode</strong></p> 16811cb0ef41Sopenharmony_ci<p>An alias of <a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual()</code></a>.</p> 16821cb0ef41Sopenharmony_ci<p><strong>Legacy assertion mode</strong></p> 16831cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual()</code></a> instead.</div><p></p> 16841cb0ef41Sopenharmony_ci<p>Tests for any deep inequality. Opposite of <a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a>.</p> 16851cb0ef41Sopenharmony_ci 16861cb0ef41Sopenharmony_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>; 16871cb0ef41Sopenharmony_ci 16881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj1 = { 16891cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 16901cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 16911cb0ef41Sopenharmony_ci }, 16921cb0ef41Sopenharmony_ci}; 16931cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = { 16941cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 16951cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>, 16961cb0ef41Sopenharmony_ci }, 16971cb0ef41Sopenharmony_ci}; 16981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj3 = { 16991cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 17001cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 17011cb0ef41Sopenharmony_ci }, 17021cb0ef41Sopenharmony_ci}; 17031cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1); 17041cb0ef41Sopenharmony_ci 17051cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1); 17061cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span> 17071cb0ef41Sopenharmony_ci 17081cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2); 17091cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 17101cb0ef41Sopenharmony_ci 17111cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3); 17121cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span> 17131cb0ef41Sopenharmony_ci 17141cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4); 17151cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 17161cb0ef41Sopenharmony_ci 17171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj1 = { 17181cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 17191cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 17201cb0ef41Sopenharmony_ci }, 17211cb0ef41Sopenharmony_ci}; 17221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = { 17231cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 17241cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>, 17251cb0ef41Sopenharmony_ci }, 17261cb0ef41Sopenharmony_ci}; 17271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj3 = { 17281cb0ef41Sopenharmony_ci <span class="hljs-attr">a</span>: { 17291cb0ef41Sopenharmony_ci <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>, 17301cb0ef41Sopenharmony_ci }, 17311cb0ef41Sopenharmony_ci}; 17321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1); 17331cb0ef41Sopenharmony_ci 17341cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1); 17351cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span> 17361cb0ef41Sopenharmony_ci 17371cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2); 17381cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 17391cb0ef41Sopenharmony_ci 17401cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3); 17411cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span> 17421cb0ef41Sopenharmony_ci 17431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4); 17441cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 17451cb0ef41Sopenharmony_ci<p>If the values are deeply equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a 17461cb0ef41Sopenharmony_ci<code>message</code> property set equal to the value of the <code>message</code> parameter. If the 17471cb0ef41Sopenharmony_ci<code>message</code> parameter is undefined, a default error message is assigned. If the 17481cb0ef41Sopenharmony_ci<code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown 17491cb0ef41Sopenharmony_ciinstead of the <code>AssertionError</code>.</p> 17501cb0ef41Sopenharmony_ci</section><section><h3><code>assert.notDeepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotdeepstrictequalactual-expected-message" id="assertnotdeepstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notdeepstrictequal_actual_expected_message"></a></h3> 17511cb0ef41Sopenharmony_ci<div class="api_metadata"> 17521cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17531cb0ef41Sopenharmony_ci<table> 17541cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17551cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 17561cb0ef41Sopenharmony_ci<td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr> 17571cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 17581cb0ef41Sopenharmony_ci<td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-samevaluezero">SameValueZero</a> comparison.</p></td></tr> 17591cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 17601cb0ef41Sopenharmony_ci<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 17611cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 17621cb0ef41Sopenharmony_ci<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 17631cb0ef41Sopenharmony_ci<tr><td>v6.1.0</td> 17641cb0ef41Sopenharmony_ci<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 17651cb0ef41Sopenharmony_ci<tr><td>v6.4.0, v4.7.1</td> 17661cb0ef41Sopenharmony_ci<td><p>Typed array slices are handled correctly now.</p></td></tr> 17671cb0ef41Sopenharmony_ci<tr><td>v5.10.1, v4.4.3</td> 17681cb0ef41Sopenharmony_ci<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 17691cb0ef41Sopenharmony_ci<tr><td>v1.2.0</td> 17701cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.2.0</span></p></td></tr> 17711cb0ef41Sopenharmony_ci</tbody></table> 17721cb0ef41Sopenharmony_ci</details> 17731cb0ef41Sopenharmony_ci</div> 17741cb0ef41Sopenharmony_ci<ul> 17751cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 17761cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 17771cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 17781cb0ef41Sopenharmony_ci</ul> 17791cb0ef41Sopenharmony_ci<p>Tests for deep strict inequality. Opposite of <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a>.</p> 17801cb0ef41Sopenharmony_ci 17811cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 17821cb0ef41Sopenharmony_ci 17831cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'1'</span> }); 17841cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 17851cb0ef41Sopenharmony_ci 17861cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notDeepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'1'</span> }); 17871cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 17881cb0ef41Sopenharmony_ci<p>If the values are deeply and strictly equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown 17891cb0ef41Sopenharmony_ciwith a <code>message</code> property set equal to the value of the <code>message</code> parameter. If 17901cb0ef41Sopenharmony_cithe <code>message</code> parameter is undefined, a default error message is assigned. If 17911cb0ef41Sopenharmony_cithe <code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown 17921cb0ef41Sopenharmony_ciinstead of the <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 17931cb0ef41Sopenharmony_ci</section><section><h3><code>assert.notEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotequalactual-expected-message" id="assertnotequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notequal_actual_expected_message"></a></h3> 17941cb0ef41Sopenharmony_ci<div class="api_metadata"> 17951cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17961cb0ef41Sopenharmony_ci<table> 17971cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17981cb0ef41Sopenharmony_ci<tr><td>v16.0.0, v14.18.0</td> 17991cb0ef41Sopenharmony_ci<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 18001cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 18011cb0ef41Sopenharmony_ci<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr> 18021cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 18031cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 18041cb0ef41Sopenharmony_ci</tbody></table> 18051cb0ef41Sopenharmony_ci</details> 18061cb0ef41Sopenharmony_ci</div> 18071cb0ef41Sopenharmony_ci<ul> 18081cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 18091cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 18101cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 18111cb0ef41Sopenharmony_ci</ul> 18121cb0ef41Sopenharmony_ci<p><strong>Strict assertion mode</strong></p> 18131cb0ef41Sopenharmony_ci<p>An alias of <a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual()</code></a>.</p> 18141cb0ef41Sopenharmony_ci<p><strong>Legacy assertion mode</strong></p> 18151cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual()</code></a> instead.</div><p></p> 18161cb0ef41Sopenharmony_ci<p>Tests shallow, coercive inequality with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality"><code>!=</code> operator</a>. <code>NaN</code> is 18171cb0ef41Sopenharmony_cispecially handled and treated as being identical if both sides are <code>NaN</code>.</p> 18181cb0ef41Sopenharmony_ci 18191cb0ef41Sopenharmony_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>; 18201cb0ef41Sopenharmony_ci 18211cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 18221cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 18231cb0ef41Sopenharmony_ci 18241cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 18251cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: 1 != 1</span> 18261cb0ef41Sopenharmony_ci 18271cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 18281cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: 1 != '1'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 18291cb0ef41Sopenharmony_ci 18301cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 18311cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 18321cb0ef41Sopenharmony_ci 18331cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 18341cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: 1 != 1</span> 18351cb0ef41Sopenharmony_ci 18361cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 18371cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: 1 != '1'</span></code><button class="copy-button">copy</button></pre> 18381cb0ef41Sopenharmony_ci<p>If the values are equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 18391cb0ef41Sopenharmony_ciproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code> 18401cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the <code>message</code> 18411cb0ef41Sopenharmony_ciparameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 18421cb0ef41Sopenharmony_ci<code>AssertionError</code>.</p> 18431cb0ef41Sopenharmony_ci</section><section><h3><code>assert.notStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotstrictequalactual-expected-message" id="assertnotstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notstrictequal_actual_expected_message"></a></h3> 18441cb0ef41Sopenharmony_ci<div class="api_metadata"> 18451cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 18461cb0ef41Sopenharmony_ci<table> 18471cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 18481cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 18491cb0ef41Sopenharmony_ci<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code>.</p></td></tr> 18501cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 18511cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 18521cb0ef41Sopenharmony_ci</tbody></table> 18531cb0ef41Sopenharmony_ci</details> 18541cb0ef41Sopenharmony_ci</div> 18551cb0ef41Sopenharmony_ci<ul> 18561cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 18571cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 18581cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 18591cb0ef41Sopenharmony_ci</ul> 18601cb0ef41Sopenharmony_ci<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as 18611cb0ef41Sopenharmony_cidetermined by <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</p> 18621cb0ef41Sopenharmony_ci 18631cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 18641cb0ef41Sopenharmony_ci 18651cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 18661cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 18671cb0ef41Sopenharmony_ci 18681cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 18691cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:</span> 18701cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 18711cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1</span> 18721cb0ef41Sopenharmony_ci 18731cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 18741cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 18751cb0ef41Sopenharmony_ci 18761cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 18771cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 18781cb0ef41Sopenharmony_ci 18791cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 18801cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:</span> 18811cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 18821cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1</span> 18831cb0ef41Sopenharmony_ci 18841cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 18851cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 18861cb0ef41Sopenharmony_ci<p>If the values are strictly equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a 18871cb0ef41Sopenharmony_ci<code>message</code> property set equal to the value of the <code>message</code> parameter. If the 18881cb0ef41Sopenharmony_ci<code>message</code> parameter is undefined, a default error message is assigned. If the 18891cb0ef41Sopenharmony_ci<code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown 18901cb0ef41Sopenharmony_ciinstead of the <code>AssertionError</code>.</p> 18911cb0ef41Sopenharmony_ci</section><section><h3><code>assert.ok(value[, message])</code><span><a class="mark" href="#assertokvalue-message" id="assertokvalue-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_ok_value_message"></a></h3> 18921cb0ef41Sopenharmony_ci<div class="api_metadata"> 18931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 18941cb0ef41Sopenharmony_ci<table> 18951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 18961cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 18971cb0ef41Sopenharmony_ci<td><p>The <code>assert.ok()</code> (no arguments) will now use a predefined error message.</p></td></tr> 18981cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 18991cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 19001cb0ef41Sopenharmony_ci</tbody></table> 19011cb0ef41Sopenharmony_ci</details> 19021cb0ef41Sopenharmony_ci</div> 19031cb0ef41Sopenharmony_ci<ul> 19041cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 19051cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 19061cb0ef41Sopenharmony_ci</ul> 19071cb0ef41Sopenharmony_ci<p>Tests if <code>value</code> is truthy. It is equivalent to 19081cb0ef41Sopenharmony_ci<code>assert.equal(!!value, true, message)</code>.</p> 19091cb0ef41Sopenharmony_ci<p>If <code>value</code> is not truthy, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 19101cb0ef41Sopenharmony_ciproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code> 19111cb0ef41Sopenharmony_ciparameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code> 19121cb0ef41Sopenharmony_ciparameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the 19131cb0ef41Sopenharmony_ci<code>AssertionError</code>. 19141cb0ef41Sopenharmony_ciIf no arguments are passed in at all <code>message</code> will be set to the string: 19151cb0ef41Sopenharmony_ci<code>'No value argument passed to `assert.ok()`'</code>.</p> 19161cb0ef41Sopenharmony_ci<p>Be aware that in the <code>repl</code> the error message will be different to the one 19171cb0ef41Sopenharmony_cithrown in a file! See below for further details.</p> 19181cb0ef41Sopenharmony_ci 19191cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 19201cb0ef41Sopenharmony_ci 19211cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>); 19221cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 19231cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>); 19241cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 19251cb0ef41Sopenharmony_ci 19261cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(); 19271cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span> 19281cb0ef41Sopenharmony_ci 19291cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span class="hljs-string">'it\'s false'</span>); 19301cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: it's false</span> 19311cb0ef41Sopenharmony_ci 19321cb0ef41Sopenharmony_ci<span class="hljs-comment">// In the repl:</span> 19331cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">123</span> === <span class="hljs-string">'string'</span>); 19341cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: false == true</span> 19351cb0ef41Sopenharmony_ci 19361cb0ef41Sopenharmony_ci<span class="hljs-comment">// In a file (e.g. test.js):</span> 19371cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">123</span> === <span class="hljs-string">'string'</span>); 19381cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19391cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19401cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span> 19411cb0ef41Sopenharmony_ci 19421cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>); 19431cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19441cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19451cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert.ok(false)</span> 19461cb0ef41Sopenharmony_ci 19471cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>); 19481cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19491cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19501cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert.ok(0)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 19511cb0ef41Sopenharmony_ci 19521cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>); 19531cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 19541cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>); 19551cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 19561cb0ef41Sopenharmony_ci 19571cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(); 19581cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span> 19591cb0ef41Sopenharmony_ci 19601cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span class="hljs-string">'it\'s false'</span>); 19611cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: it's false</span> 19621cb0ef41Sopenharmony_ci 19631cb0ef41Sopenharmony_ci<span class="hljs-comment">// In the repl:</span> 19641cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">123</span> === <span class="hljs-string">'string'</span>); 19651cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: false == true</span> 19661cb0ef41Sopenharmony_ci 19671cb0ef41Sopenharmony_ci<span class="hljs-comment">// In a file (e.g. test.js):</span> 19681cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">123</span> === <span class="hljs-string">'string'</span>); 19691cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19701cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19711cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span> 19721cb0ef41Sopenharmony_ci 19731cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>); 19741cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19751cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19761cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert.ok(false)</span> 19771cb0ef41Sopenharmony_ci 19781cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>); 19791cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19801cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19811cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert.ok(0)</span></code><button class="copy-button">copy</button></pre> 19821cb0ef41Sopenharmony_ci 19831cb0ef41Sopenharmony_ci<pre class="with-40-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/strict'</span>; 19841cb0ef41Sopenharmony_ci 19851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using `assert()` works the same:</span> 19861cb0ef41Sopenharmony_ci<span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>); 19871cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19881cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19891cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert(0)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 19901cb0ef41Sopenharmony_ci 19911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using `assert()` works the same:</span> 19921cb0ef41Sopenharmony_ci<span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>); 19931cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 19941cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 19951cb0ef41Sopenharmony_ci<span class="hljs-comment">// assert(0)</span></code><button class="copy-button">copy</button></pre> 19961cb0ef41Sopenharmony_ci</section><section><h3><code>assert.rejects(asyncFn[, error][, message])</code><span><a class="mark" href="#assertrejectsasyncfn-error-message" id="assertrejectsasyncfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_rejects_asyncfn_error_message"></a></h3> 19971cb0ef41Sopenharmony_ci<div class="api_metadata"> 19981cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 19991cb0ef41Sopenharmony_ci</div> 20001cb0ef41Sopenharmony_ci<ul> 20011cb0ef41Sopenharmony_ci<li><code>asyncFn</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/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 20021cb0ef41Sopenharmony_ci<li><code>error</code> <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/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 20031cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 20041cb0ef41Sopenharmony_ci</ul> 20051cb0ef41Sopenharmony_ci<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately 20061cb0ef41Sopenharmony_cicalls the function and awaits the returned promise to complete. It will then 20071cb0ef41Sopenharmony_cicheck that the promise is rejected.</p> 20081cb0ef41Sopenharmony_ci<p>If <code>asyncFn</code> is a function and it throws an error synchronously, 20091cb0ef41Sopenharmony_ci<code>assert.rejects()</code> will return a rejected <code>Promise</code> with that error. If the 20101cb0ef41Sopenharmony_cifunction does not return a promise, <code>assert.rejects()</code> will return a rejected 20111cb0ef41Sopenharmony_ci<code>Promise</code> with an <a href="errors.html#err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases the error 20121cb0ef41Sopenharmony_cihandler is skipped.</p> 20131cb0ef41Sopenharmony_ci<p>Besides the async nature to await the completion behaves identically to 20141cb0ef41Sopenharmony_ci<a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a>.</p> 20151cb0ef41Sopenharmony_ci<p>If specified, <code>error</code> can be a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>, a validation function, 20161cb0ef41Sopenharmony_cian object where each property will be tested for, or an instance of error where 20171cb0ef41Sopenharmony_cieach property will be tested for including the non-enumerable <code>message</code> and 20181cb0ef41Sopenharmony_ci<code>name</code> properties.</p> 20191cb0ef41Sopenharmony_ci<p>If specified, <code>message</code> will be the message provided by the <a href="#class-assertassertionerror"><code>AssertionError</code></a> 20201cb0ef41Sopenharmony_ciif the <code>asyncFn</code> fails to reject.</p> 20211cb0ef41Sopenharmony_ci 20221cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 20231cb0ef41Sopenharmony_ci 20241cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>( 20251cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> () => { 20261cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 20271cb0ef41Sopenharmony_ci }, 20281cb0ef41Sopenharmony_ci { 20291cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>, 20301cb0ef41Sopenharmony_ci <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>, 20311cb0ef41Sopenharmony_ci }, 20321cb0ef41Sopenharmony_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/strict'</span>); 20331cb0ef41Sopenharmony_ci 20341cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => { 20351cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>( 20361cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> () => { 20371cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 20381cb0ef41Sopenharmony_ci }, 20391cb0ef41Sopenharmony_ci { 20401cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>, 20411cb0ef41Sopenharmony_ci <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>, 20421cb0ef41Sopenharmony_ci }, 20431cb0ef41Sopenharmony_ci ); 20441cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre> 20451cb0ef41Sopenharmony_ci 20461cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 20471cb0ef41Sopenharmony_ci 20481cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>( 20491cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> () => { 20501cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 20511cb0ef41Sopenharmony_ci }, 20521cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 20531cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'TypeError'</span>); 20541cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, <span class="hljs-string">'Wrong value'</span>); 20551cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 20561cb0ef41Sopenharmony_ci }, 20571cb0ef41Sopenharmony_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/strict'</span>); 20581cb0ef41Sopenharmony_ci 20591cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => { 20601cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>( 20611cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> () => { 20621cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 20631cb0ef41Sopenharmony_ci }, 20641cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 20651cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'TypeError'</span>); 20661cb0ef41Sopenharmony_ci assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, <span class="hljs-string">'Wrong value'</span>); 20671cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 20681cb0ef41Sopenharmony_ci }, 20691cb0ef41Sopenharmony_ci ); 20701cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre> 20711cb0ef41Sopenharmony_ci 20721cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 20731cb0ef41Sopenharmony_ci 20741cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">rejects</span>( 20751cb0ef41Sopenharmony_ci <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>)), 20761cb0ef41Sopenharmony_ci <span class="hljs-title class_">Error</span>, 20771cb0ef41Sopenharmony_ci).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 20781cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 20791cb0ef41Sopenharmony_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/strict'</span>); 20801cb0ef41Sopenharmony_ci 20811cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">rejects</span>( 20821cb0ef41Sopenharmony_ci <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>)), 20831cb0ef41Sopenharmony_ci <span class="hljs-title class_">Error</span>, 20841cb0ef41Sopenharmony_ci).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 20851cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 20861cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 20871cb0ef41Sopenharmony_ci<p><code>error</code> cannot be a string. If a string is provided as the second 20881cb0ef41Sopenharmony_ciargument, then <code>error</code> is assumed to be omitted and the string will be used for 20891cb0ef41Sopenharmony_ci<code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the 20901cb0ef41Sopenharmony_ciexample in <a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a> carefully if using a string as the second 20911cb0ef41Sopenharmony_ciargument gets considered.</p> 20921cb0ef41Sopenharmony_ci</section><section><h3><code>assert.strictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertstrictequalactual-expected-message" id="assertstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_strictequal_actual_expected_message"></a></h3> 20931cb0ef41Sopenharmony_ci<div class="api_metadata"> 20941cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 20951cb0ef41Sopenharmony_ci<table> 20961cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 20971cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 20981cb0ef41Sopenharmony_ci<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code>.</p></td></tr> 20991cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 21001cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 21011cb0ef41Sopenharmony_ci</tbody></table> 21021cb0ef41Sopenharmony_ci</details> 21031cb0ef41Sopenharmony_ci</div> 21041cb0ef41Sopenharmony_ci<ul> 21051cb0ef41Sopenharmony_ci<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 21061cb0ef41Sopenharmony_ci<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 21071cb0ef41Sopenharmony_ci<li><code>message</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/Error" class="type"><Error></a></li> 21081cb0ef41Sopenharmony_ci</ul> 21091cb0ef41Sopenharmony_ci<p>Tests strict equality between the <code>actual</code> and <code>expected</code> parameters as 21101cb0ef41Sopenharmony_cidetermined by <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</p> 21111cb0ef41Sopenharmony_ci 21121cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 21131cb0ef41Sopenharmony_ci 21141cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 21151cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span> 21161cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 21171cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1 !== 2</span> 21181cb0ef41Sopenharmony_ci 21191cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 21201cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 21211cb0ef41Sopenharmony_ci 21221cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello foobar'</span>, <span class="hljs-string">'Hello World!'</span>); 21231cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span> 21241cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 21251cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 21261cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 'Hello foobar'</span> 21271cb0ef41Sopenharmony_ci<span class="hljs-comment">// - 'Hello World!'</span> 21281cb0ef41Sopenharmony_ci<span class="hljs-comment">// ^</span> 21291cb0ef41Sopenharmony_ci 21301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>; 21311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>; 21321cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(apples, oranges, <span class="hljs-string">`apples <span class="hljs-subst">${apples}</span> !== oranges <span class="hljs-subst">${oranges}</span>`</span>); 21331cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span> 21341cb0ef41Sopenharmony_ci 21351cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Inputs are not identical'</span>)); 21361cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError: Inputs are not identical</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 21371cb0ef41Sopenharmony_ci 21381cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 21391cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span> 21401cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 21411cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1 !== 2</span> 21421cb0ef41Sopenharmony_ci 21431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 21441cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 21451cb0ef41Sopenharmony_ci 21461cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello foobar'</span>, <span class="hljs-string">'Hello World!'</span>); 21471cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span> 21481cb0ef41Sopenharmony_ci<span class="hljs-comment">// + actual - expected</span> 21491cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 21501cb0ef41Sopenharmony_ci<span class="hljs-comment">// + 'Hello foobar'</span> 21511cb0ef41Sopenharmony_ci<span class="hljs-comment">// - 'Hello World!'</span> 21521cb0ef41Sopenharmony_ci<span class="hljs-comment">// ^</span> 21531cb0ef41Sopenharmony_ci 21541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>; 21551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>; 21561cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(apples, oranges, <span class="hljs-string">`apples <span class="hljs-subst">${apples}</span> !== oranges <span class="hljs-subst">${oranges}</span>`</span>); 21571cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span> 21581cb0ef41Sopenharmony_ci 21591cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Inputs are not identical'</span>)); 21601cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError: Inputs are not identical</span></code><button class="copy-button">copy</button></pre> 21611cb0ef41Sopenharmony_ci<p>If the values are not strictly equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a 21621cb0ef41Sopenharmony_ci<code>message</code> property set equal to the value of the <code>message</code> parameter. If the 21631cb0ef41Sopenharmony_ci<code>message</code> parameter is undefined, a default error message is assigned. If the 21641cb0ef41Sopenharmony_ci<code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown 21651cb0ef41Sopenharmony_ciinstead of the <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 21661cb0ef41Sopenharmony_ci</section><section><h3><code>assert.throws(fn[, error][, message])</code><span><a class="mark" href="#assertthrowsfn-error-message" id="assertthrowsfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_throws_fn_error_message"></a></h3> 21671cb0ef41Sopenharmony_ci<div class="api_metadata"> 21681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21691cb0ef41Sopenharmony_ci<table> 21701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21711cb0ef41Sopenharmony_ci<tr><td>v10.2.0</td> 21721cb0ef41Sopenharmony_ci<td><p>The <code>error</code> parameter can be an object containing regular expressions now.</p></td></tr> 21731cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td> 21741cb0ef41Sopenharmony_ci<td><p>The <code>error</code> parameter can now be an object as well.</p></td></tr> 21751cb0ef41Sopenharmony_ci<tr><td>v4.2.0</td> 21761cb0ef41Sopenharmony_ci<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr> 21771cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td> 21781cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr> 21791cb0ef41Sopenharmony_ci</tbody></table> 21801cb0ef41Sopenharmony_ci</details> 21811cb0ef41Sopenharmony_ci</div> 21821cb0ef41Sopenharmony_ci<ul> 21831cb0ef41Sopenharmony_ci<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 21841cb0ef41Sopenharmony_ci<li><code>error</code> <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/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 21851cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 21861cb0ef41Sopenharmony_ci</ul> 21871cb0ef41Sopenharmony_ci<p>Expects the function <code>fn</code> to throw an error.</p> 21881cb0ef41Sopenharmony_ci<p>If specified, <code>error</code> can be a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>, a validation function, 21891cb0ef41Sopenharmony_cia validation object where each property will be tested for strict deep equality, 21901cb0ef41Sopenharmony_cior an instance of error where each property will be tested for strict deep 21911cb0ef41Sopenharmony_ciequality including the non-enumerable <code>message</code> and <code>name</code> properties. When 21921cb0ef41Sopenharmony_ciusing an object, it is also possible to use a regular expression, when 21931cb0ef41Sopenharmony_civalidating against a string property. See below for examples.</p> 21941cb0ef41Sopenharmony_ci<p>If specified, <code>message</code> will be appended to the message provided by the 21951cb0ef41Sopenharmony_ci<code>AssertionError</code> if the <code>fn</code> call fails to throw or in case the error validation 21961cb0ef41Sopenharmony_cifails.</p> 21971cb0ef41Sopenharmony_ci<p>Custom validation object/error instance:</p> 21981cb0ef41Sopenharmony_ci 21991cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 22001cb0ef41Sopenharmony_ci 22011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 22021cb0ef41Sopenharmony_cierr.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>; 22031cb0ef41Sopenharmony_cierr.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>; 22041cb0ef41Sopenharmony_cierr.<span class="hljs-property">info</span> = { 22051cb0ef41Sopenharmony_ci <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 22061cb0ef41Sopenharmony_ci <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>, 22071cb0ef41Sopenharmony_ci}; 22081cb0ef41Sopenharmony_cierr.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>; 22091cb0ef41Sopenharmony_ci 22101cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 22111cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 22121cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> err; 22131cb0ef41Sopenharmony_ci }, 22141cb0ef41Sopenharmony_ci { 22151cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>, 22161cb0ef41Sopenharmony_ci <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>, 22171cb0ef41Sopenharmony_ci <span class="hljs-attr">info</span>: { 22181cb0ef41Sopenharmony_ci <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 22191cb0ef41Sopenharmony_ci <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>, 22201cb0ef41Sopenharmony_ci }, 22211cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only properties on the validation object will be tested for.</span> 22221cb0ef41Sopenharmony_ci <span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwise</span> 22231cb0ef41Sopenharmony_ci <span class="hljs-comment">// the validation is going to fail.</span> 22241cb0ef41Sopenharmony_ci }, 22251cb0ef41Sopenharmony_ci); 22261cb0ef41Sopenharmony_ci 22271cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using regular expressions to validate error properties:</span> 22281cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 22291cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 22301cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> err; 22311cb0ef41Sopenharmony_ci }, 22321cb0ef41Sopenharmony_ci { 22331cb0ef41Sopenharmony_ci <span class="hljs-comment">// The `name` and `message` properties are strings and using regular</span> 22341cb0ef41Sopenharmony_ci <span class="hljs-comment">// expressions on those will match against the string. If they fail, an</span> 22351cb0ef41Sopenharmony_ci <span class="hljs-comment">// error is thrown.</span> 22361cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>, 22371cb0ef41Sopenharmony_ci <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>, 22381cb0ef41Sopenharmony_ci <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, 22391cb0ef41Sopenharmony_ci <span class="hljs-attr">info</span>: { 22401cb0ef41Sopenharmony_ci <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 22411cb0ef41Sopenharmony_ci <span class="hljs-comment">// It is not possible to use regular expressions for nested properties!</span> 22421cb0ef41Sopenharmony_ci <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>, 22431cb0ef41Sopenharmony_ci }, 22441cb0ef41Sopenharmony_ci <span class="hljs-comment">// The `reg` property contains a regular expression and only if the</span> 22451cb0ef41Sopenharmony_ci <span class="hljs-comment">// validation object contains an identical regular expression, it is going</span> 22461cb0ef41Sopenharmony_ci <span class="hljs-comment">// to pass.</span> 22471cb0ef41Sopenharmony_ci <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>, 22481cb0ef41Sopenharmony_ci }, 22491cb0ef41Sopenharmony_ci); 22501cb0ef41Sopenharmony_ci 22511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span> 22521cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 22531cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 22541cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> otherErr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Not found'</span>); 22551cb0ef41Sopenharmony_ci <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span> 22561cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [key, value] <span class="hljs-keyword">of</span> <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">entries</span>(err)) { 22571cb0ef41Sopenharmony_ci otherErr[key] = value; 22581cb0ef41Sopenharmony_ci } 22591cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> otherErr; 22601cb0ef41Sopenharmony_ci }, 22611cb0ef41Sopenharmony_ci <span class="hljs-comment">// The error's `message` and `name` properties will also be checked when using</span> 22621cb0ef41Sopenharmony_ci <span class="hljs-comment">// an error as validation object.</span> 22631cb0ef41Sopenharmony_ci err, 22641cb0ef41Sopenharmony_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/strict'</span>); 22651cb0ef41Sopenharmony_ci 22661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">'Wrong value'</span>); 22671cb0ef41Sopenharmony_cierr.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>; 22681cb0ef41Sopenharmony_cierr.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>; 22691cb0ef41Sopenharmony_cierr.<span class="hljs-property">info</span> = { 22701cb0ef41Sopenharmony_ci <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 22711cb0ef41Sopenharmony_ci <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>, 22721cb0ef41Sopenharmony_ci}; 22731cb0ef41Sopenharmony_cierr.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>; 22741cb0ef41Sopenharmony_ci 22751cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 22761cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 22771cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> err; 22781cb0ef41Sopenharmony_ci }, 22791cb0ef41Sopenharmony_ci { 22801cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>, 22811cb0ef41Sopenharmony_ci <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>, 22821cb0ef41Sopenharmony_ci <span class="hljs-attr">info</span>: { 22831cb0ef41Sopenharmony_ci <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 22841cb0ef41Sopenharmony_ci <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>, 22851cb0ef41Sopenharmony_ci }, 22861cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only properties on the validation object will be tested for.</span> 22871cb0ef41Sopenharmony_ci <span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwise</span> 22881cb0ef41Sopenharmony_ci <span class="hljs-comment">// the validation is going to fail.</span> 22891cb0ef41Sopenharmony_ci }, 22901cb0ef41Sopenharmony_ci); 22911cb0ef41Sopenharmony_ci 22921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using regular expressions to validate error properties:</span> 22931cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 22941cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 22951cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> err; 22961cb0ef41Sopenharmony_ci }, 22971cb0ef41Sopenharmony_ci { 22981cb0ef41Sopenharmony_ci <span class="hljs-comment">// The `name` and `message` properties are strings and using regular</span> 22991cb0ef41Sopenharmony_ci <span class="hljs-comment">// expressions on those will match against the string. If they fail, an</span> 23001cb0ef41Sopenharmony_ci <span class="hljs-comment">// error is thrown.</span> 23011cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>, 23021cb0ef41Sopenharmony_ci <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>, 23031cb0ef41Sopenharmony_ci <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, 23041cb0ef41Sopenharmony_ci <span class="hljs-attr">info</span>: { 23051cb0ef41Sopenharmony_ci <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 23061cb0ef41Sopenharmony_ci <span class="hljs-comment">// It is not possible to use regular expressions for nested properties!</span> 23071cb0ef41Sopenharmony_ci <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>, 23081cb0ef41Sopenharmony_ci }, 23091cb0ef41Sopenharmony_ci <span class="hljs-comment">// The `reg` property contains a regular expression and only if the</span> 23101cb0ef41Sopenharmony_ci <span class="hljs-comment">// validation object contains an identical regular expression, it is going</span> 23111cb0ef41Sopenharmony_ci <span class="hljs-comment">// to pass.</span> 23121cb0ef41Sopenharmony_ci <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>, 23131cb0ef41Sopenharmony_ci }, 23141cb0ef41Sopenharmony_ci); 23151cb0ef41Sopenharmony_ci 23161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span> 23171cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23181cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23191cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> otherErr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Not found'</span>); 23201cb0ef41Sopenharmony_ci <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span> 23211cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [key, value] <span class="hljs-keyword">of</span> <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">entries</span>(err)) { 23221cb0ef41Sopenharmony_ci otherErr[key] = value; 23231cb0ef41Sopenharmony_ci } 23241cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> otherErr; 23251cb0ef41Sopenharmony_ci }, 23261cb0ef41Sopenharmony_ci <span class="hljs-comment">// The error's `message` and `name` properties will also be checked when using</span> 23271cb0ef41Sopenharmony_ci <span class="hljs-comment">// an error as validation object.</span> 23281cb0ef41Sopenharmony_ci err, 23291cb0ef41Sopenharmony_ci);</code><button class="copy-button">copy</button></pre> 23301cb0ef41Sopenharmony_ci<p>Validate instanceof using constructor:</p> 23311cb0ef41Sopenharmony_ci 23321cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 23331cb0ef41Sopenharmony_ci 23341cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23351cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23361cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>); 23371cb0ef41Sopenharmony_ci }, 23381cb0ef41Sopenharmony_ci <span class="hljs-title class_">Error</span>, 23391cb0ef41Sopenharmony_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/strict'</span>); 23401cb0ef41Sopenharmony_ci 23411cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23421cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23431cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>); 23441cb0ef41Sopenharmony_ci }, 23451cb0ef41Sopenharmony_ci <span class="hljs-title class_">Error</span>, 23461cb0ef41Sopenharmony_ci);</code><button class="copy-button">copy</button></pre> 23471cb0ef41Sopenharmony_ci<p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>:</p> 23481cb0ef41Sopenharmony_ci<p>Using a regular expression runs <code>.toString</code> on the error object, and will 23491cb0ef41Sopenharmony_citherefore also include the error name.</p> 23501cb0ef41Sopenharmony_ci 23511cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 23521cb0ef41Sopenharmony_ci 23531cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23541cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23551cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>); 23561cb0ef41Sopenharmony_ci }, 23571cb0ef41Sopenharmony_ci <span class="hljs-regexp">/^Error: Wrong value$/</span>, 23581cb0ef41Sopenharmony_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/strict'</span>); 23591cb0ef41Sopenharmony_ci 23601cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23611cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23621cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>); 23631cb0ef41Sopenharmony_ci }, 23641cb0ef41Sopenharmony_ci <span class="hljs-regexp">/^Error: Wrong value$/</span>, 23651cb0ef41Sopenharmony_ci);</code><button class="copy-button">copy</button></pre> 23661cb0ef41Sopenharmony_ci<p>Custom error validation:</p> 23671cb0ef41Sopenharmony_ci<p>The function must return <code>true</code> to indicate all internal validations passed. 23681cb0ef41Sopenharmony_ciIt will otherwise fail with an <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p> 23691cb0ef41Sopenharmony_ci 23701cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 23711cb0ef41Sopenharmony_ci 23721cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23731cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23741cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>); 23751cb0ef41Sopenharmony_ci }, 23761cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 23771cb0ef41Sopenharmony_ci <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>); 23781cb0ef41Sopenharmony_ci <span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span class="hljs-title function_">test</span>(err)); 23791cb0ef41Sopenharmony_ci <span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</span> 23801cb0ef41Sopenharmony_ci <span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instead,</span> 23811cb0ef41Sopenharmony_ci <span class="hljs-comment">// throw an error about the specific validation that failed (as done in this</span> 23821cb0ef41Sopenharmony_ci <span class="hljs-comment">// example) and add as much helpful debugging information to that error as</span> 23831cb0ef41Sopenharmony_ci <span class="hljs-comment">// possible.</span> 23841cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 23851cb0ef41Sopenharmony_ci }, 23861cb0ef41Sopenharmony_ci <span class="hljs-string">'unexpected error'</span>, 23871cb0ef41Sopenharmony_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/strict'</span>); 23881cb0ef41Sopenharmony_ci 23891cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>( 23901cb0ef41Sopenharmony_ci <span class="hljs-function">() =></span> { 23911cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Wrong value'</span>); 23921cb0ef41Sopenharmony_ci }, 23931cb0ef41Sopenharmony_ci <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 23941cb0ef41Sopenharmony_ci <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>); 23951cb0ef41Sopenharmony_ci <span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span class="hljs-title function_">test</span>(err)); 23961cb0ef41Sopenharmony_ci <span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</span> 23971cb0ef41Sopenharmony_ci <span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instead,</span> 23981cb0ef41Sopenharmony_ci <span class="hljs-comment">// throw an error about the specific validation that failed (as done in this</span> 23991cb0ef41Sopenharmony_ci <span class="hljs-comment">// example) and add as much helpful debugging information to that error as</span> 24001cb0ef41Sopenharmony_ci <span class="hljs-comment">// possible.</span> 24011cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 24021cb0ef41Sopenharmony_ci }, 24031cb0ef41Sopenharmony_ci <span class="hljs-string">'unexpected error'</span>, 24041cb0ef41Sopenharmony_ci);</code><button class="copy-button">copy</button></pre> 24051cb0ef41Sopenharmony_ci<p><code>error</code> cannot be a string. If a string is provided as the second 24061cb0ef41Sopenharmony_ciargument, then <code>error</code> is assumed to be omitted and the string will be used for 24071cb0ef41Sopenharmony_ci<code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same 24081cb0ef41Sopenharmony_cimessage as the thrown error message is going to result in an 24091cb0ef41Sopenharmony_ci<code>ERR_AMBIGUOUS_ARGUMENT</code> error. Please read the example below carefully if using 24101cb0ef41Sopenharmony_cia string as the second argument gets considered:</p> 24111cb0ef41Sopenharmony_ci 24121cb0ef41Sopenharmony_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> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>; 24131cb0ef41Sopenharmony_ci 24141cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingFirst</span>(<span class="hljs-params"></span>) { 24151cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'First'</span>); 24161cb0ef41Sopenharmony_ci} 24171cb0ef41Sopenharmony_ci 24181cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>(<span class="hljs-params"></span>) { 24191cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Second'</span>); 24201cb0ef41Sopenharmony_ci} 24211cb0ef41Sopenharmony_ci 24221cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<span class="hljs-params"></span>) {} 24231cb0ef41Sopenharmony_ci 24241cb0ef41Sopenharmony_ci<span class="hljs-comment">// The second argument is a string and the input function threw an Error.</span> 24251cb0ef41Sopenharmony_ci<span class="hljs-comment">// The first case will not throw as it does not match for the error message</span> 24261cb0ef41Sopenharmony_ci<span class="hljs-comment">// thrown by the input function!</span> 24271cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'Second'</span>); 24281cb0ef41Sopenharmony_ci<span class="hljs-comment">// In the next example the message has no benefit over the message from the</span> 24291cb0ef41Sopenharmony_ci<span class="hljs-comment">// error and since it is not clear if the user intended to actually match</span> 24301cb0ef41Sopenharmony_ci<span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.</span> 24311cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'Second'</span>); 24321cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span> 24331cb0ef41Sopenharmony_ci 24341cb0ef41Sopenharmony_ci<span class="hljs-comment">// The string is only used (as message) in case the function does not throw:</span> 24351cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Second'</span>); 24361cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</span> 24371cb0ef41Sopenharmony_ci 24381cb0ef41Sopenharmony_ci<span class="hljs-comment">// If it was intended to match for the error message do this instead:</span> 24391cb0ef41Sopenharmony_ci<span class="hljs-comment">// It does not throw because the error messages match.</span> 24401cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/Second$/</span>); 24411cb0ef41Sopenharmony_ci 24421cb0ef41Sopenharmony_ci<span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</span> 24431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/Second$/</span>); 24441cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>); 24451cb0ef41Sopenharmony_ci 24461cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingFirst</span>(<span class="hljs-params"></span>) { 24471cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'First'</span>); 24481cb0ef41Sopenharmony_ci} 24491cb0ef41Sopenharmony_ci 24501cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>(<span class="hljs-params"></span>) { 24511cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Second'</span>); 24521cb0ef41Sopenharmony_ci} 24531cb0ef41Sopenharmony_ci 24541cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<span class="hljs-params"></span>) {} 24551cb0ef41Sopenharmony_ci 24561cb0ef41Sopenharmony_ci<span class="hljs-comment">// The second argument is a string and the input function threw an Error.</span> 24571cb0ef41Sopenharmony_ci<span class="hljs-comment">// The first case will not throw as it does not match for the error message</span> 24581cb0ef41Sopenharmony_ci<span class="hljs-comment">// thrown by the input function!</span> 24591cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'Second'</span>); 24601cb0ef41Sopenharmony_ci<span class="hljs-comment">// In the next example the message has no benefit over the message from the</span> 24611cb0ef41Sopenharmony_ci<span class="hljs-comment">// error and since it is not clear if the user intended to actually match</span> 24621cb0ef41Sopenharmony_ci<span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.</span> 24631cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'Second'</span>); 24641cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span> 24651cb0ef41Sopenharmony_ci 24661cb0ef41Sopenharmony_ci<span class="hljs-comment">// The string is only used (as message) in case the function does not throw:</span> 24671cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Second'</span>); 24681cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</span> 24691cb0ef41Sopenharmony_ci 24701cb0ef41Sopenharmony_ci<span class="hljs-comment">// If it was intended to match for the error message do this instead:</span> 24711cb0ef41Sopenharmony_ci<span class="hljs-comment">// It does not throw because the error messages match.</span> 24721cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/Second$/</span>); 24731cb0ef41Sopenharmony_ci 24741cb0ef41Sopenharmony_ci<span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</span> 24751cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/Second$/</span>); 24761cb0ef41Sopenharmony_ci<span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code><button class="copy-button">copy</button></pre> 24771cb0ef41Sopenharmony_ci<p>Due to the confusing error-prone notation, avoid a string as the second 24781cb0ef41Sopenharmony_ciargument.</p></section> 24791cb0ef41Sopenharmony_ci <!-- API END --> 24801cb0ef41Sopenharmony_ci </div> 24811cb0ef41Sopenharmony_ci </div> 24821cb0ef41Sopenharmony_ci </div> 24831cb0ef41Sopenharmony_ci</body> 24841cb0ef41Sopenharmony_ci</html> 2485