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