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