xref: /third_party/node/doc/api/assert.html (revision 1cb0ef41)
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="v18.20.1">
7  <title>Assert | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/assert.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:598px){.with-47-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:622px){.with-50-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:542px){.with-40-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-assert">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert active">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="assert" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <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"/>
104              <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"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <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"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115            
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#assert">Assert</a></span>
124<ul>
125<li><a href="#strict-assertion-mode">Strict assertion mode</a></li>
126<li><a href="#legacy-assertion-mode">Legacy assertion mode</a></li>
127<li><a href="#class-assertassertionerror">Class: assert.AssertionError</a>
128<ul>
129<li><a href="#new-assertassertionerroroptions"><code>new assert.AssertionError(options)</code></a></li>
130</ul>
131</li>
132<li><span class="stability_1"><a href="#class-assertcalltracker">Class: <code>assert.CallTracker</code></a></span>
133<ul>
134<li><a href="#new-assertcalltracker"><code>new assert.CallTracker()</code></a></li>
135<li><a href="#trackercallsfn-exact"><code>tracker.calls([fn][, exact])</code></a></li>
136<li><a href="#trackergetcallsfn"><code>tracker.getCalls(fn)</code></a></li>
137<li><a href="#trackerreport"><code>tracker.report()</code></a></li>
138<li><a href="#trackerresetfn"><code>tracker.reset([fn])</code></a></li>
139<li><a href="#trackerverify"><code>tracker.verify()</code></a></li>
140</ul>
141</li>
142<li><a href="#assertvalue-message"><code>assert(value[, message])</code></a></li>
143<li><a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual(actual, expected[, message])</code></a>
144<ul>
145<li><a href="#comparison-details">Comparison details</a></li>
146</ul>
147</li>
148<li><a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual(actual, expected[, message])</code></a>
149<ul>
150<li><a href="#comparison-details_1">Comparison details</a></li>
151</ul>
152</li>
153<li><a href="#assertdoesnotmatchstring-regexp-message"><code>assert.doesNotMatch(string, regexp[, message])</code></a></li>
154<li><a href="#assertdoesnotrejectasyncfn-error-message"><code>assert.doesNotReject(asyncFn[, error][, message])</code></a></li>
155<li><a href="#assertdoesnotthrowfn-error-message"><code>assert.doesNotThrow(fn[, error][, message])</code></a></li>
156<li><a href="#assertequalactual-expected-message"><code>assert.equal(actual, expected[, message])</code></a></li>
157<li><a href="#assertfailmessage"><code>assert.fail([message])</code></a></li>
158<li><span class="stability_0"><a href="#assertfailactual-expected-message-operator-stackstartfn"><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code></a></span></li>
159<li><a href="#assertiferrorvalue"><code>assert.ifError(value)</code></a></li>
160<li><a href="#assertmatchstring-regexp-message"><code>assert.match(string, regexp[, message])</code></a></li>
161<li><a href="#assertnotdeepequalactual-expected-message"><code>assert.notDeepEqual(actual, expected[, message])</code></a></li>
162<li><a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual(actual, expected[, message])</code></a></li>
163<li><a href="#assertnotequalactual-expected-message"><code>assert.notEqual(actual, expected[, message])</code></a></li>
164<li><a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual(actual, expected[, message])</code></a></li>
165<li><a href="#assertokvalue-message"><code>assert.ok(value[, message])</code></a></li>
166<li><a href="#assertrejectsasyncfn-error-message"><code>assert.rejects(asyncFn[, error][, message])</code></a></li>
167<li><a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual(actual, expected[, message])</code></a></li>
168<li><a href="#assertthrowsfn-error-message"><code>assert.throws(fn[, error][, message])</code></a></li>
169</ul>
170</li>
171</ul></div></div>
172    </li>
173  
174            
175    <li class="picker-header">
176      <a href="#">
177        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
178        Index
179      </a>
180
181      <div class="picker"><ul>
182<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
183<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
184
185      <li>
186        <a href="index.html">Index</a>
187      </li>
188    </ul>
189  
190<hr class="line">
191<ul>
192<li><a href="assert.html" class="nav-assert active">Assertion testing</a></li>
193<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
194<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
195<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
196<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
197<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
198<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
199<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
200<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
201<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
202<li><a href="console.html" class="nav-console">Console</a></li>
203<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
204<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
205<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
206<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
207<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
208<li><a href="dns.html" class="nav-dns">DNS</a></li>
209<li><a href="domain.html" class="nav-domain">Domain</a></li>
210<li><a href="errors.html" class="nav-errors">Errors</a></li>
211<li><a href="events.html" class="nav-events">Events</a></li>
212<li><a href="fs.html" class="nav-fs">File system</a></li>
213<li><a href="globals.html" class="nav-globals">Globals</a></li>
214<li><a href="http.html" class="nav-http">HTTP</a></li>
215<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
216<li><a href="https.html" class="nav-https">HTTPS</a></li>
217<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
218<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
219<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
220<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
221<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
222<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
223<li><a href="net.html" class="nav-net">Net</a></li>
224<li><a href="os.html" class="nav-os">OS</a></li>
225<li><a href="path.html" class="nav-path">Path</a></li>
226<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
227<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
228<li><a href="process.html" class="nav-process">Process</a></li>
229<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
230<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
231<li><a href="readline.html" class="nav-readline">Readline</a></li>
232<li><a href="repl.html" class="nav-repl">REPL</a></li>
233<li><a href="report.html" class="nav-report">Report</a></li>
234<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
235<li><a href="stream.html" class="nav-stream">Stream</a></li>
236<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
237<li><a href="test.html" class="nav-test">Test runner</a></li>
238<li><a href="timers.html" class="nav-timers">Timers</a></li>
239<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
240<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
241<li><a href="tty.html" class="nav-tty">TTY</a></li>
242<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
243<li><a href="url.html" class="nav-url">URL</a></li>
244<li><a href="util.html" class="nav-util">Utilities</a></li>
245<li><a href="v8.html" class="nav-v8">V8</a></li>
246<li><a href="vm.html" class="nav-vm">VM</a></li>
247<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
248<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
249<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
250<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
251<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
252</ul>
253<hr class="line">
254<ul>
255<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
256</ul></div>
257    </li>
258  
259            
260    <li class="picker-header">
261      <a href="#">
262        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
263        Other versions
264      </a>
265      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/assert.html">21.x</a></li>
266<li><a href="https://nodejs.org/docs/latest-v20.x/api/assert.html">20.x <b>LTS</b></a></li>
267<li><a href="https://nodejs.org/docs/latest-v19.x/api/assert.html">19.x</a></li>
268<li><a href="https://nodejs.org/docs/latest-v18.x/api/assert.html">18.x <b>LTS</b></a></li>
269<li><a href="https://nodejs.org/docs/latest-v17.x/api/assert.html">17.x</a></li>
270<li><a href="https://nodejs.org/docs/latest-v16.x/api/assert.html">16.x</a></li>
271<li><a href="https://nodejs.org/docs/latest-v15.x/api/assert.html">15.x</a></li>
272<li><a href="https://nodejs.org/docs/latest-v14.x/api/assert.html">14.x</a></li>
273<li><a href="https://nodejs.org/docs/latest-v13.x/api/assert.html">13.x</a></li>
274<li><a href="https://nodejs.org/docs/latest-v12.x/api/assert.html">12.x</a></li>
275<li><a href="https://nodejs.org/docs/latest-v11.x/api/assert.html">11.x</a></li>
276<li><a href="https://nodejs.org/docs/latest-v10.x/api/assert.html">10.x</a></li>
277<li><a href="https://nodejs.org/docs/latest-v9.x/api/assert.html">9.x</a></li>
278<li><a href="https://nodejs.org/docs/latest-v8.x/api/assert.html">8.x</a></li>
279<li><a href="https://nodejs.org/docs/latest-v7.x/api/assert.html">7.x</a></li>
280<li><a href="https://nodejs.org/docs/latest-v6.x/api/assert.html">6.x</a></li>
281<li><a href="https://nodejs.org/docs/latest-v5.x/api/assert.html">5.x</a></li>
282<li><a href="https://nodejs.org/docs/latest-v4.x/api/assert.html">4.x</a></li>
283<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/assert.html">0.12.x</a></li>
284<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/assert.html">0.10.x</a></li></ol></div>
285    </li>
286  
287            <li class="picker-header">
288              <a href="#">
289                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
290                Options
291              </a>
292        
293              <div class="picker">
294                <ul>
295                  <li>
296                    <a href="all.html">View on single page</a>
297                  </li>
298                  <li>
299                    <a href="assert.json">View as JSON</a>
300                  </li>
301                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/assert.md">Edit on GitHub</a></li>    
302                </ul>
303              </div>
304            </li>
305          </ul>
306        </div>
307        <hr>
308      </header>
309
310      <details id="toc" open><summary>Table of contents</summary><ul>
311<li><span class="stability_2"><a href="#assert">Assert</a></span>
312<ul>
313<li><a href="#strict-assertion-mode">Strict assertion mode</a></li>
314<li><a href="#legacy-assertion-mode">Legacy assertion mode</a></li>
315<li><a href="#class-assertassertionerror">Class: assert.AssertionError</a>
316<ul>
317<li><a href="#new-assertassertionerroroptions"><code>new assert.AssertionError(options)</code></a></li>
318</ul>
319</li>
320<li><span class="stability_1"><a href="#class-assertcalltracker">Class: <code>assert.CallTracker</code></a></span>
321<ul>
322<li><a href="#new-assertcalltracker"><code>new assert.CallTracker()</code></a></li>
323<li><a href="#trackercallsfn-exact"><code>tracker.calls([fn][, exact])</code></a></li>
324<li><a href="#trackergetcallsfn"><code>tracker.getCalls(fn)</code></a></li>
325<li><a href="#trackerreport"><code>tracker.report()</code></a></li>
326<li><a href="#trackerresetfn"><code>tracker.reset([fn])</code></a></li>
327<li><a href="#trackerverify"><code>tracker.verify()</code></a></li>
328</ul>
329</li>
330<li><a href="#assertvalue-message"><code>assert(value[, message])</code></a></li>
331<li><a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual(actual, expected[, message])</code></a>
332<ul>
333<li><a href="#comparison-details">Comparison details</a></li>
334</ul>
335</li>
336<li><a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual(actual, expected[, message])</code></a>
337<ul>
338<li><a href="#comparison-details_1">Comparison details</a></li>
339</ul>
340</li>
341<li><a href="#assertdoesnotmatchstring-regexp-message"><code>assert.doesNotMatch(string, regexp[, message])</code></a></li>
342<li><a href="#assertdoesnotrejectasyncfn-error-message"><code>assert.doesNotReject(asyncFn[, error][, message])</code></a></li>
343<li><a href="#assertdoesnotthrowfn-error-message"><code>assert.doesNotThrow(fn[, error][, message])</code></a></li>
344<li><a href="#assertequalactual-expected-message"><code>assert.equal(actual, expected[, message])</code></a></li>
345<li><a href="#assertfailmessage"><code>assert.fail([message])</code></a></li>
346<li><span class="stability_0"><a href="#assertfailactual-expected-message-operator-stackstartfn"><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code></a></span></li>
347<li><a href="#assertiferrorvalue"><code>assert.ifError(value)</code></a></li>
348<li><a href="#assertmatchstring-regexp-message"><code>assert.match(string, regexp[, message])</code></a></li>
349<li><a href="#assertnotdeepequalactual-expected-message"><code>assert.notDeepEqual(actual, expected[, message])</code></a></li>
350<li><a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual(actual, expected[, message])</code></a></li>
351<li><a href="#assertnotequalactual-expected-message"><code>assert.notEqual(actual, expected[, message])</code></a></li>
352<li><a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual(actual, expected[, message])</code></a></li>
353<li><a href="#assertokvalue-message"><code>assert.ok(value[, message])</code></a></li>
354<li><a href="#assertrejectsasyncfn-error-message"><code>assert.rejects(asyncFn[, error][, message])</code></a></li>
355<li><a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual(actual, expected[, message])</code></a></li>
356<li><a href="#assertthrowsfn-error-message"><code>assert.throws(fn[, error][, message])</code></a></li>
357</ul>
358</li>
359</ul></details>
360
361      <div id="apicontent">
362        <h2>Assert<span><a class="mark" href="#assert" id="assert">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert"></a></h2>
363
364<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
365<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/assert.js">lib/assert.js</a></p>
366<p>The <code>node:assert</code> module provides a set of assertion functions for verifying
367invariants.</p>
368<section><h3>Strict assertion mode<span><a class="mark" href="#strict-assertion-mode" id="strict-assertion-mode">#</a></span><a aria-hidden="true" class="legacy" id="assert_strict_assertion_mode"></a></h3>
369<div class="api_metadata">
370<details class="changelog"><summary>History</summary>
371<table>
372<tbody><tr><th>Version</th><th>Changes</th></tr>
373<tr><td>v15.0.0</td>
374<td><p>Exposed as <code>require('node:assert/strict')</code>.</p></td></tr>
375<tr><td>v13.9.0, v12.16.2</td>
376<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>
377<tr><td>v9.9.0</td>
378<td><p>Added error diffs to the strict assertion mode.</p></td></tr>
379<tr><td>v9.9.0</td>
380<td><p>Added strict assertion mode to the assert module.</p></td></tr>
381<tr><td>v9.9.0</td>
382<td><p><span>Added in: v9.9.0</span></p></td></tr>
383</tbody></table>
384</details>
385</div>
386<p>In strict assertion mode, non-strict methods behave like their corresponding
387strict methods. For example, <a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a> will behave like
388<a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a>.</p>
389<p>In strict assertion mode, error messages for objects display a diff. In legacy
390assertion mode, error messages for objects display the objects, often truncated.</p>
391<p>To use strict assertion mode:</p>
392
393<pre class="with-47-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { strict <span class="hljs-keyword">as</span> assert } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>).<span class="hljs-property">strict</span>;</code><button class="copy-button">copy</button></pre>
394
395<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);</code><button class="copy-button">copy</button></pre>
396<p>Example error diff:</p>
397
398<pre class="with-47-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { strict <span class="hljs-keyword">as</span> assert } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
399
400assert.<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>]);
401<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
402<span class="hljs-comment">// + actual - expected ... Lines skipped</span>
403<span class="hljs-comment">//</span>
404<span class="hljs-comment">//   [</span>
405<span class="hljs-comment">//     [</span>
406<span class="hljs-comment">// ...</span>
407<span class="hljs-comment">//       2,</span>
408<span class="hljs-comment">// +     3</span>
409<span class="hljs-comment">// -     '3'</span>
410<span class="hljs-comment">//     ],</span>
411<span class="hljs-comment">// ...</span>
412<span class="hljs-comment">//     5</span>
413<span class="hljs-comment">//   ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
414
415assert.<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>]);
416<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
417<span class="hljs-comment">// + actual - expected ... Lines skipped</span>
418<span class="hljs-comment">//</span>
419<span class="hljs-comment">//   [</span>
420<span class="hljs-comment">//     [</span>
421<span class="hljs-comment">// ...</span>
422<span class="hljs-comment">//       2,</span>
423<span class="hljs-comment">// +     3</span>
424<span class="hljs-comment">// -     '3'</span>
425<span class="hljs-comment">//     ],</span>
426<span class="hljs-comment">// ...</span>
427<span class="hljs-comment">//     5</span>
428<span class="hljs-comment">//   ]</span></code><button class="copy-button">copy</button></pre>
429<p>To deactivate the colors, use the <code>NO_COLOR</code> or <code>NODE_DISABLE_COLORS</code>
430environment variables. This will also deactivate the colors in the REPL. For
431more on color support in terminal environments, read the tty
432<a href="tty.html#writestreamgetcolordepthenv"><code>getColorDepth()</code></a> documentation.</p>
433</section><section><h3>Legacy assertion mode<span><a class="mark" href="#legacy-assertion-mode" id="legacy-assertion-mode">#</a></span><a aria-hidden="true" class="legacy" id="assert_legacy_assertion_mode"></a></h3>
434<p>Legacy assertion mode uses the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a> in:</p>
435<ul>
436<li><a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a></li>
437<li><a href="#assertequalactual-expected-message"><code>assert.equal()</code></a></li>
438<li><a href="#assertnotdeepequalactual-expected-message"><code>assert.notDeepEqual()</code></a></li>
439<li><a href="#assertnotequalactual-expected-message"><code>assert.notEqual()</code></a></li>
440</ul>
441<p>To use legacy assertion mode:</p>
442
443<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);</code><button class="copy-button">copy</button></pre>
444<p>Legacy assertion mode may have surprising results, especially when using
445<a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a>:</p>
446<pre><code class="language-js cjs"><span class="hljs-comment">// WARNING: This does not throw an AssertionError in legacy assertion mode!</span>
447assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-regexp">/a/gi</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>());</code> <button class="copy-button">copy</button></pre>
448</section><section><h3>Class: assert.AssertionError<a class="srclink" href="https://github.com/nodejs/node/blob/1d526a47af29d59ad196c4e05606b0ec6f8090d9/lib/assert.js#L178">[src]</a><span><a class="mark" href="#class-assertassertionerror" id="class-assertassertionerror">#</a></span><a aria-hidden="true" class="legacy" id="assert_class_assert_assertionerror"></a></h3>
449<ul>
450<li>Extends: <a href="errors.html#class-error" class="type">&#x3C;errors.Error></a></li>
451</ul>
452<p>Indicates the failure of an assertion. All errors thrown by the <code>node:assert</code>
453module will be instances of the <code>AssertionError</code> class.</p>
454<h4><code>new assert.AssertionError(options)</code><span><a class="mark" href="#new-assertassertionerroroptions" id="new-assertassertionerroroptions">#</a></span><a aria-hidden="true" class="legacy" id="assert_new_assert_assertionerror_options"></a></h4>
455<div class="api_metadata">
456<span>Added in: v0.1.21</span>
457</div>
458<ul>
459<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
460<ul>
461<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>
462<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>
463<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>
464<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>
465<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
466frames before this function.</li>
467</ul>
468</li>
469</ul>
470<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p>
471<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>)
472and:</p>
473<ul>
474<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
475<a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></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> Set to the <code>expected</code> value for methods such as
477<a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a>.</li>
478<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
479(<code>true</code>) or not.</li>
480<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
481assertion error.</li>
482<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>
483</ul>
484
485<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
486
487<span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span>
488<span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">AssertionError</span>({
489  <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>,
490  <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>,
491  <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>,
492});
493
494<span class="hljs-comment">// Verify error output:</span>
495<span class="hljs-keyword">try</span> {
496  assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
497} <span class="hljs-keyword">catch</span> (err) {
498  <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> assert.<span class="hljs-property">AssertionError</span>);
499  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, message);
500  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'AssertionError'</span>);
501  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actual</span>, <span class="hljs-number">1</span>);
502  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expected</span>, <span class="hljs-number">2</span>);
503  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code</span>, <span class="hljs-string">'ERR_ASSERTION'</span>);
504  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">operator</span>, <span class="hljs-string">'strictEqual'</span>);
505  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generatedMessage</span>, <span class="hljs-literal">true</span>);
506}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
507
508<span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span>
509<span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">AssertionError</span>({
510  <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>,
511  <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>,
512  <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>,
513});
514
515<span class="hljs-comment">// Verify error output:</span>
516<span class="hljs-keyword">try</span> {
517  assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
518} <span class="hljs-keyword">catch</span> (err) {
519  <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> assert.<span class="hljs-property">AssertionError</span>);
520  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, message);
521  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'AssertionError'</span>);
522  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actual</span>, <span class="hljs-number">1</span>);
523  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expected</span>, <span class="hljs-number">2</span>);
524  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code</span>, <span class="hljs-string">'ERR_ASSERTION'</span>);
525  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">operator</span>, <span class="hljs-string">'strictEqual'</span>);
526  assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generatedMessage</span>, <span class="hljs-literal">true</span>);
527}</code><button class="copy-button">copy</button></pre>
528</section><section><h3>Class: <code>assert.CallTracker</code><span><a class="mark" href="#class-assertcalltracker" id="class-assertcalltracker">#</a></span><a aria-hidden="true" class="legacy" id="assert_class_assert_calltracker"></a></h3>
529<div class="api_metadata">
530<span>Added in: v14.2.0, v12.19.0</span>
531</div>
532<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
533<p>This feature is currently experimental and behavior might still change.</p>
534<h4><code>new assert.CallTracker()</code><span><a class="mark" href="#new-assertcalltracker" id="new-assertcalltracker">#</a></span><a aria-hidden="true" class="legacy" id="assert_new_assert_calltracker"></a></h4>
535<div class="api_metadata">
536<span>Added in: v14.2.0, v12.19.0</span>
537</div>
538<p>Creates a new <a href="#class-assertcalltracker"><code>CallTracker</code></a> object which can be used to track if functions
539were called a specific number of times. The <code>tracker.verify()</code> must be called
540for the verification to take place. The usual pattern would be to call it in a
541<a href="process.html#event-exit"><code>process.on('exit')</code></a> handler.</p>
542
543<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
544<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
545
546<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
547
548<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
549
550<span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</span>
551<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">1</span>);
552
553<span class="hljs-title function_">callsfunc</span>();
554
555<span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions have</span>
556<span class="hljs-comment">// been called exact times.</span>
557process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
558  tracker.<span class="hljs-title function_">verify</span>();
559});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
560
561<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
562
563<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
564
565<span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</span>
566<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">1</span>);
567
568<span class="hljs-title function_">callsfunc</span>();
569
570<span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions have</span>
571<span class="hljs-comment">// been called exact times.</span>
572process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
573  tracker.<span class="hljs-title function_">verify</span>();
574});</code><button class="copy-button">copy</button></pre>
575<h4><code>tracker.calls([fn][, exact])</code><span><a class="mark" href="#trackercallsfn-exact" id="trackercallsfn-exact">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_calls_fn_exact"></a></h4>
576<div class="api_metadata">
577<span>Added in: v14.2.0, v12.19.0</span>
578</div>
579<ul>
580<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>
581<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>
582<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>
583</ul>
584<p>The wrapper function is expected to be called exactly <code>exact</code> times. If the
585function has not been called exactly <code>exact</code> times when
586<a href="#trackerverify"><code>tracker.verify()</code></a> is called, then <a href="#trackerverify"><code>tracker.verify()</code></a> will throw an
587error.</p>
588
589<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
590
591<span class="hljs-comment">// Creates call tracker.</span>
592<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
593
594<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
595
596<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span>
597<span class="hljs-comment">// before tracker.verify().</span>
598<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
599
600<span class="hljs-comment">// Creates call tracker.</span>
601<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
602
603<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
604
605<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span>
606<span class="hljs-comment">// before tracker.verify().</span>
607<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);</code><button class="copy-button">copy</button></pre>
608<h4><code>tracker.getCalls(fn)</code><span><a class="mark" href="#trackergetcallsfn" id="trackergetcallsfn">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_getcalls_fn"></a></h4>
609<div class="api_metadata">
610<span>Added in: v18.8.0</span>
611</div>
612<ul>
613<li>
614<p><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>.</p>
615</li>
616<li>
617<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> with all the calls to a tracked function.</p>
618</li>
619<li>
620<p>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
621<ul>
622<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
623<li><code>arguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> the arguments passed to the tracked function</li>
624</ul>
625</li>
626</ul>
627
628<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
629
630<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
631
632<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
633<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);
634<span class="hljs-title function_">callsfunc</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>);
635
636assert.<span class="hljs-title function_">deepStrictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc),
637                       [{ <span class="hljs-attr">thisArg</span>: <span class="hljs-literal">undefined</span>, <span class="hljs-attr">arguments</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] }]);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
638
639<span class="hljs-comment">// Creates call tracker.</span>
640<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
641
642<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
643<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);
644<span class="hljs-title function_">callsfunc</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>);
645
646assert.<span class="hljs-title function_">deepStrictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc),
647                       [{ <span class="hljs-attr">thisArg</span>: <span class="hljs-literal">undefined</span>, <span class="hljs-attr">arguments</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] }]);</code><button class="copy-button">copy</button></pre>
648<h4><code>tracker.report()</code><span><a class="mark" href="#trackerreport" id="trackerreport">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_report"></a></h4>
649<div class="api_metadata">
650<span>Added in: v14.2.0, v12.19.0</span>
651</div>
652<ul>
653<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
654returned by <a href="#trackercallsfn-exact"><code>tracker.calls()</code></a>.</li>
655<li>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
656<ul>
657<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>
658<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>
659<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
660called.</li>
661<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>
662<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>
663</ul>
664</li>
665</ul>
666<p>The arrays contains information about the expected and actual number of calls of
667the functions that have not been called the expected number of times.</p>
668
669<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
670
671<span class="hljs-comment">// Creates call tracker.</span>
672<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
673
674<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
675
676<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span>
677<span class="hljs-comment">// before tracker.verify().</span>
678<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>);
679
680<span class="hljs-comment">// Returns an array containing information on callsfunc()</span>
681<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(tracker.<span class="hljs-title function_">report</span>());
682<span class="hljs-comment">// [</span>
683<span class="hljs-comment">//  {</span>
684<span class="hljs-comment">//    message: 'Expected the func function to be executed 2 time(s) but was</span>
685<span class="hljs-comment">//    executed 0 time(s).',</span>
686<span class="hljs-comment">//    actual: 0,</span>
687<span class="hljs-comment">//    expected: 2,</span>
688<span class="hljs-comment">//    operator: 'func',</span>
689<span class="hljs-comment">//    stack: stack trace</span>
690<span class="hljs-comment">//  }</span>
691<span class="hljs-comment">// ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
692
693<span class="hljs-comment">// Creates call tracker.</span>
694<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
695
696<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
697
698<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span>
699<span class="hljs-comment">// before tracker.verify().</span>
700<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>);
701
702<span class="hljs-comment">// Returns an array containing information on callsfunc()</span>
703<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(tracker.<span class="hljs-title function_">report</span>());
704<span class="hljs-comment">// [</span>
705<span class="hljs-comment">//  {</span>
706<span class="hljs-comment">//    message: 'Expected the func function to be executed 2 time(s) but was</span>
707<span class="hljs-comment">//    executed 0 time(s).',</span>
708<span class="hljs-comment">//    actual: 0,</span>
709<span class="hljs-comment">//    expected: 2,</span>
710<span class="hljs-comment">//    operator: 'func',</span>
711<span class="hljs-comment">//    stack: stack trace</span>
712<span class="hljs-comment">//  }</span>
713<span class="hljs-comment">// ]</span></code><button class="copy-button">copy</button></pre>
714<h4><code>tracker.reset([fn])</code><span><a class="mark" href="#trackerresetfn" id="trackerresetfn">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_reset_fn"></a></h4>
715<div class="api_metadata">
716<span>Added in: v18.8.0</span>
717</div>
718<ul>
719<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> a tracked function to reset.</li>
720</ul>
721<p>Reset calls of the call tracker.
722If a tracked function is passed as an argument, the calls will be reset for it.
723If no arguments are passed, all tracked functions will be reset</p>
724
725<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
726
727<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
728
729<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
730<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);
731
732<span class="hljs-title function_">callsfunc</span>();
733<span class="hljs-comment">// Tracker was called once</span>
734assert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">1</span>);
735
736tracker.<span class="hljs-title function_">reset</span>(callsfunc);
737assert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">0</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
738
739<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
740
741<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
742<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func);
743
744<span class="hljs-title function_">callsfunc</span>();
745<span class="hljs-comment">// Tracker was called once</span>
746assert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">1</span>);
747
748tracker.<span class="hljs-title function_">reset</span>(callsfunc);
749assert.<span class="hljs-title function_">strictEqual</span>(tracker.<span class="hljs-title function_">getCalls</span>(callsfunc).<span class="hljs-property">length</span>, <span class="hljs-number">0</span>);</code><button class="copy-button">copy</button></pre>
750<h4><code>tracker.verify()</code><span><a class="mark" href="#trackerverify" id="trackerverify">#</a></span><a aria-hidden="true" class="legacy" id="assert_tracker_verify"></a></h4>
751<div class="api_metadata">
752<span>Added in: v14.2.0, v12.19.0</span>
753</div>
754<p>Iterates through the list of functions passed to
755<a href="#trackercallsfn-exact"><code>tracker.calls()</code></a> and will throw an error for functions that
756have not been called the expected number of times.</p>
757
758<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
759
760<span class="hljs-comment">// Creates call tracker.</span>
761<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
762
763<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
764
765<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span>
766<span class="hljs-comment">// before tracker.verify().</span>
767<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>);
768
769<span class="hljs-title function_">callsfunc</span>();
770
771<span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span>
772tracker.<span class="hljs-title function_">verify</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
773
774<span class="hljs-comment">// Creates call tracker.</span>
775<span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<span class="hljs-title class_">CallTracker</span>();
776
777<span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span class="hljs-params"></span>) {}
778
779<span class="hljs-comment">// Returns a function that wraps func() that must be called exact times</span>
780<span class="hljs-comment">// before tracker.verify().</span>
781<span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">calls</span>(func, <span class="hljs-number">2</span>);
782
783<span class="hljs-title function_">callsfunc</span>();
784
785<span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span>
786tracker.<span class="hljs-title function_">verify</span>();</code><button class="copy-button">copy</button></pre>
787</section><section><h3><code>assert(value[, message])</code><span><a class="mark" href="#assertvalue-message" id="assertvalue-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_value_message"></a></h3>
788<div class="api_metadata">
789<span>Added in: v0.5.9</span>
790</div>
791<ul>
792<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>
793<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>
794</ul>
795<p>An alias of <a href="#assertokvalue-message"><code>assert.ok()</code></a>.</p>
796</section><section><h3><code>assert.deepEqual(actual, expected[, message])</code><span><a class="mark" href="#assertdeepequalactual-expected-message" id="assertdeepequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_deepequal_actual_expected_message"></a></h3>
797<div class="api_metadata">
798<details class="changelog"><summary>History</summary>
799<table>
800<tbody><tr><th>Version</th><th>Changes</th></tr>
801<tr><td>v18.0.0</td>
802<td><p>Regular expressions lastIndex property is now compared as well.</p></td></tr>
803<tr><td>v16.0.0, v14.18.0</td>
804<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr>
805<tr><td>v14.0.0</td>
806<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr>
807<tr><td>v12.0.0</td>
808<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>
809<tr><td>v9.0.0</td>
810<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr>
811<tr><td>v8.0.0</td>
812<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr>
813<tr><td>v6.4.0, v4.7.1</td>
814<td><p>Typed array slices are handled correctly now.</p></td></tr>
815<tr><td>v6.1.0, v4.5.0</td>
816<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
817<tr><td>v5.10.1, v4.4.3</td>
818<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
819<tr><td>v0.1.21</td>
820<td><p><span>Added in: v0.1.21</span></p></td></tr>
821</tbody></table>
822</details>
823</div>
824<ul>
825<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>
826<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>
827<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>
828</ul>
829<p><strong>Strict assertion mode</strong></p>
830<p>An alias of <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a>.</p>
831<p><strong>Legacy assertion mode</strong></p>
832<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a> instead.</div><p></p>
833<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. Consider
834using <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a> instead. <a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a> can have
835surprising results.</p>
836<p><em>Deep equality</em> means that the enumerable "own" properties of child objects
837are also recursively evaluated by the following rules.</p>
838<h4>Comparison details<span><a class="mark" href="#comparison-details" id="comparison-details">#</a></span><a aria-hidden="true" class="legacy" id="assert_comparison_details"></a></h4>
839<ul>
840<li>Primitive values are compared with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a>,
841with the exception of <code>NaN</code>. It is treated as being identical in case
842both sides are <code>NaN</code>.</li>
843<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li>
844<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>
845<li><a href="errors.html#class-error"><code>Error</code></a> names and messages are always compared, even if these are not
846enumerable properties.</li>
847<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>
848<li><code>Object</code> properties are compared unordered.</li>
849<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>
850<li>Recursion stops when both sides differ or both sides encounter a circular
851reference.</li>
852<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
853objects.</li>
854<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>
855<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>
856<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> lastIndex, flags, and source are always compared, even if these
857are not enumerable properties.</li>
858</ul>
859<p>The following example does not throw an <a href="#class-assertassertionerror"><code>AssertionError</code></a> because the
860primitives are compared using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a>.</p>
861
862<pre class="with-50-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
863<span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span>
864
865assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</span>, <span class="hljs-literal">false</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
866<span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span>
867
868assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</span>, <span class="hljs-literal">false</span>);</code><button class="copy-button">copy</button></pre>
869<p>"Deep" equality means that the enumerable "own" properties of child objects
870are evaluated also:</p>
871
872<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
873
874<span class="hljs-keyword">const</span> obj1 = {
875  <span class="hljs-attr">a</span>: {
876    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
877  },
878};
879<span class="hljs-keyword">const</span> obj2 = {
880  <span class="hljs-attr">a</span>: {
881    <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>,
882  },
883};
884<span class="hljs-keyword">const</span> obj3 = {
885  <span class="hljs-attr">a</span>: {
886    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
887  },
888};
889<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1);
890
891assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1);
892<span class="hljs-comment">// OK</span>
893
894<span class="hljs-comment">// Values of b are different:</span>
895assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2);
896<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span>
897
898assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3);
899<span class="hljs-comment">// OK</span>
900
901<span class="hljs-comment">// Prototypes are ignored:</span>
902assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4);
903<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
904
905<span class="hljs-keyword">const</span> obj1 = {
906  <span class="hljs-attr">a</span>: {
907    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
908  },
909};
910<span class="hljs-keyword">const</span> obj2 = {
911  <span class="hljs-attr">a</span>: {
912    <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>,
913  },
914};
915<span class="hljs-keyword">const</span> obj3 = {
916  <span class="hljs-attr">a</span>: {
917    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
918  },
919};
920<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1);
921
922assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1);
923<span class="hljs-comment">// OK</span>
924
925<span class="hljs-comment">// Values of b are different:</span>
926assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2);
927<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span>
928
929assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3);
930<span class="hljs-comment">// OK</span>
931
932<span class="hljs-comment">// Prototypes are ignored:</span>
933assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4);
934<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code><button class="copy-button">copy</button></pre>
935<p>If the values are not equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
936property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
937parameter is undefined, a default error message is assigned. If the <code>message</code>
938parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
939<a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
940</section><section><h3><code>assert.deepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertdeepstrictequalactual-expected-message" id="assertdeepstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_deepstrictequal_actual_expected_message"></a></h3>
941<div class="api_metadata">
942<details class="changelog"><summary>History</summary>
943<table>
944<tbody><tr><th>Version</th><th>Changes</th></tr>
945<tr><td>v18.0.0</td>
946<td><p>Regular expressions lastIndex property is now compared as well.</p></td></tr>
947<tr><td>v9.0.0</td>
948<td><p>Enumerable symbol properties are now compared.</p></td></tr>
949<tr><td>v9.0.0</td>
950<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>
951<tr><td>v8.5.0</td>
952<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr>
953<tr><td>v8.0.0</td>
954<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr>
955<tr><td>v6.1.0</td>
956<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
957<tr><td>v6.4.0, v4.7.1</td>
958<td><p>Typed array slices are handled correctly now.</p></td></tr>
959<tr><td>v5.10.1, v4.4.3</td>
960<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
961<tr><td>v1.2.0</td>
962<td><p><span>Added in: v1.2.0</span></p></td></tr>
963</tbody></table>
964</details>
965</div>
966<ul>
967<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>
968<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>
969<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>
970</ul>
971<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.
972"Deep" equality means that the enumerable "own" properties of child objects
973are recursively evaluated also by the following rules.</p>
974<h4>Comparison details<span><a class="mark" href="#comparison-details_1" id="comparison-details_1">#</a></span><a aria-hidden="true" class="legacy" id="assert_comparison_details_1"></a></h4>
975<ul>
976<li>Primitive values are compared using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</li>
977<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li>
978<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
979the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality"><code>===</code> operator</a>.</li>
980<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>
981<li><a href="errors.html#class-error"><code>Error</code></a> names and messages are always compared, even if these are not
982enumerable properties.</li>
983<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>
984<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>
985<li><code>Object</code> properties are compared unordered.</li>
986<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>
987<li>Recursion stops when both sides differ or both sides encounter a circular
988reference.</li>
989<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
990below for further details.</li>
991<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> lastIndex, flags, and source are always compared, even if these
992are not enumerable properties.</li>
993</ul>
994
995<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
996
997<span class="hljs-comment">// This fails because 1 !== '1'.</span>
998assert.<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> });
999<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1000<span class="hljs-comment">// + actual - expected</span>
1001<span class="hljs-comment">//</span>
1002<span class="hljs-comment">//   {</span>
1003<span class="hljs-comment">// +   a: 1</span>
1004<span class="hljs-comment">// -   a: '1'</span>
1005<span class="hljs-comment">//   }</span>
1006
1007<span class="hljs-comment">// The following objects don't have own properties</span>
1008<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();
1009<span class="hljs-keyword">const</span> object = {};
1010<span class="hljs-keyword">const</span> fakeDate = {};
1011<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>);
1012
1013<span class="hljs-comment">// Different [[Prototype]]:</span>
1014assert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate);
1015<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1016<span class="hljs-comment">// + actual - expected</span>
1017<span class="hljs-comment">//</span>
1018<span class="hljs-comment">// + {}</span>
1019<span class="hljs-comment">// - Date {}</span>
1020
1021<span class="hljs-comment">// Different type tags:</span>
1022assert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate);
1023<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1024<span class="hljs-comment">// + actual - expected</span>
1025<span class="hljs-comment">//</span>
1026<span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span>
1027<span class="hljs-comment">// - Date {}</span>
1028
1029assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>);
1030<span class="hljs-comment">// OK because Object.is(NaN, NaN) is true.</span>
1031
1032<span class="hljs-comment">// Different unwrapped numbers:</span>
1033assert.<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>));
1034<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1035<span class="hljs-comment">// + actual - expected</span>
1036<span class="hljs-comment">//</span>
1037<span class="hljs-comment">// + [Number: 1]</span>
1038<span class="hljs-comment">// - [Number: 2]</span>
1039
1040assert.<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>));
1041<span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</span>
1042
1043assert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>);
1044<span class="hljs-comment">// OK</span>
1045
1046<span class="hljs-comment">// Different zeros:</span>
1047assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>);
1048<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1049<span class="hljs-comment">// + actual - expected</span>
1050<span class="hljs-comment">//</span>
1051<span class="hljs-comment">// + 0</span>
1052<span class="hljs-comment">// - -0</span>
1053
1054<span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>();
1055<span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>();
1056assert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol1]: <span class="hljs-number">1</span> });
1057<span class="hljs-comment">// OK, because it is the same symbol on both objects.</span>
1058
1059assert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol2]: <span class="hljs-number">1</span> });
1060<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:</span>
1061<span class="hljs-comment">//</span>
1062<span class="hljs-comment">// {</span>
1063<span class="hljs-comment">//   [Symbol()]: 1</span>
1064<span class="hljs-comment">// }</span>
1065
1066<span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>();
1067<span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>([[{}, {}]]);
1068<span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>();
1069weakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>;
1070
1071assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2);
1072<span class="hljs-comment">// OK, because it is impossible to compare the entries</span>
1073
1074<span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:</span>
1075assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3);
1076<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1077<span class="hljs-comment">// + actual - expected</span>
1078<span class="hljs-comment">//</span>
1079<span class="hljs-comment">//   WeakMap {</span>
1080<span class="hljs-comment">// +   [items unknown]</span>
1081<span class="hljs-comment">// -   [items unknown],</span>
1082<span class="hljs-comment">// -   unequal: true</span>
1083<span class="hljs-comment">//   }</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1084
1085<span class="hljs-comment">// This fails because 1 !== '1'.</span>
1086assert.<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> });
1087<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1088<span class="hljs-comment">// + actual - expected</span>
1089<span class="hljs-comment">//</span>
1090<span class="hljs-comment">//   {</span>
1091<span class="hljs-comment">// +   a: 1</span>
1092<span class="hljs-comment">// -   a: '1'</span>
1093<span class="hljs-comment">//   }</span>
1094
1095<span class="hljs-comment">// The following objects don't have own properties</span>
1096<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();
1097<span class="hljs-keyword">const</span> object = {};
1098<span class="hljs-keyword">const</span> fakeDate = {};
1099<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>);
1100
1101<span class="hljs-comment">// Different [[Prototype]]:</span>
1102assert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate);
1103<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1104<span class="hljs-comment">// + actual - expected</span>
1105<span class="hljs-comment">//</span>
1106<span class="hljs-comment">// + {}</span>
1107<span class="hljs-comment">// - Date {}</span>
1108
1109<span class="hljs-comment">// Different type tags:</span>
1110assert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate);
1111<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1112<span class="hljs-comment">// + actual - expected</span>
1113<span class="hljs-comment">//</span>
1114<span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span>
1115<span class="hljs-comment">// - Date {}</span>
1116
1117assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>);
1118<span class="hljs-comment">// OK because Object.is(NaN, NaN) is true.</span>
1119
1120<span class="hljs-comment">// Different unwrapped numbers:</span>
1121assert.<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>));
1122<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1123<span class="hljs-comment">// + actual - expected</span>
1124<span class="hljs-comment">//</span>
1125<span class="hljs-comment">// + [Number: 1]</span>
1126<span class="hljs-comment">// - [Number: 2]</span>
1127
1128assert.<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>));
1129<span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</span>
1130
1131assert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>);
1132<span class="hljs-comment">// OK</span>
1133
1134<span class="hljs-comment">// Different zeros:</span>
1135assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">0</span>);
1136<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1137<span class="hljs-comment">// + actual - expected</span>
1138<span class="hljs-comment">//</span>
1139<span class="hljs-comment">// + 0</span>
1140<span class="hljs-comment">// - -0</span>
1141
1142<span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>();
1143<span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>();
1144assert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol1]: <span class="hljs-number">1</span> });
1145<span class="hljs-comment">// OK, because it is the same symbol on both objects.</span>
1146
1147assert.<span class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol2]: <span class="hljs-number">1</span> });
1148<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:</span>
1149<span class="hljs-comment">//</span>
1150<span class="hljs-comment">// {</span>
1151<span class="hljs-comment">//   [Symbol()]: 1</span>
1152<span class="hljs-comment">// }</span>
1153
1154<span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>();
1155<span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>([[{}, {}]]);
1156<span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>();
1157weakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>;
1158
1159assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2);
1160<span class="hljs-comment">// OK, because it is impossible to compare the entries</span>
1161
1162<span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:</span>
1163assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3);
1164<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
1165<span class="hljs-comment">// + actual - expected</span>
1166<span class="hljs-comment">//</span>
1167<span class="hljs-comment">//   WeakMap {</span>
1168<span class="hljs-comment">// +   [items unknown]</span>
1169<span class="hljs-comment">// -   [items unknown],</span>
1170<span class="hljs-comment">// -   unequal: true</span>
1171<span class="hljs-comment">//   }</span></code><button class="copy-button">copy</button></pre>
1172<p>If the values are not equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
1173property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
1174parameter is undefined, a default error message is assigned. If the <code>message</code>
1175parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
1176<code>AssertionError</code>.</p>
1177</section><section><h3><code>assert.doesNotMatch(string, regexp[, message])</code><span><a class="mark" href="#assertdoesnotmatchstring-regexp-message" id="assertdoesnotmatchstring-regexp-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_doesnotmatch_string_regexp_message"></a></h3>
1178<div class="api_metadata">
1179<details class="changelog"><summary>History</summary>
1180<table>
1181<tbody><tr><th>Version</th><th>Changes</th></tr>
1182<tr><td>v16.0.0</td>
1183<td><p>This API is no longer experimental.</p></td></tr>
1184<tr><td>v13.6.0, v12.16.0</td>
1185<td><p><span>Added in: v13.6.0, v12.16.0</span></p></td></tr>
1186</tbody></table>
1187</details>
1188</div>
1189<ul>
1190<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>
1191<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>
1192<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>
1193</ul>
1194<p>Expects the <code>string</code> input not to match the regular expression.</p>
1195
1196<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1197
1198assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/fail/</span>);
1199<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...</span>
1200
1201assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>);
1202<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span>
1203
1204assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/different/</span>);
1205<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1206
1207assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/fail/</span>);
1208<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...</span>
1209
1210assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>);
1211<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span>
1212
1213assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/different/</span>);
1214<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre>
1215<p>If the values do match, or if the <code>string</code> argument is of another type than
1216<code>string</code>, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal
1217to the value of the <code>message</code> parameter. If the <code>message</code> parameter is
1218undefined, a default error message is assigned. If the <code>message</code> parameter is an
1219instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
1220<a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
1221</section><section><h3><code>assert.doesNotReject(asyncFn[, error][, message])</code><span><a class="mark" href="#assertdoesnotrejectasyncfn-error-message" id="assertdoesnotrejectasyncfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_doesnotreject_asyncfn_error_message"></a></h3>
1222<div class="api_metadata">
1223<span>Added in: v10.0.0</span>
1224</div>
1225<ul>
1226<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>
1227<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>
1228<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>
1229</ul>
1230<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately
1231calls the function and awaits the returned promise to complete. It will then
1232check that the promise is not rejected.</p>
1233<p>If <code>asyncFn</code> is a function and it throws an error synchronously,
1234<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If
1235the function does not return a promise, <code>assert.doesNotReject()</code> will return a
1236rejected <code>Promise</code> with an <a href="errors.html#err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases
1237the error handler is skipped.</p>
1238<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little
1239benefit in catching a rejection and then rejecting it again. Instead, consider
1240adding a comment next to the specific code path that should not reject and keep
1241error messages as expressive as possible.</p>
1242<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
1243function. See <a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a> for more details.</p>
1244<p>Besides the async nature to await the completion behaves identically to
1245<a href="#assertdoesnotthrowfn-error-message"><code>assert.doesNotThrow()</code></a>.</p>
1246
1247<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1248
1249<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</span>(
1250  <span class="hljs-keyword">async</span> () => {
1251    <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>);
1252  },
1253  <span class="hljs-title class_">SyntaxError</span>,
1254);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1255
1256(<span class="hljs-keyword">async</span> () => {
1257  <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</span>(
1258    <span class="hljs-keyword">async</span> () => {
1259      <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>);
1260    },
1261    <span class="hljs-title class_">SyntaxError</span>,
1262  );
1263})();</code><button class="copy-button">copy</button></pre>
1264
1265<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1266
1267assert.<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>)))
1268  .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
1269    <span class="hljs-comment">// ...</span>
1270  });</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1271
1272assert.<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>)))
1273  .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
1274    <span class="hljs-comment">// ...</span>
1275  });</code><button class="copy-button">copy</button></pre>
1276</section><section><h3><code>assert.doesNotThrow(fn[, error][, message])</code><span><a class="mark" href="#assertdoesnotthrowfn-error-message" id="assertdoesnotthrowfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_doesnotthrow_fn_error_message"></a></h3>
1277<div class="api_metadata">
1278<details class="changelog"><summary>History</summary>
1279<table>
1280<tbody><tr><th>Version</th><th>Changes</th></tr>
1281<tr><td>v5.11.0, v4.4.5</td>
1282<td><p>The <code>message</code> parameter is respected now.</p></td></tr>
1283<tr><td>v4.2.0</td>
1284<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr>
1285<tr><td>v0.1.21</td>
1286<td><p><span>Added in: v0.1.21</span></p></td></tr>
1287</tbody></table>
1288</details>
1289</div>
1290<ul>
1291<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>
1292<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>
1293<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>
1294</ul>
1295<p>Asserts that the function <code>fn</code> does not throw an error.</p>
1296<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there
1297is no benefit in catching an error and then rethrowing it. Instead, consider
1298adding a comment next to the specific code path that should not throw and keep
1299error messages as expressive as possible.</p>
1300<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code>
1301function.</p>
1302<p>If an error is thrown and it is the same type as that specified by the <code>error</code>
1303parameter, then an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown. If the error is of a
1304different type, or if the <code>error</code> parameter is undefined, the error is
1305propagated back to the caller.</p>
1306<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
1307function. See <a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a> for more details.</p>
1308<p>The following, for instance, will throw the <a href="errors.html#class-typeerror"><code>TypeError</code></a> because there is no
1309matching error type in the assertion:</p>
1310
1311<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1312
1313assert.<span class="hljs-title function_">doesNotThrow</span>(
1314  <span class="hljs-function">() =></span> {
1315    <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>);
1316  },
1317  <span class="hljs-title class_">SyntaxError</span>,
1318);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1319
1320assert.<span class="hljs-title function_">doesNotThrow</span>(
1321  <span class="hljs-function">() =></span> {
1322    <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>);
1323  },
1324  <span class="hljs-title class_">SyntaxError</span>,
1325);</code><button class="copy-button">copy</button></pre>
1326<p>However, the following will result in an <a href="#class-assertassertionerror"><code>AssertionError</code></a> with the message
1327'Got unwanted exception...':</p>
1328
1329<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1330
1331assert.<span class="hljs-title function_">doesNotThrow</span>(
1332  <span class="hljs-function">() =></span> {
1333    <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>);
1334  },
1335  <span class="hljs-title class_">TypeError</span>,
1336);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1337
1338assert.<span class="hljs-title function_">doesNotThrow</span>(
1339  <span class="hljs-function">() =></span> {
1340    <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>);
1341  },
1342  <span class="hljs-title class_">TypeError</span>,
1343);</code><button class="copy-button">copy</button></pre>
1344<p>If an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown and a value is provided for the <code>message</code>
1345parameter, the value of <code>message</code> will be appended to the <a href="#class-assertassertionerror"><code>AssertionError</code></a>
1346message:</p>
1347
1348<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1349
1350assert.<span class="hljs-title function_">doesNotThrow</span>(
1351  <span class="hljs-function">() =></span> {
1352    <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>);
1353  },
1354  <span class="hljs-regexp">/Wrong value/</span>,
1355  <span class="hljs-string">'Whoops'</span>,
1356);
1357<span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1358
1359assert.<span class="hljs-title function_">doesNotThrow</span>(
1360  <span class="hljs-function">() =></span> {
1361    <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>);
1362  },
1363  <span class="hljs-regexp">/Wrong value/</span>,
1364  <span class="hljs-string">'Whoops'</span>,
1365);
1366<span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><button class="copy-button">copy</button></pre>
1367</section><section><h3><code>assert.equal(actual, expected[, message])</code><span><a class="mark" href="#assertequalactual-expected-message" id="assertequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_equal_actual_expected_message"></a></h3>
1368<div class="api_metadata">
1369<details class="changelog"><summary>History</summary>
1370<table>
1371<tbody><tr><th>Version</th><th>Changes</th></tr>
1372<tr><td>v16.0.0, v14.18.0</td>
1373<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr>
1374<tr><td>v14.0.0</td>
1375<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr>
1376<tr><td>v0.1.21</td>
1377<td><p><span>Added in: v0.1.21</span></p></td></tr>
1378</tbody></table>
1379</details>
1380</div>
1381<ul>
1382<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>
1383<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>
1384<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>
1385</ul>
1386<p><strong>Strict assertion mode</strong></p>
1387<p>An alias of <a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a>.</p>
1388<p><strong>Legacy assertion mode</strong></p>
1389<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertstrictequalactual-expected-message"><code>assert.strictEqual()</code></a> instead.</div><p></p>
1390<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters
1391using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><code>==</code> operator</a>. <code>NaN</code> is specially handled
1392and treated as being identical if both sides are <code>NaN</code>.</p>
1393
1394<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
1395
1396assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1397<span class="hljs-comment">// OK, 1 == 1</span>
1398assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
1399<span class="hljs-comment">// OK, 1 == '1'</span>
1400assert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>);
1401<span class="hljs-comment">// OK</span>
1402
1403assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
1404<span class="hljs-comment">// AssertionError: 1 == 2</span>
1405assert.<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> } });
1406<span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
1407
1408assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1409<span class="hljs-comment">// OK, 1 == 1</span>
1410assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
1411<span class="hljs-comment">// OK, 1 == '1'</span>
1412assert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">NaN</span>, <span class="hljs-title class_">NaN</span>);
1413<span class="hljs-comment">// OK</span>
1414
1415assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
1416<span class="hljs-comment">// AssertionError: 1 == 2</span>
1417assert.<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> } });
1418<span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code><button class="copy-button">copy</button></pre>
1419<p>If the values are not equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
1420property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
1421parameter is undefined, a default error message is assigned. If the <code>message</code>
1422parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
1423<code>AssertionError</code>.</p>
1424</section><section><h3><code>assert.fail([message])</code><span><a class="mark" href="#assertfailmessage" id="assertfailmessage">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_fail_message"></a></h3>
1425<div class="api_metadata">
1426<span>Added in: v0.1.21</span>
1427</div>
1428<ul>
1429<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>
1430</ul>
1431<p>Throws an <a href="#class-assertassertionerror"><code>AssertionError</code></a> with the provided error message or a default
1432error message. If the <code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then
1433it will be thrown instead of the <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
1434
1435<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1436
1437assert.<span class="hljs-title function_">fail</span>();
1438<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span>
1439
1440assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>);
1441<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span>
1442
1443assert.<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>));
1444<span class="hljs-comment">// TypeError: need array</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1445
1446assert.<span class="hljs-title function_">fail</span>();
1447<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span>
1448
1449assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>);
1450<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span>
1451
1452assert.<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>));
1453<span class="hljs-comment">// TypeError: need array</span></code><button class="copy-button">copy</button></pre>
1454<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated.
1455See below for further details.</p>
1456</section><section><h3><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code><span><a class="mark" href="#assertfailactual-expected-message-operator-stackstartfn" id="assertfailactual-expected-message-operator-stackstartfn">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_fail_actual_expected_message_operator_stackstartfn"></a></h3>
1457<div class="api_metadata">
1458<details class="changelog"><summary>History</summary>
1459<table>
1460<tbody><tr><th>Version</th><th>Changes</th></tr>
1461<tr><td>v10.0.0</td>
1462<td><p>Calling <code>assert.fail()</code> with more than one argument is deprecated and emits a warning.</p></td></tr>
1463<tr><td>v0.1.21</td>
1464<td><p><span>Added in: v0.1.21</span></p></td></tr>
1465</tbody></table>
1466</details>
1467</div>
1468<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>assert.fail([message])</code> or other assert
1469functions instead.</div><p></p>
1470<ul>
1471<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>
1472<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>
1473<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>
1474<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>
1475<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>
1476</ul>
1477<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and
1478<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and
1479<code>expected</code> arguments are provided, <code>operator</code> will default to <code>'!='</code>. If
1480<code>message</code> is provided as third argument it will be used as the error message and
1481the other arguments will be stored as properties on the thrown object. If
1482<code>stackStartFn</code> is provided, all stack frames above that function will be
1483removed from stacktrace (see <a href="errors.html#errorcapturestacktracetargetobject-constructoropt"><code>Error.captureStackTrace</code></a>). If no arguments are
1484given, the default message <code>Failed</code> will be used.</p>
1485
1486<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1487
1488assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);
1489<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span>
1490
1491assert.<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>);
1492<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span>
1493
1494assert.<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>);
1495<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span>
1496
1497assert.<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>);
1498<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span>
1499
1500assert.<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>));
1501<span class="hljs-comment">// TypeError: need array</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1502
1503assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);
1504<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span>
1505
1506assert.<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>);
1507<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span>
1508
1509assert.<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>);
1510<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span>
1511
1512assert.<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>);
1513<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span>
1514
1515assert.<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>));
1516<span class="hljs-comment">// TypeError: need array</span></code><button class="copy-button">copy</button></pre>
1517<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no
1518influence on the error message.</p>
1519<p>Example use of <code>stackStartFn</code> for truncating the exception's stacktrace:</p>
1520
1521<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1522
1523<span class="hljs-keyword">function</span> <span class="hljs-title function_">suppressFrame</span>(<span class="hljs-params"></span>) {
1524  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);
1525}
1526<span class="hljs-title function_">suppressFrame</span>();
1527<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span>
1528<span class="hljs-comment">//     at repl:1:1</span>
1529<span class="hljs-comment">//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span>
1530<span class="hljs-comment">//     ...</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1531
1532<span class="hljs-keyword">function</span> <span class="hljs-title function_">suppressFrame</span>(<span class="hljs-params"></span>) {
1533  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);
1534}
1535<span class="hljs-title function_">suppressFrame</span>();
1536<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span>
1537<span class="hljs-comment">//     at repl:1:1</span>
1538<span class="hljs-comment">//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span>
1539<span class="hljs-comment">//     ...</span></code><button class="copy-button">copy</button></pre>
1540</section><section><h3><code>assert.ifError(value)</code><span><a class="mark" href="#assertiferrorvalue" id="assertiferrorvalue">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_iferror_value"></a></h3>
1541<div class="api_metadata">
1542<details class="changelog"><summary>History</summary>
1543<table>
1544<tbody><tr><th>Version</th><th>Changes</th></tr>
1545<tr><td>v10.0.0</td>
1546<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>
1547<tr><td>v10.0.0</td>
1548<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>
1549<tr><td>v0.1.97</td>
1550<td><p><span>Added in: v0.1.97</span></p></td></tr>
1551</tbody></table>
1552</details>
1553</div>
1554<ul>
1555<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>
1556</ul>
1557<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when
1558testing the <code>error</code> argument in callbacks. The stack trace contains all frames
1559from the error passed to <code>ifError()</code> including the potential new frames for
1560<code>ifError()</code> itself.</p>
1561
1562<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1563
1564assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>);
1565<span class="hljs-comment">// OK</span>
1566assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>);
1567<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</span>
1568assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>);
1569<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'</span>
1570assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>());
1571<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error</span>
1572
1573<span class="hljs-comment">// Create some random error frames.</span>
1574<span class="hljs-keyword">let</span> err;
1575(<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<span class="hljs-params"></span>) {
1576  err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'test error'</span>);
1577})();
1578
1579(<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(<span class="hljs-params"></span>) {
1580  assert.<span class="hljs-title function_">ifError</span>(err);
1581})();
1582<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error</span>
1583<span class="hljs-comment">//     at ifErrorFrame</span>
1584<span class="hljs-comment">//     at errorFrame</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1585
1586assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>);
1587<span class="hljs-comment">// OK</span>
1588assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>);
1589<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</span>
1590assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>);
1591<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'</span>
1592assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>());
1593<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error</span>
1594
1595<span class="hljs-comment">// Create some random error frames.</span>
1596<span class="hljs-keyword">let</span> err;
1597(<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<span class="hljs-params"></span>) {
1598  err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'test error'</span>);
1599})();
1600
1601(<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(<span class="hljs-params"></span>) {
1602  assert.<span class="hljs-title function_">ifError</span>(err);
1603})();
1604<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error</span>
1605<span class="hljs-comment">//     at ifErrorFrame</span>
1606<span class="hljs-comment">//     at errorFrame</span></code><button class="copy-button">copy</button></pre>
1607</section><section><h3><code>assert.match(string, regexp[, message])</code><span><a class="mark" href="#assertmatchstring-regexp-message" id="assertmatchstring-regexp-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_match_string_regexp_message"></a></h3>
1608<div class="api_metadata">
1609<details class="changelog"><summary>History</summary>
1610<table>
1611<tbody><tr><th>Version</th><th>Changes</th></tr>
1612<tr><td>v16.0.0</td>
1613<td><p>This API is no longer experimental.</p></td></tr>
1614<tr><td>v13.6.0, v12.16.0</td>
1615<td><p><span>Added in: v13.6.0, v12.16.0</span></p></td></tr>
1616</tbody></table>
1617</details>
1618</div>
1619<ul>
1620<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>
1621<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>
1622<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>
1623</ul>
1624<p>Expects the <code>string</code> input to match the regular expression.</p>
1625
1626<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1627
1628assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/pass/</span>);
1629<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular ...</span>
1630
1631assert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>);
1632<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span>
1633
1634assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/pass/</span>);
1635<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1636
1637assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</span>, <span class="hljs-regexp">/pass/</span>);
1638<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular ...</span>
1639
1640assert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span class="hljs-regexp">/pass/</span>);
1641<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.</span>
1642
1643assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</span>, <span class="hljs-regexp">/pass/</span>);
1644<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre>
1645<p>If the values do not match, or if the <code>string</code> argument is of another type than
1646<code>string</code>, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal
1647to the value of the <code>message</code> parameter. If the <code>message</code> parameter is
1648undefined, a default error message is assigned. If the <code>message</code> parameter is an
1649instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
1650<a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
1651</section><section><h3><code>assert.notDeepEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotdeepequalactual-expected-message" id="assertnotdeepequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notdeepequal_actual_expected_message"></a></h3>
1652<div class="api_metadata">
1653<details class="changelog"><summary>History</summary>
1654<table>
1655<tbody><tr><th>Version</th><th>Changes</th></tr>
1656<tr><td>v16.0.0, v14.18.0</td>
1657<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr>
1658<tr><td>v14.0.0</td>
1659<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr>
1660<tr><td>v9.0.0</td>
1661<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr>
1662<tr><td>v8.0.0</td>
1663<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr>
1664<tr><td>v6.4.0, v4.7.1</td>
1665<td><p>Typed array slices are handled correctly now.</p></td></tr>
1666<tr><td>v6.1.0, v4.5.0</td>
1667<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
1668<tr><td>v5.10.1, v4.4.3</td>
1669<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
1670<tr><td>v0.1.21</td>
1671<td><p><span>Added in: v0.1.21</span></p></td></tr>
1672</tbody></table>
1673</details>
1674</div>
1675<ul>
1676<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>
1677<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>
1678<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>
1679</ul>
1680<p><strong>Strict assertion mode</strong></p>
1681<p>An alias of <a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual()</code></a>.</p>
1682<p><strong>Legacy assertion mode</strong></p>
1683<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertnotdeepstrictequalactual-expected-message"><code>assert.notDeepStrictEqual()</code></a> instead.</div><p></p>
1684<p>Tests for any deep inequality. Opposite of <a href="#assertdeepequalactual-expected-message"><code>assert.deepEqual()</code></a>.</p>
1685
1686<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
1687
1688<span class="hljs-keyword">const</span> obj1 = {
1689  <span class="hljs-attr">a</span>: {
1690    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
1691  },
1692};
1693<span class="hljs-keyword">const</span> obj2 = {
1694  <span class="hljs-attr">a</span>: {
1695    <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>,
1696  },
1697};
1698<span class="hljs-keyword">const</span> obj3 = {
1699  <span class="hljs-attr">a</span>: {
1700    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
1701  },
1702};
1703<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1);
1704
1705assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1);
1706<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
1707
1708assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2);
1709<span class="hljs-comment">// OK</span>
1710
1711assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3);
1712<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
1713
1714assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4);
1715<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
1716
1717<span class="hljs-keyword">const</span> obj1 = {
1718  <span class="hljs-attr">a</span>: {
1719    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
1720  },
1721};
1722<span class="hljs-keyword">const</span> obj2 = {
1723  <span class="hljs-attr">a</span>: {
1724    <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>,
1725  },
1726};
1727<span class="hljs-keyword">const</span> obj3 = {
1728  <span class="hljs-attr">a</span>: {
1729    <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>,
1730  },
1731};
1732<span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj1);
1733
1734assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1);
1735<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
1736
1737assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2);
1738<span class="hljs-comment">// OK</span>
1739
1740assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3);
1741<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
1742
1743assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4);
1744<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre>
1745<p>If the values are deeply equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a
1746<code>message</code> property set equal to the value of the <code>message</code> parameter. If the
1747<code>message</code> parameter is undefined, a default error message is assigned. If the
1748<code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown
1749instead of the <code>AssertionError</code>.</p>
1750</section><section><h3><code>assert.notDeepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotdeepstrictequalactual-expected-message" id="assertnotdeepstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notdeepstrictequal_actual_expected_message"></a></h3>
1751<div class="api_metadata">
1752<details class="changelog"><summary>History</summary>
1753<table>
1754<tbody><tr><th>Version</th><th>Changes</th></tr>
1755<tr><td>v9.0.0</td>
1756<td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr>
1757<tr><td>v9.0.0</td>
1758<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>
1759<tr><td>v9.0.0</td>
1760<td><p>The <code>Error</code> names and messages are now properly compared.</p></td></tr>
1761<tr><td>v8.0.0</td>
1762<td><p>The <code>Set</code> and <code>Map</code> content is also compared.</p></td></tr>
1763<tr><td>v6.1.0</td>
1764<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
1765<tr><td>v6.4.0, v4.7.1</td>
1766<td><p>Typed array slices are handled correctly now.</p></td></tr>
1767<tr><td>v5.10.1, v4.4.3</td>
1768<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
1769<tr><td>v1.2.0</td>
1770<td><p><span>Added in: v1.2.0</span></p></td></tr>
1771</tbody></table>
1772</details>
1773</div>
1774<ul>
1775<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>
1776<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>
1777<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>
1778</ul>
1779<p>Tests for deep strict inequality. Opposite of <a href="#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a>.</p>
1780
1781<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1782
1783assert.<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> });
1784<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1785
1786assert.<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> });
1787<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre>
1788<p>If the values are deeply and strictly equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown
1789with a <code>message</code> property set equal to the value of the <code>message</code> parameter. If
1790the <code>message</code> parameter is undefined, a default error message is assigned. If
1791the <code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown
1792instead of the <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
1793</section><section><h3><code>assert.notEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotequalactual-expected-message" id="assertnotequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notequal_actual_expected_message"></a></h3>
1794<div class="api_metadata">
1795<details class="changelog"><summary>History</summary>
1796<table>
1797<tbody><tr><th>Version</th><th>Changes</th></tr>
1798<tr><td>v16.0.0, v14.18.0</td>
1799<td><p>In Legacy assertion mode, changed status from Deprecated to Legacy.</p></td></tr>
1800<tr><td>v14.0.0</td>
1801<td><p>NaN is now treated as being identical if both sides are NaN.</p></td></tr>
1802<tr><td>v0.1.21</td>
1803<td><p><span>Added in: v0.1.21</span></p></td></tr>
1804</tbody></table>
1805</details>
1806</div>
1807<ul>
1808<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>
1809<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>
1810<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>
1811</ul>
1812<p><strong>Strict assertion mode</strong></p>
1813<p>An alias of <a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual()</code></a>.</p>
1814<p><strong>Legacy assertion mode</strong></p>
1815<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#assertnotstrictequalactual-expected-message"><code>assert.notStrictEqual()</code></a> instead.</div><p></p>
1816<p>Tests shallow, coercive inequality with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality"><code>!=</code> operator</a>. <code>NaN</code> is
1817specially handled and treated as being identical if both sides are <code>NaN</code>.</p>
1818
1819<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>;
1820
1821assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
1822<span class="hljs-comment">// OK</span>
1823
1824assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1825<span class="hljs-comment">// AssertionError: 1 != 1</span>
1826
1827assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
1828<span class="hljs-comment">// AssertionError: 1 != '1'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
1829
1830assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
1831<span class="hljs-comment">// OK</span>
1832
1833assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1834<span class="hljs-comment">// AssertionError: 1 != 1</span>
1835
1836assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
1837<span class="hljs-comment">// AssertionError: 1 != '1'</span></code><button class="copy-button">copy</button></pre>
1838<p>If the values are equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
1839property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
1840parameter is undefined, a default error message is assigned. If the <code>message</code>
1841parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
1842<code>AssertionError</code>.</p>
1843</section><section><h3><code>assert.notStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertnotstrictequalactual-expected-message" id="assertnotstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_notstrictequal_actual_expected_message"></a></h3>
1844<div class="api_metadata">
1845<details class="changelog"><summary>History</summary>
1846<table>
1847<tbody><tr><th>Version</th><th>Changes</th></tr>
1848<tr><td>v10.0.0</td>
1849<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code>.</p></td></tr>
1850<tr><td>v0.1.21</td>
1851<td><p><span>Added in: v0.1.21</span></p></td></tr>
1852</tbody></table>
1853</details>
1854</div>
1855<ul>
1856<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>
1857<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>
1858<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>
1859</ul>
1860<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as
1861determined by <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</p>
1862
1863<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1864
1865assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
1866<span class="hljs-comment">// OK</span>
1867
1868assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1869<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:</span>
1870<span class="hljs-comment">//</span>
1871<span class="hljs-comment">// 1</span>
1872
1873assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
1874<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1875
1876assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
1877<span class="hljs-comment">// OK</span>
1878
1879assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1880<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:</span>
1881<span class="hljs-comment">//</span>
1882<span class="hljs-comment">// 1</span>
1883
1884assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
1885<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre>
1886<p>If the values are strictly equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a
1887<code>message</code> property set equal to the value of the <code>message</code> parameter. If the
1888<code>message</code> parameter is undefined, a default error message is assigned. If the
1889<code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown
1890instead of the <code>AssertionError</code>.</p>
1891</section><section><h3><code>assert.ok(value[, message])</code><span><a class="mark" href="#assertokvalue-message" id="assertokvalue-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_ok_value_message"></a></h3>
1892<div class="api_metadata">
1893<details class="changelog"><summary>History</summary>
1894<table>
1895<tbody><tr><th>Version</th><th>Changes</th></tr>
1896<tr><td>v10.0.0</td>
1897<td><p>The <code>assert.ok()</code> (no arguments) will now use a predefined error message.</p></td></tr>
1898<tr><td>v0.1.21</td>
1899<td><p><span>Added in: v0.1.21</span></p></td></tr>
1900</tbody></table>
1901</details>
1902</div>
1903<ul>
1904<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>
1905<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>
1906</ul>
1907<p>Tests if <code>value</code> is truthy. It is equivalent to
1908<code>assert.equal(!!value, true, message)</code>.</p>
1909<p>If <code>value</code> is not truthy, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
1910property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
1911parameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code>
1912parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown instead of the
1913<code>AssertionError</code>.
1914If no arguments are passed in at all <code>message</code> will be set to the string:
1915<code>'No value argument passed to `assert.ok()`'</code>.</p>
1916<p>Be aware that in the <code>repl</code> the error message will be different to the one
1917thrown in a file! See below for further details.</p>
1918
1919<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1920
1921assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>);
1922<span class="hljs-comment">// OK</span>
1923assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>);
1924<span class="hljs-comment">// OK</span>
1925
1926assert.<span class="hljs-title function_">ok</span>();
1927<span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span>
1928
1929assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span class="hljs-string">'it\'s false'</span>);
1930<span class="hljs-comment">// AssertionError: it's false</span>
1931
1932<span class="hljs-comment">// In the repl:</span>
1933assert.<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>);
1934<span class="hljs-comment">// AssertionError: false == true</span>
1935
1936<span class="hljs-comment">// In a file (e.g. test.js):</span>
1937assert.<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>);
1938<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1939<span class="hljs-comment">//</span>
1940<span class="hljs-comment">//   assert.ok(typeof 123 === 'string')</span>
1941
1942assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>);
1943<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1944<span class="hljs-comment">//</span>
1945<span class="hljs-comment">//   assert.ok(false)</span>
1946
1947assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>);
1948<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1949<span class="hljs-comment">//</span>
1950<span class="hljs-comment">//   assert.ok(0)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
1951
1952assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>);
1953<span class="hljs-comment">// OK</span>
1954assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>);
1955<span class="hljs-comment">// OK</span>
1956
1957assert.<span class="hljs-title function_">ok</span>();
1958<span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span>
1959
1960assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span class="hljs-string">'it\'s false'</span>);
1961<span class="hljs-comment">// AssertionError: it's false</span>
1962
1963<span class="hljs-comment">// In the repl:</span>
1964assert.<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>);
1965<span class="hljs-comment">// AssertionError: false == true</span>
1966
1967<span class="hljs-comment">// In a file (e.g. test.js):</span>
1968assert.<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>);
1969<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1970<span class="hljs-comment">//</span>
1971<span class="hljs-comment">//   assert.ok(typeof 123 === 'string')</span>
1972
1973assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>);
1974<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1975<span class="hljs-comment">//</span>
1976<span class="hljs-comment">//   assert.ok(false)</span>
1977
1978assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>);
1979<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1980<span class="hljs-comment">//</span>
1981<span class="hljs-comment">//   assert.ok(0)</span></code><button class="copy-button">copy</button></pre>
1982
1983<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
1984
1985<span class="hljs-comment">// Using `assert()` works the same:</span>
1986<span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>);
1987<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1988<span class="hljs-comment">//</span>
1989<span class="hljs-comment">//   assert(0)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
1990
1991<span class="hljs-comment">// Using `assert()` works the same:</span>
1992<span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>);
1993<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
1994<span class="hljs-comment">//</span>
1995<span class="hljs-comment">//   assert(0)</span></code><button class="copy-button">copy</button></pre>
1996</section><section><h3><code>assert.rejects(asyncFn[, error][, message])</code><span><a class="mark" href="#assertrejectsasyncfn-error-message" id="assertrejectsasyncfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_rejects_asyncfn_error_message"></a></h3>
1997<div class="api_metadata">
1998<span>Added in: v10.0.0</span>
1999</div>
2000<ul>
2001<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>
2002<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>
2003<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>
2004</ul>
2005<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately
2006calls the function and awaits the returned promise to complete. It will then
2007check that the promise is rejected.</p>
2008<p>If <code>asyncFn</code> is a function and it throws an error synchronously,
2009<code>assert.rejects()</code> will return a rejected <code>Promise</code> with that error. If the
2010function does not return a promise, <code>assert.rejects()</code> will return a rejected
2011<code>Promise</code> with an <a href="errors.html#err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases the error
2012handler is skipped.</p>
2013<p>Besides the async nature to await the completion behaves identically to
2014<a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a>.</p>
2015<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,
2016an object where each property will be tested for, or an instance of error where
2017each property will be tested for including the non-enumerable <code>message</code> and
2018<code>name</code> properties.</p>
2019<p>If specified, <code>message</code> will be the message provided by the <a href="#class-assertassertionerror"><code>AssertionError</code></a>
2020if the <code>asyncFn</code> fails to reject.</p>
2021
2022<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2023
2024<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
2025  <span class="hljs-keyword">async</span> () => {
2026    <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>);
2027  },
2028  {
2029    <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
2030    <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
2031  },
2032);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2033
2034(<span class="hljs-keyword">async</span> () => {
2035  <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
2036    <span class="hljs-keyword">async</span> () => {
2037      <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>);
2038    },
2039    {
2040      <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
2041      <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
2042    },
2043  );
2044})();</code><button class="copy-button">copy</button></pre>
2045
2046<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2047
2048<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
2049  <span class="hljs-keyword">async</span> () => {
2050    <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>);
2051  },
2052  <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
2053    assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'TypeError'</span>);
2054    assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, <span class="hljs-string">'Wrong value'</span>);
2055    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
2056  },
2057);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2058
2059(<span class="hljs-keyword">async</span> () => {
2060  <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
2061    <span class="hljs-keyword">async</span> () => {
2062      <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>);
2063    },
2064    <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
2065      assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name</span>, <span class="hljs-string">'TypeError'</span>);
2066      assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">message</span>, <span class="hljs-string">'Wrong value'</span>);
2067      <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
2068    },
2069  );
2070})();</code><button class="copy-button">copy</button></pre>
2071
2072<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2073
2074assert.<span class="hljs-title function_">rejects</span>(
2075  <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>)),
2076  <span class="hljs-title class_">Error</span>,
2077).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
2078  <span class="hljs-comment">// ...</span>
2079});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2080
2081assert.<span class="hljs-title function_">rejects</span>(
2082  <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>)),
2083  <span class="hljs-title class_">Error</span>,
2084).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
2085  <span class="hljs-comment">// ...</span>
2086});</code><button class="copy-button">copy</button></pre>
2087<p><code>error</code> cannot be a string. If a string is provided as the second
2088argument, then <code>error</code> is assumed to be omitted and the string will be used for
2089<code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the
2090example in <a href="#assertthrowsfn-error-message"><code>assert.throws()</code></a> carefully if using a string as the second
2091argument gets considered.</p>
2092</section><section><h3><code>assert.strictEqual(actual, expected[, message])</code><span><a class="mark" href="#assertstrictequalactual-expected-message" id="assertstrictequalactual-expected-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_strictequal_actual_expected_message"></a></h3>
2093<div class="api_metadata">
2094<details class="changelog"><summary>History</summary>
2095<table>
2096<tbody><tr><th>Version</th><th>Changes</th></tr>
2097<tr><td>v10.0.0</td>
2098<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code>.</p></td></tr>
2099<tr><td>v0.1.21</td>
2100<td><p><span>Added in: v0.1.21</span></p></td></tr>
2101</tbody></table>
2102</details>
2103</div>
2104<ul>
2105<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>
2106<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>
2107<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>
2108</ul>
2109<p>Tests strict equality between the <code>actual</code> and <code>expected</code> parameters as
2110determined by <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</p>
2111
2112<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2113
2114assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
2115<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span>
2116<span class="hljs-comment">//</span>
2117<span class="hljs-comment">// 1 !== 2</span>
2118
2119assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
2120<span class="hljs-comment">// OK</span>
2121
2122assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello foobar'</span>, <span class="hljs-string">'Hello World!'</span>);
2123<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span>
2124<span class="hljs-comment">// + actual - expected</span>
2125<span class="hljs-comment">//</span>
2126<span class="hljs-comment">// + 'Hello foobar'</span>
2127<span class="hljs-comment">// - 'Hello World!'</span>
2128<span class="hljs-comment">//          ^</span>
2129
2130<span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>;
2131<span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>;
2132assert.<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>);
2133<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span>
2134
2135assert.<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>));
2136<span class="hljs-comment">// TypeError: Inputs are not identical</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2137
2138assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
2139<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span>
2140<span class="hljs-comment">//</span>
2141<span class="hljs-comment">// 1 !== 2</span>
2142
2143assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
2144<span class="hljs-comment">// OK</span>
2145
2146assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello foobar'</span>, <span class="hljs-string">'Hello World!'</span>);
2147<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:</span>
2148<span class="hljs-comment">// + actual - expected</span>
2149<span class="hljs-comment">//</span>
2150<span class="hljs-comment">// + 'Hello foobar'</span>
2151<span class="hljs-comment">// - 'Hello World!'</span>
2152<span class="hljs-comment">//          ^</span>
2153
2154<span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>;
2155<span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>;
2156assert.<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>);
2157<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span>
2158
2159assert.<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>));
2160<span class="hljs-comment">// TypeError: Inputs are not identical</span></code><button class="copy-button">copy</button></pre>
2161<p>If the values are not strictly equal, an <a href="#class-assertassertionerror"><code>AssertionError</code></a> is thrown with a
2162<code>message</code> property set equal to the value of the <code>message</code> parameter. If the
2163<code>message</code> parameter is undefined, a default error message is assigned. If the
2164<code>message</code> parameter is an instance of an <a href="errors.html#class-error"><code>Error</code></a> then it will be thrown
2165instead of the <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
2166</section><section><h3><code>assert.throws(fn[, error][, message])</code><span><a class="mark" href="#assertthrowsfn-error-message" id="assertthrowsfn-error-message">#</a></span><a aria-hidden="true" class="legacy" id="assert_assert_throws_fn_error_message"></a></h3>
2167<div class="api_metadata">
2168<details class="changelog"><summary>History</summary>
2169<table>
2170<tbody><tr><th>Version</th><th>Changes</th></tr>
2171<tr><td>v10.2.0</td>
2172<td><p>The <code>error</code> parameter can be an object containing regular expressions now.</p></td></tr>
2173<tr><td>v9.9.0</td>
2174<td><p>The <code>error</code> parameter can now be an object as well.</p></td></tr>
2175<tr><td>v4.2.0</td>
2176<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr>
2177<tr><td>v0.1.21</td>
2178<td><p><span>Added in: v0.1.21</span></p></td></tr>
2179</tbody></table>
2180</details>
2181</div>
2182<ul>
2183<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>
2184<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>
2185<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>
2186</ul>
2187<p>Expects the function <code>fn</code> to throw an error.</p>
2188<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,
2189a validation object where each property will be tested for strict deep equality,
2190or an instance of error where each property will be tested for strict deep
2191equality including the non-enumerable <code>message</code> and <code>name</code> properties. When
2192using an object, it is also possible to use a regular expression, when
2193validating against a string property. See below for examples.</p>
2194<p>If specified, <code>message</code> will be appended to the message provided by the
2195<code>AssertionError</code> if the <code>fn</code> call fails to throw or in case the error validation
2196fails.</p>
2197<p>Custom validation object/error instance:</p>
2198
2199<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2200
2201<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>);
2202err.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>;
2203err.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>;
2204err.<span class="hljs-property">info</span> = {
2205  <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
2206  <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>,
2207};
2208err.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>;
2209
2210assert.<span class="hljs-title function_">throws</span>(
2211  <span class="hljs-function">() =></span> {
2212    <span class="hljs-keyword">throw</span> err;
2213  },
2214  {
2215    <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
2216    <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
2217    <span class="hljs-attr">info</span>: {
2218      <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
2219      <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>,
2220    },
2221    <span class="hljs-comment">// Only properties on the validation object will be tested for.</span>
2222    <span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwise</span>
2223    <span class="hljs-comment">// the validation is going to fail.</span>
2224  },
2225);
2226
2227<span class="hljs-comment">// Using regular expressions to validate error properties:</span>
2228assert.<span class="hljs-title function_">throws</span>(
2229  <span class="hljs-function">() =></span> {
2230    <span class="hljs-keyword">throw</span> err;
2231  },
2232  {
2233    <span class="hljs-comment">// The `name` and `message` properties are strings and using regular</span>
2234    <span class="hljs-comment">// expressions on those will match against the string. If they fail, an</span>
2235    <span class="hljs-comment">// error is thrown.</span>
2236    <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>,
2237    <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>,
2238    <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
2239    <span class="hljs-attr">info</span>: {
2240      <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
2241      <span class="hljs-comment">// It is not possible to use regular expressions for nested properties!</span>
2242      <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>,
2243    },
2244    <span class="hljs-comment">// The `reg` property contains a regular expression and only if the</span>
2245    <span class="hljs-comment">// validation object contains an identical regular expression, it is going</span>
2246    <span class="hljs-comment">// to pass.</span>
2247    <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>,
2248  },
2249);
2250
2251<span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span>
2252assert.<span class="hljs-title function_">throws</span>(
2253  <span class="hljs-function">() =></span> {
2254    <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>);
2255    <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span>
2256    <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)) {
2257      otherErr[key] = value;
2258    }
2259    <span class="hljs-keyword">throw</span> otherErr;
2260  },
2261  <span class="hljs-comment">// The error's `message` and `name` properties will also be checked when using</span>
2262  <span class="hljs-comment">// an error as validation object.</span>
2263  err,
2264);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2265
2266<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>);
2267err.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>;
2268err.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>;
2269err.<span class="hljs-property">info</span> = {
2270  <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
2271  <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>,
2272};
2273err.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>;
2274
2275assert.<span class="hljs-title function_">throws</span>(
2276  <span class="hljs-function">() =></span> {
2277    <span class="hljs-keyword">throw</span> err;
2278  },
2279  {
2280    <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
2281    <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
2282    <span class="hljs-attr">info</span>: {
2283      <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
2284      <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>,
2285    },
2286    <span class="hljs-comment">// Only properties on the validation object will be tested for.</span>
2287    <span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwise</span>
2288    <span class="hljs-comment">// the validation is going to fail.</span>
2289  },
2290);
2291
2292<span class="hljs-comment">// Using regular expressions to validate error properties:</span>
2293assert.<span class="hljs-title function_">throws</span>(
2294  <span class="hljs-function">() =></span> {
2295    <span class="hljs-keyword">throw</span> err;
2296  },
2297  {
2298    <span class="hljs-comment">// The `name` and `message` properties are strings and using regular</span>
2299    <span class="hljs-comment">// expressions on those will match against the string. If they fail, an</span>
2300    <span class="hljs-comment">// error is thrown.</span>
2301    <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>,
2302    <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>,
2303    <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
2304    <span class="hljs-attr">info</span>: {
2305      <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
2306      <span class="hljs-comment">// It is not possible to use regular expressions for nested properties!</span>
2307      <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>,
2308    },
2309    <span class="hljs-comment">// The `reg` property contains a regular expression and only if the</span>
2310    <span class="hljs-comment">// validation object contains an identical regular expression, it is going</span>
2311    <span class="hljs-comment">// to pass.</span>
2312    <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>,
2313  },
2314);
2315
2316<span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span>
2317assert.<span class="hljs-title function_">throws</span>(
2318  <span class="hljs-function">() =></span> {
2319    <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>);
2320    <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span>
2321    <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)) {
2322      otherErr[key] = value;
2323    }
2324    <span class="hljs-keyword">throw</span> otherErr;
2325  },
2326  <span class="hljs-comment">// The error's `message` and `name` properties will also be checked when using</span>
2327  <span class="hljs-comment">// an error as validation object.</span>
2328  err,
2329);</code><button class="copy-button">copy</button></pre>
2330<p>Validate instanceof using constructor:</p>
2331
2332<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2333
2334assert.<span class="hljs-title function_">throws</span>(
2335  <span class="hljs-function">() =></span> {
2336    <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>);
2337  },
2338  <span class="hljs-title class_">Error</span>,
2339);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2340
2341assert.<span class="hljs-title function_">throws</span>(
2342  <span class="hljs-function">() =></span> {
2343    <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>);
2344  },
2345  <span class="hljs-title class_">Error</span>,
2346);</code><button class="copy-button">copy</button></pre>
2347<p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>:</p>
2348<p>Using a regular expression runs <code>.toString</code> on the error object, and will
2349therefore also include the error name.</p>
2350
2351<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2352
2353assert.<span class="hljs-title function_">throws</span>(
2354  <span class="hljs-function">() =></span> {
2355    <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>);
2356  },
2357  <span class="hljs-regexp">/^Error: Wrong value$/</span>,
2358);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2359
2360assert.<span class="hljs-title function_">throws</span>(
2361  <span class="hljs-function">() =></span> {
2362    <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>);
2363  },
2364  <span class="hljs-regexp">/^Error: Wrong value$/</span>,
2365);</code><button class="copy-button">copy</button></pre>
2366<p>Custom error validation:</p>
2367<p>The function must return <code>true</code> to indicate all internal validations passed.
2368It will otherwise fail with an <a href="#class-assertassertionerror"><code>AssertionError</code></a>.</p>
2369
2370<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2371
2372assert.<span class="hljs-title function_">throws</span>(
2373  <span class="hljs-function">() =></span> {
2374    <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>);
2375  },
2376  <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
2377    <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>);
2378    <span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span class="hljs-title function_">test</span>(err));
2379    <span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</span>
2380    <span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instead,</span>
2381    <span class="hljs-comment">// throw an error about the specific validation that failed (as done in this</span>
2382    <span class="hljs-comment">// example) and add as much helpful debugging information to that error as</span>
2383    <span class="hljs-comment">// possible.</span>
2384    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
2385  },
2386  <span class="hljs-string">'unexpected error'</span>,
2387);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2388
2389assert.<span class="hljs-title function_">throws</span>(
2390  <span class="hljs-function">() =></span> {
2391    <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>);
2392  },
2393  <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
2394    <span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>);
2395    <span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span class="hljs-title function_">test</span>(err));
2396    <span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</span>
2397    <span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instead,</span>
2398    <span class="hljs-comment">// throw an error about the specific validation that failed (as done in this</span>
2399    <span class="hljs-comment">// example) and add as much helpful debugging information to that error as</span>
2400    <span class="hljs-comment">// possible.</span>
2401    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
2402  },
2403  <span class="hljs-string">'unexpected error'</span>,
2404);</code><button class="copy-button">copy</button></pre>
2405<p><code>error</code> cannot be a string. If a string is provided as the second
2406argument, then <code>error</code> is assumed to be omitted and the string will be used for
2407<code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same
2408message as the thrown error message is going to result in an
2409<code>ERR_AMBIGUOUS_ARGUMENT</code> error. Please read the example below carefully if using
2410a string as the second argument gets considered:</p>
2411
2412<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert/strict'</span>;
2413
2414<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingFirst</span>(<span class="hljs-params"></span>) {
2415  <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>);
2416}
2417
2418<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>(<span class="hljs-params"></span>) {
2419  <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>);
2420}
2421
2422<span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<span class="hljs-params"></span>) {}
2423
2424<span class="hljs-comment">// The second argument is a string and the input function threw an Error.</span>
2425<span class="hljs-comment">// The first case will not throw as it does not match for the error message</span>
2426<span class="hljs-comment">// thrown by the input function!</span>
2427assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'Second'</span>);
2428<span class="hljs-comment">// In the next example the message has no benefit over the message from the</span>
2429<span class="hljs-comment">// error and since it is not clear if the user intended to actually match</span>
2430<span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.</span>
2431assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'Second'</span>);
2432<span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span>
2433
2434<span class="hljs-comment">// The string is only used (as message) in case the function does not throw:</span>
2435assert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Second'</span>);
2436<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</span>
2437
2438<span class="hljs-comment">// If it was intended to match for the error message do this instead:</span>
2439<span class="hljs-comment">// It does not throw because the error messages match.</span>
2440assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/Second$/</span>);
2441
2442<span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</span>
2443assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/Second$/</span>);
2444<span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert/strict'</span>);
2445
2446<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingFirst</span>(<span class="hljs-params"></span>) {
2447  <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>);
2448}
2449
2450<span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>(<span class="hljs-params"></span>) {
2451  <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>);
2452}
2453
2454<span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<span class="hljs-params"></span>) {}
2455
2456<span class="hljs-comment">// The second argument is a string and the input function threw an Error.</span>
2457<span class="hljs-comment">// The first case will not throw as it does not match for the error message</span>
2458<span class="hljs-comment">// thrown by the input function!</span>
2459assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'Second'</span>);
2460<span class="hljs-comment">// In the next example the message has no benefit over the message from the</span>
2461<span class="hljs-comment">// error and since it is not clear if the user intended to actually match</span>
2462<span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.</span>
2463assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'Second'</span>);
2464<span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span>
2465
2466<span class="hljs-comment">// The string is only used (as message) in case the function does not throw:</span>
2467assert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Second'</span>);
2468<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</span>
2469
2470<span class="hljs-comment">// If it was intended to match for the error message do this instead:</span>
2471<span class="hljs-comment">// It does not throw because the error messages match.</span>
2472assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/Second$/</span>);
2473
2474<span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</span>
2475assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/Second$/</span>);
2476<span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code><button class="copy-button">copy</button></pre>
2477<p>Due to the confusing error-prone notation, avoid a string as the second
2478argument.</p></section>
2479        <!-- API END -->
2480      </div>
2481    </div>
2482  </div>
2483</body>
2484</html>
2485