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