1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.20.1"> 7 <title>Assert | Node.js v14.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/assert.html"> 12</head> 13<body class="alt apidoc" id="api-section-assert"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert active">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="assert" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.20.1 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <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"/> 97 <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"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <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"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="assert.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/assert.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/assert.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/assert.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/assert.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/assert.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/assert.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/assert.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/assert.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/assert.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/assert.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/assert.html">8.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v7.x/api/assert.html">7.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v6.x/api/assert.html">6.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v5.x/api/assert.html">5.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v4.x/api/assert.html">4.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/assert.html">0.12.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/assert.html">0.10.x</a></li></ol> 136 </li> 137 138 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/assert.md">Edit on GitHub</a></li> 139 </ul> 140 </div> 141 <hr> 142 </header> 143 144 <details id="toc" open><summary>Table of contents</summary><ul> 145<li><span class="stability_2"><a href="#assert_assert">Assert</a></span> 146<ul> 147<li><a href="#assert_strict_assertion_mode">Strict assertion mode</a></li> 148<li><a href="#assert_legacy_assertion_mode">Legacy assertion mode</a></li> 149<li><a href="#assert_class_assert_assertionerror">Class: assert.AssertionError</a> 150<ul> 151<li><a href="#assert_new_assert_assertionerror_options"><code>new assert.AssertionError(options)</code></a></li> 152</ul> 153</li> 154<li><span class="stability_1"><a href="#assert_class_assert_calltracker">Class: <code>assert.CallTracker</code></a></span> 155<ul> 156<li><a href="#assert_new_assert_calltracker"><code>new assert.CallTracker()</code></a></li> 157<li><a href="#assert_tracker_calls_fn_exact"><code>tracker.calls([fn][, exact])</code></a></li> 158<li><a href="#assert_tracker_report"><code>tracker.report()</code></a></li> 159<li><a href="#assert_tracker_verify"><code>tracker.verify()</code></a></li> 160</ul> 161</li> 162<li><a href="#assert_assert_value_message"><code>assert(value[, message])</code></a></li> 163<li><a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual(actual, expected[, message])</code></a> 164<ul> 165<li><a href="#assert_comparison_details">Comparison details</a></li> 166</ul> 167</li> 168<li><a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual(actual, expected[, message])</code></a> 169<ul> 170<li><a href="#assert_comparison_details_1">Comparison details</a></li> 171</ul> 172</li> 173<li><span class="stability_1"><a href="#assert_assert_doesnotmatch_string_regexp_message"><code>assert.doesNotMatch(string, regexp[, message])</code></a></span></li> 174<li><a href="#assert_assert_doesnotreject_asyncfn_error_message"><code>assert.doesNotReject(asyncFn[, error][, message])</code></a></li> 175<li><a href="#assert_assert_doesnotthrow_fn_error_message"><code>assert.doesNotThrow(fn[, error][, message])</code></a></li> 176<li><a href="#assert_assert_equal_actual_expected_message"><code>assert.equal(actual, expected[, message])</code></a></li> 177<li><a href="#assert_assert_fail_message"><code>assert.fail([message])</code></a></li> 178<li><span class="stability_0"><a href="#assert_assert_fail_actual_expected_message_operator_stackstartfn"><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code></a></span></li> 179<li><a href="#assert_assert_iferror_value"><code>assert.ifError(value)</code></a></li> 180<li><span class="stability_1"><a href="#assert_assert_match_string_regexp_message"><code>assert.match(string, regexp[, message])</code></a></span></li> 181<li><a href="#assert_assert_notdeepequal_actual_expected_message"><code>assert.notDeepEqual(actual, expected[, message])</code></a></li> 182<li><a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual(actual, expected[, message])</code></a></li> 183<li><a href="#assert_assert_notequal_actual_expected_message"><code>assert.notEqual(actual, expected[, message])</code></a></li> 184<li><a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual(actual, expected[, message])</code></a></li> 185<li><a href="#assert_assert_ok_value_message"><code>assert.ok(value[, message])</code></a></li> 186<li><a href="#assert_assert_rejects_asyncfn_error_message"><code>assert.rejects(asyncFn[, error][, message])</code></a></li> 187<li><a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual(actual, expected[, message])</code></a></li> 188<li><a href="#assert_assert_throws_fn_error_message"><code>assert.throws(fn[, error][, message])</code></a></li> 189</ul> 190</li> 191</ul></details> 192 193 <div id="apicontent"> 194 <h2>Assert<span><a class="mark" href="#assert_assert" id="assert_assert">#</a></span></h2> 195 196<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 197<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/assert.js">lib/assert.js</a></p> 198<p>The <code>assert</code> module provides a set of assertion functions for verifying 199invariants.</p> 200<section><h3>Strict assertion mode<span><a class="mark" href="#assert_strict_assertion_mode" id="assert_strict_assertion_mode">#</a></span></h3> 201<div class="api_metadata"> 202<details class="changelog"><summary>History</summary> 203<table> 204<tbody><tr><th>Version</th><th>Changes</th></tr> 205<tr><td>v13.9.0, v12.16.2</td> 206<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> 207<tr><td>v9.9.0</td> 208<td><p>Added error diffs to the strict assertion mode.</p></td></tr> 209<tr><td>v9.9.0</td> 210<td><p>Added strict assertion mode to the assert module.</p></td></tr> 211<tr><td>v9.9.0</td> 212<td><p><span>Added in: v9.9.0</span></p></td></tr> 213</tbody></table> 214</details> 215</div> 216<p>In strict assertion mode, non-strict methods behave like their corresponding 217strict methods. For example, <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> will behave like 218<a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p> 219<p>In strict assertion mode, error messages for objects display a diff. In legacy 220assertion mode, error messages for objects display the objects, often truncated.</p> 221<p>To use strict assertion mode:</p> 222<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>;</code></pre> 223<p>Example error diff:</p> 224<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 225 226assert.<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>]); 227<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 228<span class="hljs-comment">// + actual - expected ... Lines skipped</span> 229<span class="hljs-comment">//</span> 230<span class="hljs-comment">// [</span> 231<span class="hljs-comment">// [</span> 232<span class="hljs-comment">// ...</span> 233<span class="hljs-comment">// 2,</span> 234<span class="hljs-comment">// + 3</span> 235<span class="hljs-comment">// - '3'</span> 236<span class="hljs-comment">// ],</span> 237<span class="hljs-comment">// ...</span> 238<span class="hljs-comment">// 5</span> 239<span class="hljs-comment">// ]</span></code></pre> 240<p>To deactivate the colors, use the <code>NO_COLOR</code> or <code>NODE_DISABLE_COLORS</code> 241environment variables. This will also deactivate the colors in the REPL. For 242more on color support in terminal environments, read the tty 243<a href="tty.html#tty_writestream_getcolordepth_env">getColorDepth()</a> documentation.</p> 244</section><section><h3>Legacy assertion mode<span><a class="mark" href="#assert_legacy_assertion_mode" id="assert_legacy_assertion_mode">#</a></span></h3> 245<p>Legacy assertion mode uses the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> in:</p> 246<ul> 247<li><a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a></li> 248<li><a href="#assert_assert_equal_actual_expected_message"><code>assert.equal()</code></a></li> 249<li><a href="#assert_assert_notdeepequal_actual_expected_message"><code>assert.notDeepEqual()</code></a></li> 250<li><a href="#assert_assert_notequal_actual_expected_message"><code>assert.notEqual()</code></a></li> 251</ul> 252<p>To use legacy assertion mode:</p> 253<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);</code></pre> 254<p>Whenever possible, use the <a href="#assert_strict_assertion_mode">strict assertion mode</a> instead. Otherwise, the 255<a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> may cause surprising results. This is 256especially true for <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>, where the comparison rules are 257lax:</p> 258<pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span> 259assert.<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></pre> 260</section><section><h3>Class: assert.AssertionError<a class="srclink" href="https://github.com/nodejs/node/blob/c11b8215bd6388854efcc9302a7383c8c63b8127/lib/assert.js#L172">[src]</a><span><a class="mark" href="#assert_class_assert_assertionerror" id="assert_class_assert_assertionerror">#</a></span></h3> 261<ul> 262<li>Extends: <a href="errors.html#errors_class_error" class="type"><errors.Error></a></li> 263</ul> 264<p>Indicates the failure of an assertion. All errors thrown by the <code>assert</code> module 265will be instances of the <code>AssertionError</code> class.</p> 266<h4><code>new assert.AssertionError(options)</code><span><a class="mark" href="#assert_new_assert_assertionerror_options" id="assert_new_assert_assertionerror_options">#</a></span></h4> 267<div class="api_metadata"> 268<span>Added in: v0.1.21</span> 269</div> 270<ul> 271<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 272<ul> 273<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> 274<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> 275<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> 276<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> 277<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 278frames before this function.</li> 279</ul> 280</li> 281</ul> 282<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p> 283<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>) 284and:</p> 285<ul> 286<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 287<a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</li> 288<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 289<a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</li> 290<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 291(<code>true</code>) or not.</li> 292<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 293assertion error.</li> 294<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> 295</ul> 296<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 297 298<span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span> 299<span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">AssertionError</span>({ 300 <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>, 301 <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>, 302 <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span> 303}); 304 305<span class="hljs-comment">// Verify error output:</span> 306<span class="hljs-keyword">try</span> { 307 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 308} <span class="hljs-keyword">catch</span> (err) { 309 <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> assert.<span class="hljs-property">AssertionError</span>); 310 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, message); 311 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'AssertionError'</span>); 312 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actual</span>, <span class="hljs-number">1</span>); 313 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expected</span>, <span class="hljs-number">2</span>); 314 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code</span>, <span class="hljs-string">'ERR_ASSERTION'</span>); 315 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">operator</span>, <span class="hljs-string">'strictEqual'</span>); 316 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generatedMessage</span>, <span class="hljs-literal">true</span>); 317}</code></pre> 318</section><section><h3>Class: <code>assert.CallTracker</code><span><a class="mark" href="#assert_class_assert_calltracker" id="assert_class_assert_calltracker">#</a></span></h3> 319<div class="api_metadata"> 320<span>Added in: v14.2.0</span> 321</div> 322<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> 323<p>This feature is currently experimental and behavior might still change.</p> 324<h4><code>new assert.CallTracker()</code><span><a class="mark" href="#assert_new_assert_calltracker" id="assert_new_assert_calltracker">#</a></span></h4> 325<div class="api_metadata"> 326<span>Added in: v14.2.0</span> 327</div> 328<p>Creates a new <a href="#assert_class_assert_calltracker"><code>CallTracker</code></a> object which can be used to track if functions 329were called a specific number of times. The <code>tracker.verify()</code> must be called 330for the verification to take place. The usual pattern would be to call it in a 331<a href="process.html#process_event_exit"><code>process.on('exit')</code></a> handler.</p> 332<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 333 334<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 335 336<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 337 338<span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</span> 339<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">1</span>); 340 341<span class="hljs-title function_">callsfunc</span>(); 342 343<span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions have</span> 344<span class="hljs-comment">// been called exact times.</span> 345process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 346 tracker.<span class="hljs-title function_">verify</span>(); 347});</code></pre> 348<h4><code>tracker.calls([fn][, exact])</code><span><a class="mark" href="#assert_tracker_calls_fn_exact" id="assert_tracker_calls_fn_exact">#</a></span></h4> 349<div class="api_metadata"> 350<span>Added in: v14.2.0</span> 351</div> 352<ul> 353<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> 354<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> 355<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> 356</ul> 357<p>The wrapper function is expected to be called exactly <code>exact</code> times. If the 358function has not been called exactly <code>exact</code> times when 359<a href="#assert_tracker_verify"><code>tracker.verify()</code></a> is called, then <a href="#assert_tracker_verify"><code>tracker.verify()</code></a> will throw an 360error.</p> 361<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 362 363<span class="hljs-comment">// Creates call tracker.</span> 364<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 365 366<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 367 368<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 369<span class="hljs-comment">// before tracker.verify().</span> 370<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);</code></pre> 371<h4><code>tracker.report()</code><span><a class="mark" href="#assert_tracker_report" id="assert_tracker_report">#</a></span></h4> 372<div class="api_metadata"> 373<span>Added in: v14.2.0</span> 374</div> 375<ul> 376<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 377returned by <a href="#assert_tracker_calls_fn_exact"><code>tracker.calls()</code></a>.</li> 378<li>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 379<ul> 380<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 381<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> 382<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 383called.</li> 384<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> 385<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> 386</ul> 387</li> 388</ul> 389<p>The arrays contains information about the expected and actual number of calls of 390the functions that have not been called the expected number of times.</p> 391<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 392 393<span class="hljs-comment">// Creates call tracker.</span> 394<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 395 396<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 397 398<span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {} 399 400<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 401<span class="hljs-comment">// before tracker.verify().</span> 402<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>); 403 404<span class="hljs-comment">// Returns an array containing information on callsfunc()</span> 405tracker.<span class="hljs-title function_">report</span>(); 406<span class="hljs-comment">// [</span> 407<span class="hljs-comment">// {</span> 408<span class="hljs-comment">// message: 'Expected the func function to be executed 2 time(s) but was</span> 409<span class="hljs-comment">// executed 0 time(s).',</span> 410<span class="hljs-comment">// actual: 0,</span> 411<span class="hljs-comment">// expected: 2,</span> 412<span class="hljs-comment">// operator: 'func',</span> 413<span class="hljs-comment">// stack: stack trace</span> 414<span class="hljs-comment">// }</span> 415<span class="hljs-comment">// ]</span></code></pre> 416<h4><code>tracker.verify()</code><span><a class="mark" href="#assert_tracker_verify" id="assert_tracker_verify">#</a></span></h4> 417<div class="api_metadata"> 418<span>Added in: v14.2.0</span> 419</div> 420<p>Iterates through the list of functions passed to 421<a href="#assert_tracker_calls_fn_exact"><code>tracker.calls()</code></a> and will throw an error for functions that 422have not been called the expected number of times.</p> 423<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 424 425<span class="hljs-comment">// Creates call tracker.</span> 426<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>(); 427 428<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {} 429 430<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span> 431<span class="hljs-comment">// before tracker.verify().</span> 432<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>); 433 434<span class="hljs-title function_">callsfunc</span>(); 435 436<span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span> 437tracker.<span class="hljs-title function_">verify</span>();</code></pre> 438</section><section><h3><code>assert(value[, message])</code><span><a class="mark" href="#assert_assert_value_message" id="assert_assert_value_message">#</a></span></h3> 439<div class="api_metadata"> 440<span>Added in: v0.5.9</span> 441</div> 442<ul> 443<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> 444<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> 445</ul> 446<p>An alias of <a href="#assert_assert_ok_value_message"><code>assert.ok()</code></a>.</p> 447</section><section><h3><code>assert.deepEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_deepequal_actual_expected_message" id="assert_assert_deepequal_actual_expected_message">#</a></span></h3> 448<div class="api_metadata"> 449<details class="changelog"><summary>History</summary> 450<table> 451<tbody><tr><th>Version</th><th>Changes</th></tr> 452<tr><td>v14.18.0</td> 453<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 454<tr><td>v14.0.0</td> 455<td><p>NaN is now treated as being identical in case both sides are NaN.</p></td></tr> 456<tr><td>v12.0.0</td> 457<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> 458<tr><td>v9.0.0</td> 459<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 460<tr><td>v8.0.0</td> 461<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 462<tr><td>v6.4.0, v4.7.1</td> 463<td><p>Typed array slices are handled correctly now.</p></td></tr> 464<tr><td>v6.1.0, v4.5.0</td> 465<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 466<tr><td>v5.10.1, v4.4.3</td> 467<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 468<tr><td>v0.1.21</td> 469<td><p><span>Added in: v0.1.21</span></p></td></tr> 470</tbody></table> 471</details> 472</div> 473<ul> 474<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 475<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 476<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> 477</ul> 478<p><strong>Strict assertion mode</strong></p> 479<p>An alias of <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p> 480<p><strong>Legacy assertion mode</strong></p> 481<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> instead.</div><p></p> 482<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. Consider 483using <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> instead. <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> can have 484surprising results.</p> 485<p><em>Deep equality</em> means that the enumerable "own" properties of child objects 486are also recursively evaluated by the following rules.</p> 487<h4>Comparison details<span><a class="mark" href="#assert_comparison_details" id="assert_comparison_details">#</a></span></h4> 488<ul> 489<li>Primitive values are compared with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> 490( <code>==</code> ) with the exception of <code>NaN</code>. It is treated as being identical in case 491both sides are <code>NaN</code>.</li> 492<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li> 493<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> 494<li><a href="errors.html#errors_class_error"><code>Error</code></a> names and messages are always compared, even if these are not 495enumerable properties.</li> 496<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> 497<li><code>Object</code> properties are compared unordered.</li> 498<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> 499<li>Recursion stops when both sides differ or both sides encounter a circular 500reference.</li> 501<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 502objects.</li> 503<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> 504<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> 505</ul> 506<p>The following example does not throw an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> because the 507primitives are considered equal by the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> 508( <code>==</code> ).</p> 509<pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span> 510assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</span>, <span class="hljs-literal">false</span>);</code></pre> 511<p>"Deep" equality means that the enumerable "own" properties of child objects 512are evaluated also:</p> 513<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 514 515<span class="hljs-keyword">const</span> obj1 = { 516 <span class="hljs-attr">a</span>: { 517 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> 518 } 519}; 520<span class="hljs-keyword">const</span> obj2 = { 521 <span class="hljs-attr">a</span>: { 522 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> 523 } 524}; 525<span class="hljs-keyword">const</span> obj3 = { 526 <span class="hljs-attr">a</span>: { 527 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> 528 } 529}; 530<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1); 531 532assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1); 533<span class="hljs-comment">// OK</span> 534 535<span class="hljs-comment">// Values of b are different:</span> 536assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2); 537<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span> 538 539assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3); 540<span class="hljs-comment">// OK</span> 541 542<span class="hljs-comment">// Prototypes are ignored:</span> 543assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4); 544<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code></pre> 545<p>If the values are not equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 546property set equal to the value of the <code>message</code> parameter. If the <code>message</code> 547parameter is undefined, a default error message is assigned. If the <code>message</code> 548parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 549<a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 550</section><section><h3><code>assert.deepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_deepstrictequal_actual_expected_message" id="assert_assert_deepstrictequal_actual_expected_message">#</a></span></h3> 551<div class="api_metadata"> 552<details class="changelog"><summary>History</summary> 553<table> 554<tbody><tr><th>Version</th><th>Changes</th></tr> 555<tr><td>v9.0.0</td> 556<td><p>Enumerable symbol properties are now compared.</p></td></tr> 557<tr><td>v9.0.0</td> 558<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> 559<tr><td>v8.5.0</td> 560<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 561<tr><td>v8.0.0</td> 562<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 563<tr><td>v6.1.0</td> 564<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 565<tr><td>v6.4.0, v4.7.1</td> 566<td><p>Typed array slices are handled correctly now.</p></td></tr> 567<tr><td>v5.10.1, v4.4.3</td> 568<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 569<tr><td>v1.2.0</td> 570<td><p><span>Added in: v1.2.0</span></p></td></tr> 571</tbody></table> 572</details> 573</div> 574<ul> 575<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 576<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 577<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> 578</ul> 579<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. 580"Deep" equality means that the enumerable "own" properties of child objects 581are recursively evaluated also by the following rules.</p> 582<h4>Comparison details<span><a class="mark" href="#assert_comparison_details_1" id="assert_comparison_details_1">#</a></span></h4> 583<ul> 584<li>Primitive values are compared using the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>, used by 585<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</li> 586<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li> 587<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 588the <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Strict Equality Comparison</a>.</li> 589<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> 590<li><a href="errors.html#errors_class_error"><code>Error</code></a> names and messages are always compared, even if these are not 591enumerable properties.</li> 592<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> 593<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> 594<li><code>Object</code> properties are compared unordered.</li> 595<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> 596<li>Recursion stops when both sides differ or both sides encounter a circular 597reference.</li> 598<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 599below for further details.</li> 600</ul> 601<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 602 603<span class="hljs-comment">// This fails because 1 !== '1'.</span> 604assert.<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> }); 605<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 606<span class="hljs-comment">// + actual - expected</span> 607<span class="hljs-comment">//</span> 608<span class="hljs-comment">// {</span> 609<span class="hljs-comment">// + a: 1</span> 610<span class="hljs-comment">// - a: '1'</span> 611<span class="hljs-comment">// }</span> 612 613<span class="hljs-comment">// The following objects don't have own properties</span> 614<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(); 615<span class="hljs-keyword">const</span> object = {}; 616<span class="hljs-keyword">const</span> fakeDate = {}; 617<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>); 618 619<span class="hljs-comment">// Different [[Prototype]]:</span> 620assert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate); 621<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 622<span class="hljs-comment">// + actual - expected</span> 623<span class="hljs-comment">//</span> 624<span class="hljs-comment">// + {}</span> 625<span class="hljs-comment">// - Date {}</span> 626 627<span class="hljs-comment">// Different type tags:</span> 628assert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate); 629<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 630<span class="hljs-comment">// + actual - expected</span> 631<span class="hljs-comment">//</span> 632<span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span> 633<span class="hljs-comment">// - Date {}</span> 634 635assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">Na</span>N, <span class="hljs-title class_">Na</span>N); 636<span class="hljs-comment">// OK, because of the SameValue comparison</span> 637 638<span class="hljs-comment">// Different unwrapped numbers:</span> 639assert.<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>)); 640<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 641<span class="hljs-comment">// + actual - expected</span> 642<span class="hljs-comment">//</span> 643<span class="hljs-comment">// + [Number: 1]</span> 644<span class="hljs-comment">// - [Number: 2]</span> 645 646assert.<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>)); 647<span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</span> 648 649assert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>); 650<span class="hljs-comment">// OK</span> 651 652<span class="hljs-comment">// Different zeros using the SameValue Comparison:</span> 653assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>); 654<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 655<span class="hljs-comment">// + actual - expected</span> 656<span class="hljs-comment">//</span> 657<span class="hljs-comment">// + 0</span> 658<span class="hljs-comment">// - -0</span> 659 660<span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>(); 661<span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>(); 662assert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol1]: <span class="hljs-number">1</span> }); 663<span class="hljs-comment">// OK, because it is the same symbol on both objects.</span> 664 665assert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol2]: <span class="hljs-number">1</span> }); 666<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:</span> 667<span class="hljs-comment">//</span> 668<span class="hljs-comment">// {</span> 669<span class="hljs-comment">// [Symbol()]: 1</span> 670<span class="hljs-comment">// }</span> 671 672<span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>(); 673<span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>([[{}, {}]]); 674<span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>(); 675weakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>; 676 677assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2); 678<span class="hljs-comment">// OK, because it is impossible to compare the entries</span> 679 680<span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:</span> 681assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3); 682<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span> 683<span class="hljs-comment">// + actual - expected</span> 684<span class="hljs-comment">//</span> 685<span class="hljs-comment">// WeakMap {</span> 686<span class="hljs-comment">// + [items unknown]</span> 687<span class="hljs-comment">// - [items unknown],</span> 688<span class="hljs-comment">// - unequal: true</span> 689<span class="hljs-comment">// }</span></code></pre> 690<p>If the values are not equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 691property set equal to the value of the <code>message</code> parameter. If the <code>message</code> 692parameter is undefined, a default error message is assigned. If the <code>message</code> 693parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 694<code>AssertionError</code>.</p> 695</section><section><h3><code>assert.doesNotMatch(string, regexp[, message])</code><span><a class="mark" href="#assert_assert_doesnotmatch_string_regexp_message" id="assert_assert_doesnotmatch_string_regexp_message">#</a></span></h3> 696<div class="api_metadata"> 697<span>Added in: v13.6.0, v12.16.0</span> 698</div> 699<ul> 700<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 701<li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a></li> 702<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> 703</ul> 704<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> 705<p>Expects the <code>string</code> input not to match the regular expression.</p> 706<p>This feature is currently experimental and the name might change or it might be 707completely removed again.</p> 708<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 709 710assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/fail/</span>); 711<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...</span> 712 713assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>); 714<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span> 715 716assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/different/</span>); 717<span class="hljs-comment">// OK</span></code></pre> 718<p>If the values do match, or if the <code>string</code> argument is of another type than 719<code>string</code>, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal 720to the value of the <code>message</code> parameter. If the <code>message</code> parameter is 721undefined, a default error message is assigned. If the <code>message</code> parameter is an 722instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 723<a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 724</section><section><h3><code>assert.doesNotReject(asyncFn[, error][, message])</code><span><a class="mark" href="#assert_assert_doesnotreject_asyncfn_error_message" id="assert_assert_doesnotreject_asyncfn_error_message">#</a></span></h3> 725<div class="api_metadata"> 726<span>Added in: v10.0.0</span> 727</div> 728<ul> 729<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> 730<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> 731<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 732</ul> 733<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately 734calls the function and awaits the returned promise to complete. It will then 735check that the promise is not rejected.</p> 736<p>If <code>asyncFn</code> is a function and it throws an error synchronously, 737<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If 738the function does not return a promise, <code>assert.doesNotReject()</code> will return a 739rejected <code>Promise</code> with an <a href="errors.html#errors_err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases 740the error handler is skipped.</p> 741<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little 742benefit in catching a rejection and then rejecting it again. Instead, consider 743adding a comment next to the specific code path that should not reject and keep 744error messages as expressive as possible.</p> 745<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 746function. See <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> for more details.</p> 747<p>Besides the async nature to await the completion behaves identically to 748<a href="#assert_assert_doesnotthrow_fn_error_message"><code>assert.doesNotThrow()</code></a>.</p> 749<!-- eslint-disable no-restricted-syntax --> 750<pre><code class="language-js">(<span class="hljs-keyword">async</span> () => { 751 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</span>( 752 <span class="hljs-keyword">async</span> () => { 753 <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>); 754 }, 755 <span class="hljs-title class_">SyntaxError</span> 756 ); 757})();</code></pre> 758<!-- eslint-disable no-restricted-syntax --> 759<pre><code class="language-js">assert.<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>))) 760 .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 761 <span class="hljs-comment">// ...</span> 762 });</code></pre> 763</section><section><h3><code>assert.doesNotThrow(fn[, error][, message])</code><span><a class="mark" href="#assert_assert_doesnotthrow_fn_error_message" id="assert_assert_doesnotthrow_fn_error_message">#</a></span></h3> 764<div class="api_metadata"> 765<details class="changelog"><summary>History</summary> 766<table> 767<tbody><tr><th>Version</th><th>Changes</th></tr> 768<tr><td>v5.11.0, v4.4.5</td> 769<td><p>The <code>message</code> parameter is respected now.</p></td></tr> 770<tr><td>v4.2.0</td> 771<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr> 772<tr><td>v0.1.21</td> 773<td><p><span>Added in: v0.1.21</span></p></td></tr> 774</tbody></table> 775</details> 776</div> 777<ul> 778<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 779<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> 780<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 781</ul> 782<p>Asserts that the function <code>fn</code> does not throw an error.</p> 783<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there 784is no benefit in catching an error and then rethrowing it. Instead, consider 785adding a comment next to the specific code path that should not throw and keep 786error messages as expressive as possible.</p> 787<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code> 788function.</p> 789<p>If an error is thrown and it is the same type as that specified by the <code>error</code> 790parameter, then an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown. If the error is of a 791different type, or if the <code>error</code> parameter is undefined, the error is 792propagated back to the caller.</p> 793<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 794function. See <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> for more details.</p> 795<p>The following, for instance, will throw the <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> because there is no 796matching error type in the assertion:</p> 797<!-- eslint-disable no-restricted-syntax --> 798<pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>( 799 <span class="hljs-function">() =></span> { 800 <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>); 801 }, 802 <span class="hljs-title class_">SyntaxError</span> 803);</code></pre> 804<p>However, the following will result in an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> with the message 805'Got unwanted exception...':</p> 806<!-- eslint-disable no-restricted-syntax --> 807<pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>( 808 <span class="hljs-function">() =></span> { 809 <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>); 810 }, 811 <span class="hljs-title class_">TypeError</span> 812);</code></pre> 813<p>If an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown and a value is provided for the <code>message</code> 814parameter, the value of <code>message</code> will be appended to the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> 815message:</p> 816<!-- eslint-disable no-restricted-syntax --> 817<pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>( 818 <span class="hljs-function">() =></span> { 819 <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>); 820 }, 821 <span class="hljs-regexp">/Wrong value/</span>, 822 <span class="hljs-string">'Whoops'</span> 823); 824<span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code></pre> 825</section><section><h3><code>assert.equal(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_equal_actual_expected_message" id="assert_assert_equal_actual_expected_message">#</a></span></h3> 826<div class="api_metadata"> 827<details class="changelog"><summary>History</summary> 828<table> 829<tbody><tr><th>Version</th><th>Changes</th></tr> 830<tr><td>v14.18.0</td> 831<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 832<tr><td>v14.0.0</td> 833<td><p>NaN is now treated as being identical in case both sides are NaN.</p></td></tr> 834<tr><td>v0.1.21</td> 835<td><p><span>Added in: v0.1.21</span></p></td></tr> 836</tbody></table> 837</details> 838</div> 839<ul> 840<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 841<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 842<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> 843</ul> 844<p><strong>Strict assertion mode</strong></p> 845<p>An alias of <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</p> 846<p><strong>Legacy assertion mode</strong></p> 847<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> instead.</div><p></p> 848<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters 849using the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> ( <code>==</code> ). <code>NaN</code> is special handled 850and treated as being identical in case both sides are <code>NaN</code>.</p> 851<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 852 853assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 854<span class="hljs-comment">// OK, 1 == 1</span> 855assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 856<span class="hljs-comment">// OK, 1 == '1'</span> 857assert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">Na</span>N, <span class="hljs-title class_">Na</span>N); 858<span class="hljs-comment">// OK</span> 859 860assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 861<span class="hljs-comment">// AssertionError: 1 == 2</span> 862assert.<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> } }); 863<span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code></pre> 864<p>If the values are not equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 865property set equal to the value of the <code>message</code> parameter. If the <code>message</code> 866parameter is undefined, a default error message is assigned. If the <code>message</code> 867parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 868<code>AssertionError</code>.</p> 869</section><section><h3><code>assert.fail([message])</code><span><a class="mark" href="#assert_assert_fail_message" id="assert_assert_fail_message">#</a></span></h3> 870<div class="api_metadata"> 871<span>Added in: v0.1.21</span> 872</div> 873<ul> 874<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> 875</ul> 876<p>Throws an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> with the provided error message or a default 877error message. If the <code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then 878it will be thrown instead of the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 879<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 880 881assert.<span class="hljs-title function_">fail</span>(); 882<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span> 883 884assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>); 885<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span> 886 887assert.<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>)); 888<span class="hljs-comment">// TypeError: need array</span></code></pre> 889<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated. 890See below for further details.</p> 891</section><section><h3><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code><span><a class="mark" href="#assert_assert_fail_actual_expected_message_operator_stackstartfn" id="assert_assert_fail_actual_expected_message_operator_stackstartfn">#</a></span></h3> 892<div class="api_metadata"> 893<details class="changelog"><summary>History</summary> 894<table> 895<tbody><tr><th>Version</th><th>Changes</th></tr> 896<tr><td>v10.0.0</td> 897<td><p>Calling <code>assert.fail()</code> with more than one argument is deprecated and emits a warning.</p></td></tr> 898<tr><td>v0.1.21</td> 899<td><p><span>Added in: v0.1.21</span></p></td></tr> 900</tbody></table> 901</details> 902</div> 903<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>assert.fail([message])</code> or other assert 904functions instead.</div><p></p> 905<ul> 906<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 907<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 908<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> 909<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> 910<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> 911</ul> 912<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and 913<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and 914<code>expected</code> arguments are provided, <code>operator</code> will default to <code>'!='</code>. If 915<code>message</code> is provided as third argument it will be used as the error message and 916the other arguments will be stored as properties on the thrown object. If 917<code>stackStartFn</code> is provided, all stack frames above that function will be 918removed from stacktrace (see <a href="errors.html#errors_error_capturestacktrace_targetobject_constructoropt"><code>Error.captureStackTrace</code></a>). If no arguments are 919given, the default message <code>Failed</code> will be used.</p> 920<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 921 922assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>); 923<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span> 924 925assert.<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>); 926<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span> 927 928assert.<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>); 929<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span> 930 931assert.<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>); 932<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span> 933 934assert.<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>)); 935<span class="hljs-comment">// TypeError: need array</span></code></pre> 936<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no 937influence on the error message.</p> 938<p>Example use of <code>stackStartFn</code> for truncating the exception's stacktrace:</p> 939<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">suppressFrame</span>(<span class="hljs-params"></span>) { 940 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); 941} 942<span class="hljs-title function_">suppressFrame</span>(); 943<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span> 944<span class="hljs-comment">// at repl:1:1</span> 945<span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span> 946<span class="hljs-comment">// ...</span></code></pre> 947</section><section><h3><code>assert.ifError(value)</code><span><a class="mark" href="#assert_assert_iferror_value" id="assert_assert_iferror_value">#</a></span></h3> 948<div class="api_metadata"> 949<details class="changelog"><summary>History</summary> 950<table> 951<tbody><tr><th>Version</th><th>Changes</th></tr> 952<tr><td>v10.0.0</td> 953<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> 954<tr><td>v10.0.0</td> 955<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> 956<tr><td>v0.1.97</td> 957<td><p><span>Added in: v0.1.97</span></p></td></tr> 958</tbody></table> 959</details> 960</div> 961<ul> 962<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 963</ul> 964<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when 965testing the <code>error</code> argument in callbacks. The stack trace contains all frames 966from the error passed to <code>ifError()</code> including the potential new frames for 967<code>ifError()</code> itself.</p> 968<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 969 970assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>); 971<span class="hljs-comment">// OK</span> 972assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>); 973<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</span> 974assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>); 975<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'</span> 976assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>()); 977<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error</span> 978 979<span class="hljs-comment">// Create some random error frames.</span> 980<span class="hljs-keyword">let</span> err; 981(<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<span class="hljs-params"></span>) { 982 err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'test error'</span>); 983})(); 984 985(<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(<span class="hljs-params"></span>) { 986 assert.<span class="hljs-title function_">ifError</span>(err); 987})(); 988<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error</span> 989<span class="hljs-comment">// at ifErrorFrame</span> 990<span class="hljs-comment">// at errorFrame</span></code></pre> 991</section><section><h3><code>assert.match(string, regexp[, message])</code><span><a class="mark" href="#assert_assert_match_string_regexp_message" id="assert_assert_match_string_regexp_message">#</a></span></h3> 992<div class="api_metadata"> 993<span>Added in: v13.6.0, v12.16.0</span> 994</div> 995<ul> 996<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 997<li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a></li> 998<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> 999</ul> 1000<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> 1001<p>Expects the <code>string</code> input to match the regular expression.</p> 1002<p>This feature is currently experimental and the name might change or it might be 1003completely removed again.</p> 1004<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 1005 1006assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/pass/</span>); 1007<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular ...</span> 1008 1009assert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>); 1010<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span> 1011 1012assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/pass/</span>); 1013<span class="hljs-comment">// OK</span></code></pre> 1014<p>If the values do not match, or if the <code>string</code> argument is of another type than 1015<code>string</code>, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal 1016to the value of the <code>message</code> parameter. If the <code>message</code> parameter is 1017undefined, a default error message is assigned. If the <code>message</code> parameter is an 1018instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 1019<a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 1020</section><section><h3><code>assert.notDeepEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notdeepequal_actual_expected_message" id="assert_assert_notdeepequal_actual_expected_message">#</a></span></h3> 1021<div class="api_metadata"> 1022<details class="changelog"><summary>History</summary> 1023<table> 1024<tbody><tr><th>Version</th><th>Changes</th></tr> 1025<tr><td>v14.18.0</td> 1026<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 1027<tr><td>v14.0.0</td> 1028<td><p>NaN is now treated as being identical in case both sides are NaN.</p></td></tr> 1029<tr><td>v9.0.0</td> 1030<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 1031<tr><td>v8.0.0</td> 1032<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 1033<tr><td>v6.4.0, v4.7.1</td> 1034<td><p>Typed array slices are handled correctly now.</p></td></tr> 1035<tr><td>v6.1.0, v4.5.0</td> 1036<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 1037<tr><td>v5.10.1, v4.4.3</td> 1038<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 1039<tr><td>v0.1.21</td> 1040<td><p><span>Added in: v0.1.21</span></p></td></tr> 1041</tbody></table> 1042</details> 1043</div> 1044<ul> 1045<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1046<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1047<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> 1048</ul> 1049<p><strong>Strict assertion mode</strong></p> 1050<p>An alias of <a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual()</code></a>.</p> 1051<p><strong>Legacy assertion mode</strong></p> 1052<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual()</code></a> instead.</div><p></p> 1053<p>Tests for any deep inequality. Opposite of <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>.</p> 1054<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 1055 1056<span class="hljs-keyword">const</span> obj1 = { 1057 <span class="hljs-attr">a</span>: { 1058 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> 1059 } 1060}; 1061<span class="hljs-keyword">const</span> obj2 = { 1062 <span class="hljs-attr">a</span>: { 1063 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> 1064 } 1065}; 1066<span class="hljs-keyword">const</span> obj3 = { 1067 <span class="hljs-attr">a</span>: { 1068 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> 1069 } 1070}; 1071<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1); 1072 1073assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1); 1074<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span> 1075 1076assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2); 1077<span class="hljs-comment">// OK</span> 1078 1079assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3); 1080<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span> 1081 1082assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4); 1083<span class="hljs-comment">// OK</span></code></pre> 1084<p>If the values are deeply equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a 1085<code>message</code> property set equal to the value of the <code>message</code> parameter. If the 1086<code>message</code> parameter is undefined, a default error message is assigned. If the 1087<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown 1088instead of the <code>AssertionError</code>.</p> 1089</section><section><h3><code>assert.notDeepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notdeepstrictequal_actual_expected_message" id="assert_assert_notdeepstrictequal_actual_expected_message">#</a></span></h3> 1090<div class="api_metadata"> 1091<details class="changelog"><summary>History</summary> 1092<table> 1093<tbody><tr><th>Version</th><th>Changes</th></tr> 1094<tr><td>v9.0.0</td> 1095<td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr> 1096<tr><td>v9.0.0</td> 1097<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> 1098<tr><td>v9.0.0</td> 1099<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr> 1100<tr><td>v8.0.0</td> 1101<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr> 1102<tr><td>v6.1.0</td> 1103<td><p>Objects with circular references can be used as inputs now.</p></td></tr> 1104<tr><td>v6.4.0, v4.7.1</td> 1105<td><p>Typed array slices are handled correctly now.</p></td></tr> 1106<tr><td>v5.10.1, v4.4.3</td> 1107<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr> 1108<tr><td>v1.2.0</td> 1109<td><p><span>Added in: v1.2.0</span></p></td></tr> 1110</tbody></table> 1111</details> 1112</div> 1113<ul> 1114<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1115<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1116<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> 1117</ul> 1118<p>Tests for deep strict inequality. Opposite of <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p> 1119<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 1120 1121assert.<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> }); 1122<span class="hljs-comment">// OK</span></code></pre> 1123<p>If the values are deeply and strictly equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown 1124with a <code>message</code> property set equal to the value of the <code>message</code> parameter. If 1125the <code>message</code> parameter is undefined, a default error message is assigned. If 1126the <code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown 1127instead of the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 1128</section><section><h3><code>assert.notEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notequal_actual_expected_message" id="assert_assert_notequal_actual_expected_message">#</a></span></h3> 1129<div class="api_metadata"> 1130<details class="changelog"><summary>History</summary> 1131<table> 1132<tbody><tr><th>Version</th><th>Changes</th></tr> 1133<tr><td>v14.18.0</td> 1134<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr> 1135<tr><td>v14.0.0</td> 1136<td><p>NaN is now treated as being identical in case both sides are NaN.</p></td></tr> 1137<tr><td>v0.1.21</td> 1138<td><p><span>Added in: v0.1.21</span></p></td></tr> 1139</tbody></table> 1140</details> 1141</div> 1142<ul> 1143<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1144<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1145<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> 1146</ul> 1147<p><strong>Strict assertion mode</strong></p> 1148<p>An alias of <a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual()</code></a>.</p> 1149<p><strong>Legacy assertion mode</strong></p> 1150<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual()</code></a> instead.</div><p></p> 1151<p>Tests shallow, coercive inequality with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> 1152(<code>!=</code> ). <code>NaN</code> is special handled and treated as being identical in case both 1153sides are <code>NaN</code>.</p> 1154<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 1155 1156assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 1157<span class="hljs-comment">// OK</span> 1158 1159assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 1160<span class="hljs-comment">// AssertionError: 1 != 1</span> 1161 1162assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 1163<span class="hljs-comment">// AssertionError: 1 != '1'</span></code></pre> 1164<p>If the values are equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 1165property set equal to the value of the <code>message</code> parameter. If the <code>message</code> 1166parameter is undefined, a default error message is assigned. If the <code>message</code> 1167parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 1168<code>AssertionError</code>.</p> 1169</section><section><h3><code>assert.notStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notstrictequal_actual_expected_message" id="assert_assert_notstrictequal_actual_expected_message">#</a></span></h3> 1170<div class="api_metadata"> 1171<details class="changelog"><summary>History</summary> 1172<table> 1173<tbody><tr><th>Version</th><th>Changes</th></tr> 1174<tr><td>v10.0.0</td> 1175<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code>.</p></td></tr> 1176<tr><td>v0.1.21</td> 1177<td><p><span>Added in: v0.1.21</span></p></td></tr> 1178</tbody></table> 1179</details> 1180</div> 1181<ul> 1182<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1183<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1184<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> 1185</ul> 1186<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as 1187determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.</p> 1188<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 1189 1190assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 1191<span class="hljs-comment">// OK</span> 1192 1193assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 1194<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:</span> 1195<span class="hljs-comment">//</span> 1196<span class="hljs-comment">// 1</span> 1197 1198assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>); 1199<span class="hljs-comment">// OK</span></code></pre> 1200<p>If the values are strictly equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a 1201<code>message</code> property set equal to the value of the <code>message</code> parameter. If the 1202<code>message</code> parameter is undefined, a default error message is assigned. If the 1203<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown 1204instead of the <code>AssertionError</code>.</p> 1205</section><section><h3><code>assert.ok(value[, message])</code><span><a class="mark" href="#assert_assert_ok_value_message" id="assert_assert_ok_value_message">#</a></span></h3> 1206<div class="api_metadata"> 1207<details class="changelog"><summary>History</summary> 1208<table> 1209<tbody><tr><th>Version</th><th>Changes</th></tr> 1210<tr><td>v10.0.0</td> 1211<td><p>The <code>assert.ok()</code> (no arguments) will now use a predefined error message.</p></td></tr> 1212<tr><td>v0.1.21</td> 1213<td><p><span>Added in: v0.1.21</span></p></td></tr> 1214</tbody></table> 1215</details> 1216</div> 1217<ul> 1218<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1219<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> 1220</ul> 1221<p>Tests if <code>value</code> is truthy. It is equivalent to 1222<code>assert.equal(!!value, true, message)</code>.</p> 1223<p>If <code>value</code> is not truthy, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> 1224property set equal to the value of the <code>message</code> parameter. If the <code>message</code> 1225parameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code> 1226parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the 1227<code>AssertionError</code>. 1228If no arguments are passed in at all <code>message</code> will be set to the string: 1229<code>'No value argument passed to `assert.ok()`'</code>.</p> 1230<p>Be aware that in the <code>repl</code> the error message will be different to the one 1231thrown in a file! See below for further details.</p> 1232<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 1233 1234assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>); 1235<span class="hljs-comment">// OK</span> 1236assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>); 1237<span class="hljs-comment">// OK</span> 1238 1239assert.<span class="hljs-title function_">ok</span>(); 1240<span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span> 1241 1242assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span class="hljs-string">'it\'s false'</span>); 1243<span class="hljs-comment">// AssertionError: it's false</span> 1244 1245<span class="hljs-comment">// In the repl:</span> 1246assert.<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>); 1247<span class="hljs-comment">// AssertionError: false == true</span> 1248 1249<span class="hljs-comment">// In a file (e.g. test.js):</span> 1250assert.<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>); 1251<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 1252<span class="hljs-comment">//</span> 1253<span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span> 1254 1255assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>); 1256<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 1257<span class="hljs-comment">//</span> 1258<span class="hljs-comment">// assert.ok(false)</span> 1259 1260assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>); 1261<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 1262<span class="hljs-comment">//</span> 1263<span class="hljs-comment">// assert.ok(0)</span> 1264 1265<span class="hljs-comment">// Using `assert()` works the same:</span> 1266<span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>); 1267<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span> 1268<span class="hljs-comment">//</span> 1269<span class="hljs-comment">// assert(0)</span></code></pre> 1270</section><section><h3><code>assert.rejects(asyncFn[, error][, message])</code><span><a class="mark" href="#assert_assert_rejects_asyncfn_error_message" id="assert_assert_rejects_asyncfn_error_message">#</a></span></h3> 1271<div class="api_metadata"> 1272<span>Added in: v10.0.0</span> 1273</div> 1274<ul> 1275<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> 1276<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> 1277<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1278</ul> 1279<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately 1280calls the function and awaits the returned promise to complete. It will then 1281check that the promise is rejected.</p> 1282<p>If <code>asyncFn</code> is a function and it throws an error synchronously, 1283<code>assert.rejects()</code> will return a rejected <code>Promise</code> with that error. If the 1284function does not return a promise, <code>assert.rejects()</code> will return a rejected 1285<code>Promise</code> with an <a href="errors.html#errors_err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases the error 1286handler is skipped.</p> 1287<p>Besides the async nature to await the completion behaves identically to 1288<a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a>.</p> 1289<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, 1290an object where each property will be tested for, or an instance of error where 1291each property will be tested for including the non-enumerable <code>message</code> and 1292<code>name</code> properties.</p> 1293<p>If specified, <code>message</code> will be the message provided by the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> 1294if the <code>asyncFn</code> fails to reject.</p> 1295<pre><code class="language-js">(<span class="hljs-keyword">async</span> () => { 1296 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>( 1297 <span class="hljs-keyword">async</span> () => { 1298 <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>); 1299 }, 1300 { 1301 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>, 1302 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span> 1303 } 1304 ); 1305})();</code></pre> 1306<pre><code class="language-js">(<span class="hljs-keyword">async</span> () => { 1307 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>( 1308 <span class="hljs-keyword">async</span> () => { 1309 <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>); 1310 }, 1311 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1312 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'TypeError'</span>); 1313 assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, <span class="hljs-string">'Wrong value'</span>); 1314 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 1315 } 1316 ); 1317})();</code></pre> 1318<pre><code class="language-js">assert.<span class="hljs-title function_">rejects</span>( 1319 <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>)), 1320 <span class="hljs-title class_">Error</span> 1321).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 1322 <span class="hljs-comment">// ...</span> 1323});</code></pre> 1324<p><code>error</code> cannot be a string. If a string is provided as the second 1325argument, then <code>error</code> is assumed to be omitted and the string will be used for 1326<code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the 1327example in <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> carefully if using a string as the second 1328argument gets considered.</p> 1329</section><section><h3><code>assert.strictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_strictequal_actual_expected_message" id="assert_assert_strictequal_actual_expected_message">#</a></span></h3> 1330<div class="api_metadata"> 1331<details class="changelog"><summary>History</summary> 1332<table> 1333<tbody><tr><th>Version</th><th>Changes</th></tr> 1334<tr><td>v10.0.0</td> 1335<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code>.</p></td></tr> 1336<tr><td>v0.1.21</td> 1337<td><p><span>Added in: v0.1.21</span></p></td></tr> 1338</tbody></table> 1339</details> 1340</div> 1341<ul> 1342<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1343<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1344<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> 1345</ul> 1346<p>Tests strict equality between the <code>actual</code> and <code>expected</code> parameters as 1347determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.</p> 1348<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).<span class="hljs-property">strict</span>; 1349 1350assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); 1351<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span> 1352<span class="hljs-comment">//</span> 1353<span class="hljs-comment">// 1 !== 2</span> 1354 1355assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 1356<span class="hljs-comment">// OK</span> 1357 1358assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello foobar'</span>, <span class="hljs-string">'Hello World!'</span>); 1359<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span> 1360<span class="hljs-comment">// + actual - expected</span> 1361<span class="hljs-comment">//</span> 1362<span class="hljs-comment">// + 'Hello foobar'</span> 1363<span class="hljs-comment">// - 'Hello World!'</span> 1364<span class="hljs-comment">// ^</span> 1365 1366<span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>; 1367<span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>; 1368assert.<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>); 1369<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span> 1370 1371assert.<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>)); 1372<span class="hljs-comment">// TypeError: Inputs are not identical</span></code></pre> 1373<p>If the values are not strictly equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a 1374<code>message</code> property set equal to the value of the <code>message</code> parameter. If the 1375<code>message</code> parameter is undefined, a default error message is assigned. If the 1376<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown 1377instead of the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 1378</section><section><h3><code>assert.throws(fn[, error][, message])</code><span><a class="mark" href="#assert_assert_throws_fn_error_message" id="assert_assert_throws_fn_error_message">#</a></span></h3> 1379<div class="api_metadata"> 1380<details class="changelog"><summary>History</summary> 1381<table> 1382<tbody><tr><th>Version</th><th>Changes</th></tr> 1383<tr><td>v10.2.0</td> 1384<td><p>The <code>error</code> parameter can be an object containing regular expressions now.</p></td></tr> 1385<tr><td>v9.9.0</td> 1386<td><p>The <code>error</code> parameter can now be an object as well.</p></td></tr> 1387<tr><td>v4.2.0</td> 1388<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr> 1389<tr><td>v0.1.21</td> 1390<td><p><span>Added in: v0.1.21</span></p></td></tr> 1391</tbody></table> 1392</details> 1393</div> 1394<ul> 1395<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1396<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> 1397<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1398</ul> 1399<p>Expects the function <code>fn</code> to throw an error.</p> 1400<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, 1401a validation object where each property will be tested for strict deep equality, 1402or an instance of error where each property will be tested for strict deep 1403equality including the non-enumerable <code>message</code> and <code>name</code> properties. When 1404using an object, it is also possible to use a regular expression, when 1405validating against a string property. See below for examples.</p> 1406<p>If specified, <code>message</code> will be appended to the message provided by the 1407<code>AssertionError</code> if the <code>fn</code> call fails to throw or in case the error validation 1408fails.</p> 1409<p>Custom validation object/error instance:</p> 1410<pre><code class="language-js"><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>); 1411err.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>; 1412err.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>; 1413err.<span class="hljs-property">info</span> = { 1414 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 1415 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span> 1416}; 1417err.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>; 1418 1419assert.<span class="hljs-title function_">throws</span>( 1420 <span class="hljs-function">() =></span> { 1421 <span class="hljs-keyword">throw</span> err; 1422 }, 1423 { 1424 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>, 1425 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>, 1426 <span class="hljs-attr">info</span>: { 1427 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 1428 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span> 1429 } 1430 <span class="hljs-comment">// Only properties on the validation object will be tested for.</span> 1431 <span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwise</span> 1432 <span class="hljs-comment">// the validation is going to fail.</span> 1433 } 1434); 1435 1436<span class="hljs-comment">// Using regular expressions to validate error properties:</span> 1437assert.<span class="hljs-title function_">throws</span>( 1438 <span class="hljs-function">() =></span> { 1439 <span class="hljs-keyword">throw</span> err; 1440 }, 1441 { 1442 <span class="hljs-comment">// The `name` and `message` properties are strings and using regular</span> 1443 <span class="hljs-comment">// expressions on those will match against the string. If they fail, an</span> 1444 <span class="hljs-comment">// error is thrown.</span> 1445 <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>, 1446 <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>, 1447 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, 1448 <span class="hljs-attr">info</span>: { 1449 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>, 1450 <span class="hljs-comment">// It is not possible to use regular expressions for nested properties!</span> 1451 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span> 1452 }, 1453 <span class="hljs-comment">// The `reg` property contains a regular expression and only if the</span> 1454 <span class="hljs-comment">// validation object contains an identical regular expression, it is going</span> 1455 <span class="hljs-comment">// to pass.</span> 1456 <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span> 1457 } 1458); 1459 1460<span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span> 1461assert.<span class="hljs-title function_">throws</span>( 1462 <span class="hljs-function">() =></span> { 1463 <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>); 1464 <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span> 1465 <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)) { 1466 otherErr[key] = value; 1467 } 1468 <span class="hljs-keyword">throw</span> otherErr; 1469 }, 1470 <span class="hljs-comment">// The error's `message` and `name` properties will also be checked when using</span> 1471 <span class="hljs-comment">// an error as validation object.</span> 1472 err 1473);</code></pre> 1474<p>Validate instanceof using constructor:</p> 1475<pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>( 1476 <span class="hljs-function">() =></span> { 1477 <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>); 1478 }, 1479 <span class="hljs-title class_">Error</span> 1480);</code></pre> 1481<p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>:</p> 1482<p>Using a regular expression runs <code>.toString</code> on the error object, and will 1483therefore also include the error name.</p> 1484<pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>( 1485 <span class="hljs-function">() =></span> { 1486 <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>); 1487 }, 1488 <span class="hljs-regexp">/^Error: Wrong value$/</span> 1489);</code></pre> 1490<p>Custom error validation:</p> 1491<p>The function must return <code>true</code> to indicate all internal validations passed. 1492It will otherwise fail with an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p> 1493<pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>( 1494 <span class="hljs-function">() =></span> { 1495 <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>); 1496 }, 1497 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1498 <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>); 1499 <span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span class="hljs-title function_">test</span>(err)); 1500 <span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</span> 1501 <span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instead,</span> 1502 <span class="hljs-comment">// throw an error about the specific validation that failed (as done in this</span> 1503 <span class="hljs-comment">// example) and add as much helpful debugging information to that error as</span> 1504 <span class="hljs-comment">// possible.</span> 1505 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 1506 }, 1507 <span class="hljs-string">'unexpected error'</span> 1508);</code></pre> 1509<p><code>error</code> cannot be a string. If a string is provided as the second 1510argument, then <code>error</code> is assumed to be omitted and the string will be used for 1511<code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same 1512message as the thrown error message is going to result in an 1513<code>ERR_AMBIGUOUS_ARGUMENT</code> error. Please read the example below carefully if using 1514a string as the second argument gets considered:</p> 1515<!-- eslint-disable no-restricted-syntax --> 1516<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingFirst</span>(<span class="hljs-params"></span>) { 1517 <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>); 1518} 1519 1520<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>(<span class="hljs-params"></span>) { 1521 <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>); 1522} 1523 1524<span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<span class="hljs-params"></span>) {} 1525 1526<span class="hljs-comment">// The second argument is a string and the input function threw an Error.</span> 1527<span class="hljs-comment">// The first case will not throw as it does not match for the error message</span> 1528<span class="hljs-comment">// thrown by the input function!</span> 1529assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'Second'</span>); 1530<span class="hljs-comment">// In the next example the message has no benefit over the message from the</span> 1531<span class="hljs-comment">// error and since it is not clear if the user intended to actually match</span> 1532<span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.</span> 1533assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'Second'</span>); 1534<span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span> 1535 1536<span class="hljs-comment">// The string is only used (as message) in case the function does not throw:</span> 1537assert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Second'</span>); 1538<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</span> 1539 1540<span class="hljs-comment">// If it was intended to match for the error message do this instead:</span> 1541<span class="hljs-comment">// It does not throw because the error messages match.</span> 1542assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/Second$/</span>); 1543 1544<span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</span> 1545assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/Second$/</span>); 1546<span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code></pre> 1547<p>Due to the confusing error-prone notation, avoid a string as the second 1548argument.</p></section> 1549 <!-- API END --> 1550 </div> 1551 </div> 1552 </div> 1553 <script> 1554 'use strict'; 1555 { 1556 const kCustomPreference = 'customDarkTheme'; 1557 const userSettings = sessionStorage.getItem(kCustomPreference); 1558 const themeToggleButton = document.getElementById('theme-toggle-btn'); 1559 if (userSettings === null && window.matchMedia) { 1560 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 1561 if ('onchange' in mq) { 1562 function mqChangeListener(e) { 1563 document.body.classList.toggle('dark-mode', e.matches); 1564 } 1565 mq.addEventListener('change', mqChangeListener); 1566 if (themeToggleButton) { 1567 themeToggleButton.addEventListener('click', function() { 1568 mq.removeEventListener('change', mqChangeListener); 1569 }, { once: true }); 1570 } 1571 } 1572 if (mq.matches) { 1573 document.body.classList.add('dark-mode'); 1574 } 1575 } else if (userSettings === 'true') { 1576 document.body.classList.add('dark-mode'); 1577 } 1578 if (themeToggleButton) { 1579 themeToggleButton.hidden = false; 1580 themeToggleButton.addEventListener('click', function() { 1581 sessionStorage.setItem( 1582 kCustomPreference, 1583 document.body.classList.toggle('dark-mode') 1584 ); 1585 }); 1586 } 1587 } 1588 </script> 1589</body> 1590</html> 1591