1{ 2 "type": "module", 3 "source": "doc/api/assert.md", 4 "modules": [ 5 { 6 "textRaw": "Assert", 7 "name": "assert", 8 "introduced_in": "v0.1.21", 9 "stability": 2, 10 "stabilityText": "Stable", 11 "desc": "<p><strong>Source Code:</strong> <a href=\"https://github.com/nodejs/node/blob/v18.20.1/lib/assert.js\">lib/assert.js</a></p>\n<p>The <code>node:assert</code> module provides a set of assertion functions for verifying\ninvariants.</p>", 12 "modules": [ 13 { 14 "textRaw": "Strict assertion mode", 15 "name": "strict_assertion_mode", 16 "meta": { 17 "added": [ 18 "v9.9.0" 19 ], 20 "changes": [ 21 { 22 "version": "v15.0.0", 23 "pr-url": "https://github.com/nodejs/node/pull/34001", 24 "description": "Exposed as `require('node:assert/strict')`." 25 }, 26 { 27 "version": [ 28 "v13.9.0", 29 "v12.16.2" 30 ], 31 "pr-url": "https://github.com/nodejs/node/pull/31635", 32 "description": "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\"." 33 }, 34 { 35 "version": "v9.9.0", 36 "pr-url": "https://github.com/nodejs/node/pull/17615", 37 "description": "Added error diffs to the strict assertion mode." 38 }, 39 { 40 "version": "v9.9.0", 41 "pr-url": "https://github.com/nodejs/node/pull/17002", 42 "description": "Added strict assertion mode to the assert module." 43 } 44 ] 45 }, 46 "desc": "<p>In strict assertion mode, non-strict methods behave like their corresponding\nstrict methods. For example, <a href=\"#assertdeepequalactual-expected-message\"><code>assert.deepEqual()</code></a> will behave like\n<a href=\"#assertdeepstrictequalactual-expected-message\"><code>assert.deepStrictEqual()</code></a>.</p>\n<p>In strict assertion mode, error messages for objects display a diff. In legacy\nassertion mode, error messages for objects display the objects, often truncated.</p>\n<p>To use strict assertion mode:</p>\n<pre><code class=\"language-mjs\">import { strict as assert } from 'node:assert';\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert').strict;\n</code></pre>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n</code></pre>\n<p>Example error diff:</p>\n<pre><code class=\"language-mjs\">import { strict as assert } from 'node:assert';\n\nassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected ... Lines skipped\n//\n// [\n// [\n// ...\n// 2,\n// + 3\n// - '3'\n// ],\n// ...\n// 5\n// ]\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected ... Lines skipped\n//\n// [\n// [\n// ...\n// 2,\n// + 3\n// - '3'\n// ],\n// ...\n// 5\n// ]\n</code></pre>\n<p>To deactivate the colors, use the <code>NO_COLOR</code> or <code>NODE_DISABLE_COLORS</code>\nenvironment variables. This will also deactivate the colors in the REPL. For\nmore on color support in terminal environments, read the tty\n<a href=\"tty.html#writestreamgetcolordepthenv\"><code>getColorDepth()</code></a> documentation.</p>", 47 "type": "module", 48 "displayName": "Strict assertion mode" 49 }, 50 { 51 "textRaw": "Legacy assertion mode", 52 "name": "legacy_assertion_mode", 53 "desc": "<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>\n<ul>\n<li><a href=\"#assertdeepequalactual-expected-message\"><code>assert.deepEqual()</code></a></li>\n<li><a href=\"#assertequalactual-expected-message\"><code>assert.equal()</code></a></li>\n<li><a href=\"#assertnotdeepequalactual-expected-message\"><code>assert.notDeepEqual()</code></a></li>\n<li><a href=\"#assertnotequalactual-expected-message\"><code>assert.notEqual()</code></a></li>\n</ul>\n<p>To use legacy assertion mode:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n</code></pre>\n<p>Legacy assertion mode may have surprising results, especially when using\n<a href=\"#assertdeepequalactual-expected-message\"><code>assert.deepEqual()</code></a>:</p>\n<pre><code class=\"language-cjs\">// WARNING: This does not throw an AssertionError in legacy assertion mode!\nassert.deepEqual(/a/gi, new Date());\n</code></pre>", 54 "type": "module", 55 "displayName": "Legacy assertion mode" 56 } 57 ], 58 "classes": [ 59 { 60 "textRaw": "Class: assert.AssertionError", 61 "type": "class", 62 "name": "assert.AssertionError", 63 "desc": "<ul>\n<li>Extends: <a href=\"errors.html#class-error\" class=\"type\"><errors.Error></a></li>\n</ul>\n<p>Indicates the failure of an assertion. All errors thrown by the <code>node:assert</code>\nmodule will be instances of the <code>AssertionError</code> class.</p>", 64 "signatures": [ 65 { 66 "params": [ 67 { 68 "textRaw": "`options` {Object}", 69 "name": "options", 70 "type": "Object", 71 "options": [ 72 { 73 "textRaw": "`message` {string} If provided, the error message is set to this value.", 74 "name": "message", 75 "type": "string", 76 "desc": "If provided, the error message is set to this value." 77 }, 78 { 79 "textRaw": "`actual` {any} The `actual` property on the error instance.", 80 "name": "actual", 81 "type": "any", 82 "desc": "The `actual` property on the error instance." 83 }, 84 { 85 "textRaw": "`expected` {any} The `expected` property on the error instance.", 86 "name": "expected", 87 "type": "any", 88 "desc": "The `expected` property on the error instance." 89 }, 90 { 91 "textRaw": "`operator` {string} The `operator` property on the error instance.", 92 "name": "operator", 93 "type": "string", 94 "desc": "The `operator` property on the error instance." 95 }, 96 { 97 "textRaw": "`stackStartFn` {Function} If provided, the generated stack trace omits frames before this function.", 98 "name": "stackStartFn", 99 "type": "Function", 100 "desc": "If provided, the generated stack trace omits frames before this function." 101 } 102 ] 103 } 104 ], 105 "desc": "<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p>\n<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>)\nand:</p>\n<ul>\n<li><code>actual</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\"><any></a> Set to the <code>actual</code> argument for methods such as\n<a href=\"#assertstrictequalactual-expected-message\"><code>assert.strictEqual()</code></a>.</li>\n<li><code>expected</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\"><any></a> Set to the <code>expected</code> value for methods such as\n<a href=\"#assertstrictequalactual-expected-message\"><code>assert.strictEqual()</code></a>.</li>\n<li><code>generatedMessage</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\"><boolean></a> Indicates if the message was auto-generated\n(<code>true</code>) or not.</li>\n<li><code>code</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Value is always <code>ERR_ASSERTION</code> to show that the error is an\nassertion error.</li>\n<li><code>operator</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Set to the passed in operator value.</li>\n</ul>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\n// Generate an AssertionError to compare the error message later:\nconst { message } = new assert.AssertionError({\n actual: 1,\n expected: 2,\n operator: 'strictEqual',\n});\n\n// Verify error output:\ntry {\n assert.strictEqual(1, 2);\n} catch (err) {\n assert(err instanceof assert.AssertionError);\n assert.strictEqual(err.message, message);\n assert.strictEqual(err.name, 'AssertionError');\n assert.strictEqual(err.actual, 1);\n assert.strictEqual(err.expected, 2);\n assert.strictEqual(err.code, 'ERR_ASSERTION');\n assert.strictEqual(err.operator, 'strictEqual');\n assert.strictEqual(err.generatedMessage, true);\n}\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\n// Generate an AssertionError to compare the error message later:\nconst { message } = new assert.AssertionError({\n actual: 1,\n expected: 2,\n operator: 'strictEqual',\n});\n\n// Verify error output:\ntry {\n assert.strictEqual(1, 2);\n} catch (err) {\n assert(err instanceof assert.AssertionError);\n assert.strictEqual(err.message, message);\n assert.strictEqual(err.name, 'AssertionError');\n assert.strictEqual(err.actual, 1);\n assert.strictEqual(err.expected, 2);\n assert.strictEqual(err.code, 'ERR_ASSERTION');\n assert.strictEqual(err.operator, 'strictEqual');\n assert.strictEqual(err.generatedMessage, true);\n}\n</code></pre>" 106 } 107 ] 108 }, 109 { 110 "textRaw": "Class: `assert.CallTracker`", 111 "type": "class", 112 "name": "assert.CallTracker", 113 "meta": { 114 "added": [ 115 "v14.2.0", 116 "v12.19.0" 117 ], 118 "changes": [] 119 }, 120 "stability": 1, 121 "stabilityText": "Experimental", 122 "desc": "<p>This feature is currently experimental and behavior might still change.</p>", 123 "methods": [ 124 { 125 "textRaw": "`tracker.calls([fn][, exact])`", 126 "type": "method", 127 "name": "calls", 128 "meta": { 129 "added": [ 130 "v14.2.0", 131 "v12.19.0" 132 ], 133 "changes": [] 134 }, 135 "signatures": [ 136 { 137 "return": { 138 "textRaw": "Returns: {Function} that wraps `fn`.", 139 "name": "return", 140 "type": "Function", 141 "desc": "that wraps `fn`." 142 }, 143 "params": [ 144 { 145 "textRaw": "`fn` {Function} **Default:** A no-op function.", 146 "name": "fn", 147 "type": "Function", 148 "default": "A no-op function" 149 }, 150 { 151 "textRaw": "`exact` {number} **Default:** `1`.", 152 "name": "exact", 153 "type": "number", 154 "default": "`1`" 155 } 156 ] 157 } 158 ], 159 "desc": "<p>The wrapper function is expected to be called exactly <code>exact</code> times. If the\nfunction has not been called exactly <code>exact</code> times when\n<a href=\"#trackerverify\"><code>tracker.verify()</code></a> is called, then <a href=\"#trackerverify\"><code>tracker.verify()</code></a> will throw an\nerror.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// Returns a function that wraps func() that must be called exact times\n// before tracker.verify().\nconst callsfunc = tracker.calls(func);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// Returns a function that wraps func() that must be called exact times\n// before tracker.verify().\nconst callsfunc = tracker.calls(func);\n</code></pre>" 160 }, 161 { 162 "textRaw": "`tracker.getCalls(fn)`", 163 "type": "method", 164 "name": "getCalls", 165 "meta": { 166 "added": [ 167 "v18.8.0" 168 ], 169 "changes": [] 170 }, 171 "signatures": [ 172 { 173 "return": { 174 "textRaw": "Returns: {Array} with all the calls to a tracked function.", 175 "name": "return", 176 "type": "Array", 177 "desc": "with all the calls to a tracked function." 178 }, 179 "params": [ 180 { 181 "textRaw": "`fn` {Function}.", 182 "name": "fn", 183 "type": "Function", 184 "desc": "." 185 }, 186 { 187 "textRaw": "Object {Object}", 188 "name": "Object", 189 "type": "Object", 190 "options": [ 191 { 192 "textRaw": "`thisArg` {Object}", 193 "name": "thisArg", 194 "type": "Object" 195 }, 196 { 197 "textRaw": "`arguments` {Array} the arguments passed to the tracked function", 198 "name": "arguments", 199 "type": "Array", 200 "desc": "the arguments passed to the tracked function" 201 } 202 ] 203 } 204 ] 205 } 206 ], 207 "desc": "<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\nconst callsfunc = tracker.calls(func);\ncallsfunc(1, 2, 3);\n\nassert.deepStrictEqual(tracker.getCalls(callsfunc),\n [{ thisArg: undefined, arguments: [1, 2, 3] }]);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\nconst callsfunc = tracker.calls(func);\ncallsfunc(1, 2, 3);\n\nassert.deepStrictEqual(tracker.getCalls(callsfunc),\n [{ thisArg: undefined, arguments: [1, 2, 3] }]);\n</code></pre>" 208 }, 209 { 210 "textRaw": "`tracker.report()`", 211 "type": "method", 212 "name": "report", 213 "meta": { 214 "added": [ 215 "v14.2.0", 216 "v12.19.0" 217 ], 218 "changes": [] 219 }, 220 "signatures": [ 221 { 222 "return": { 223 "textRaw": "Returns: {Array} of objects containing information about the wrapper functions returned by [`tracker.calls()`][].", 224 "name": "return", 225 "type": "Array", 226 "desc": "of objects containing information about the wrapper functions returned by [`tracker.calls()`][]." 227 }, 228 "params": [ 229 { 230 "textRaw": "Object {Object}", 231 "name": "Object", 232 "type": "Object", 233 "options": [ 234 { 235 "textRaw": "`message` {string}", 236 "name": "message", 237 "type": "string" 238 }, 239 { 240 "textRaw": "`actual` {number} The actual number of times the function was called.", 241 "name": "actual", 242 "type": "number", 243 "desc": "The actual number of times the function was called." 244 }, 245 { 246 "textRaw": "`expected` {number} The number of times the function was expected to be called.", 247 "name": "expected", 248 "type": "number", 249 "desc": "The number of times the function was expected to be called." 250 }, 251 { 252 "textRaw": "`operator` {string} The name of the function that is wrapped.", 253 "name": "operator", 254 "type": "string", 255 "desc": "The name of the function that is wrapped." 256 }, 257 { 258 "textRaw": "`stack` {Object} A stack trace of the function.", 259 "name": "stack", 260 "type": "Object", 261 "desc": "A stack trace of the function." 262 } 263 ] 264 } 265 ] 266 } 267 ], 268 "desc": "<p>The arrays contains information about the expected and actual number of calls of\nthe functions that have not been called the expected number of times.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// Returns a function that wraps func() that must be called exact times\n// before tracker.verify().\nconst callsfunc = tracker.calls(func, 2);\n\n// Returns an array containing information on callsfunc()\nconsole.log(tracker.report());\n// [\n// {\n// message: 'Expected the func function to be executed 2 time(s) but was\n// executed 0 time(s).',\n// actual: 0,\n// expected: 2,\n// operator: 'func',\n// stack: stack trace\n// }\n// ]\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// Returns a function that wraps func() that must be called exact times\n// before tracker.verify().\nconst callsfunc = tracker.calls(func, 2);\n\n// Returns an array containing information on callsfunc()\nconsole.log(tracker.report());\n// [\n// {\n// message: 'Expected the func function to be executed 2 time(s) but was\n// executed 0 time(s).',\n// actual: 0,\n// expected: 2,\n// operator: 'func',\n// stack: stack trace\n// }\n// ]\n</code></pre>" 269 }, 270 { 271 "textRaw": "`tracker.reset([fn])`", 272 "type": "method", 273 "name": "reset", 274 "meta": { 275 "added": [ 276 "v18.8.0" 277 ], 278 "changes": [] 279 }, 280 "signatures": [ 281 { 282 "params": [ 283 { 284 "textRaw": "`fn` {Function} a tracked function to reset.", 285 "name": "fn", 286 "type": "Function", 287 "desc": "a tracked function to reset." 288 } 289 ] 290 } 291 ], 292 "desc": "<p>Reset calls of the call tracker.\nIf a tracked function is passed as an argument, the calls will be reset for it.\nIf no arguments are passed, all tracked functions will be reset</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\nconst callsfunc = tracker.calls(func);\n\ncallsfunc();\n// Tracker was called once\nassert.strictEqual(tracker.getCalls(callsfunc).length, 1);\n\ntracker.reset(callsfunc);\nassert.strictEqual(tracker.getCalls(callsfunc).length, 0);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\nconst callsfunc = tracker.calls(func);\n\ncallsfunc();\n// Tracker was called once\nassert.strictEqual(tracker.getCalls(callsfunc).length, 1);\n\ntracker.reset(callsfunc);\nassert.strictEqual(tracker.getCalls(callsfunc).length, 0);\n</code></pre>" 293 }, 294 { 295 "textRaw": "`tracker.verify()`", 296 "type": "method", 297 "name": "verify", 298 "meta": { 299 "added": [ 300 "v14.2.0", 301 "v12.19.0" 302 ], 303 "changes": [] 304 }, 305 "signatures": [ 306 { 307 "params": [] 308 } 309 ], 310 "desc": "<p>Iterates through the list of functions passed to\n<a href=\"#trackercallsfn-exact\"><code>tracker.calls()</code></a> and will throw an error for functions that\nhave not been called the expected number of times.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// Returns a function that wraps func() that must be called exact times\n// before tracker.verify().\nconst callsfunc = tracker.calls(func, 2);\n\ncallsfunc();\n\n// Will throw an error since callsfunc() was only called once.\ntracker.verify();\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\n// Creates call tracker.\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// Returns a function that wraps func() that must be called exact times\n// before tracker.verify().\nconst callsfunc = tracker.calls(func, 2);\n\ncallsfunc();\n\n// Will throw an error since callsfunc() was only called once.\ntracker.verify();\n</code></pre>" 311 } 312 ], 313 "signatures": [ 314 { 315 "params": [], 316 "desc": "<p>Creates a new <a href=\"#class-assertcalltracker\"><code>CallTracker</code></a> object which can be used to track if functions\nwere called a specific number of times. The <code>tracker.verify()</code> must be called\nfor the verification to take place. The usual pattern would be to call it in a\n<a href=\"process.html#event-exit\"><code>process.on('exit')</code></a> handler.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\nimport process from 'node:process';\n\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// callsfunc() must be called exactly 1 time before tracker.verify().\nconst callsfunc = tracker.calls(func, 1);\n\ncallsfunc();\n\n// Calls tracker.verify() and verifies if all tracker.calls() functions have\n// been called exact times.\nprocess.on('exit', () => {\n tracker.verify();\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\nconst tracker = new assert.CallTracker();\n\nfunction func() {}\n\n// callsfunc() must be called exactly 1 time before tracker.verify().\nconst callsfunc = tracker.calls(func, 1);\n\ncallsfunc();\n\n// Calls tracker.verify() and verifies if all tracker.calls() functions have\n// been called exact times.\nprocess.on('exit', () => {\n tracker.verify();\n});\n</code></pre>" 317 } 318 ] 319 } 320 ], 321 "methods": [ 322 { 323 "textRaw": "`assert(value[, message])`", 324 "type": "method", 325 "name": "assert", 326 "meta": { 327 "added": [ 328 "v0.5.9" 329 ], 330 "changes": [] 331 }, 332 "signatures": [ 333 { 334 "params": [ 335 { 336 "textRaw": "`value` {any} The input that is checked for being truthy.", 337 "name": "value", 338 "type": "any", 339 "desc": "The input that is checked for being truthy." 340 }, 341 { 342 "textRaw": "`message` {string|Error}", 343 "name": "message", 344 "type": "string|Error" 345 } 346 ] 347 } 348 ], 349 "desc": "<p>An alias of <a href=\"#assertokvalue-message\"><code>assert.ok()</code></a>.</p>" 350 }, 351 { 352 "textRaw": "`assert.deepEqual(actual, expected[, message])`", 353 "type": "method", 354 "name": "deepEqual", 355 "meta": { 356 "added": [ 357 "v0.1.21" 358 ], 359 "changes": [ 360 { 361 "version": "v18.0.0", 362 "pr-url": "https://github.com/nodejs/node/pull/41020", 363 "description": "Regular expressions lastIndex property is now compared as well." 364 }, 365 { 366 "version": [ 367 "v16.0.0", 368 "v14.18.0" 369 ], 370 "pr-url": "https://github.com/nodejs/node/pull/38113", 371 "description": "In Legacy assertion mode, changed status from Deprecated to Legacy." 372 }, 373 { 374 "version": "v14.0.0", 375 "pr-url": "https://github.com/nodejs/node/pull/30766", 376 "description": "NaN is now treated as being identical if both sides are NaN." 377 }, 378 { 379 "version": "v12.0.0", 380 "pr-url": "https://github.com/nodejs/node/pull/25008", 381 "description": "The type tags are now properly compared and there are a couple minor comparison adjustments to make the check less surprising." 382 }, 383 { 384 "version": "v9.0.0", 385 "pr-url": "https://github.com/nodejs/node/pull/15001", 386 "description": "The `Error` names and messages are now properly compared." 387 }, 388 { 389 "version": "v8.0.0", 390 "pr-url": "https://github.com/nodejs/node/pull/12142", 391 "description": "The `Set` and `Map` content is also compared." 392 }, 393 { 394 "version": [ 395 "v6.4.0", 396 "v4.7.1" 397 ], 398 "pr-url": "https://github.com/nodejs/node/pull/8002", 399 "description": "Typed array slices are handled correctly now." 400 }, 401 { 402 "version": [ 403 "v6.1.0", 404 "v4.5.0" 405 ], 406 "pr-url": "https://github.com/nodejs/node/pull/6432", 407 "description": "Objects with circular references can be used as inputs now." 408 }, 409 { 410 "version": [ 411 "v5.10.1", 412 "v4.4.3" 413 ], 414 "pr-url": "https://github.com/nodejs/node/pull/5910", 415 "description": "Handle non-`Uint8Array` typed arrays correctly." 416 } 417 ] 418 }, 419 "signatures": [ 420 { 421 "params": [ 422 { 423 "textRaw": "`actual` {any}", 424 "name": "actual", 425 "type": "any" 426 }, 427 { 428 "textRaw": "`expected` {any}", 429 "name": "expected", 430 "type": "any" 431 }, 432 { 433 "textRaw": "`message` {string|Error}", 434 "name": "message", 435 "type": "string|Error" 436 } 437 ] 438 } 439 ], 440 "desc": "<p><strong>Strict assertion mode</strong></p>\n<p>An alias of <a href=\"#assertdeepstrictequalactual-expected-message\"><code>assert.deepStrictEqual()</code></a>.</p>\n<p><strong>Legacy assertion mode</strong></p>\n<blockquote>\n<p>Stability: 3 - Legacy: Use <a href=\"#assertdeepstrictequalactual-expected-message\"><code>assert.deepStrictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. Consider\nusing <a href=\"#assertdeepstrictequalactual-expected-message\"><code>assert.deepStrictEqual()</code></a> instead. <a href=\"#assertdeepequalactual-expected-message\"><code>assert.deepEqual()</code></a> can have\nsurprising results.</p>\n<p><em>Deep equality</em> means that the enumerable \"own\" properties of child objects\nare also recursively evaluated by the following rules.</p>", 441 "modules": [ 442 { 443 "textRaw": "Comparison details", 444 "name": "comparison_details", 445 "desc": "<ul>\n<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>,\nwith the exception of <code>NaN</code>. It is treated as being identical in case\nboth sides are <code>NaN</code>.</li>\n<li><a href=\"https://tc39.github.io/ecma262/#sec-object.prototype.tostring\">Type tags</a> of objects should be the same.</li>\n<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>\n<li><a href=\"errors.html#class-error\"><code>Error</code></a> names and messages are always compared, even if these are not\nenumerable properties.</li>\n<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>\n<li><code>Object</code> properties are compared unordered.</li>\n<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>\n<li>Recursion stops when both sides differ or both sides encounter a circular\nreference.</li>\n<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\nobjects.</li>\n<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>\n<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>\n<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\nare not enumerable properties.</li>\n</ul>\n<p>The following example does not throw an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> because the\nprimitives are compared using the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality\"><code>==</code> operator</a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n// WARNING: This does not throw an AssertionError!\n\nassert.deepEqual('+00000000', false);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n// WARNING: This does not throw an AssertionError!\n\nassert.deepEqual('+00000000', false);\n</code></pre>\n<p>\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare evaluated also:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = Object.create(obj1);\n\nassert.deepEqual(obj1, obj1);\n// OK\n\n// Values of b are different:\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n\nassert.deepEqual(obj1, obj3);\n// OK\n\n// Prototypes are ignored:\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = Object.create(obj1);\n\nassert.deepEqual(obj1, obj1);\n// OK\n\n// Values of b are different:\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n\nassert.deepEqual(obj1, obj3);\n// OK\n\n// Prototypes are ignored:\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n</code></pre>\n<p>If the values are not equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>", 446 "type": "module", 447 "displayName": "Comparison details" 448 } 449 ] 450 }, 451 { 452 "textRaw": "`assert.deepStrictEqual(actual, expected[, message])`", 453 "type": "method", 454 "name": "deepStrictEqual", 455 "meta": { 456 "added": [ 457 "v1.2.0" 458 ], 459 "changes": [ 460 { 461 "version": "v18.0.0", 462 "pr-url": "https://github.com/nodejs/node/pull/41020", 463 "description": "Regular expressions lastIndex property is now compared as well." 464 }, 465 { 466 "version": "v9.0.0", 467 "pr-url": "https://github.com/nodejs/node/pull/15169", 468 "description": "Enumerable symbol properties are now compared." 469 }, 470 { 471 "version": "v9.0.0", 472 "pr-url": "https://github.com/nodejs/node/pull/15036", 473 "description": "The `NaN` is now compared using the [SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero) comparison." 474 }, 475 { 476 "version": "v8.5.0", 477 "pr-url": "https://github.com/nodejs/node/pull/15001", 478 "description": "The `Error` names and messages are now properly compared." 479 }, 480 { 481 "version": "v8.0.0", 482 "pr-url": "https://github.com/nodejs/node/pull/12142", 483 "description": "The `Set` and `Map` content is also compared." 484 }, 485 { 486 "version": [ 487 "v6.4.0", 488 "v4.7.1" 489 ], 490 "pr-url": "https://github.com/nodejs/node/pull/8002", 491 "description": "Typed array slices are handled correctly now." 492 }, 493 { 494 "version": "v6.1.0", 495 "pr-url": "https://github.com/nodejs/node/pull/6432", 496 "description": "Objects with circular references can be used as inputs now." 497 }, 498 { 499 "version": [ 500 "v5.10.1", 501 "v4.4.3" 502 ], 503 "pr-url": "https://github.com/nodejs/node/pull/5910", 504 "description": "Handle non-`Uint8Array` typed arrays correctly." 505 } 506 ] 507 }, 508 "signatures": [ 509 { 510 "params": [ 511 { 512 "textRaw": "`actual` {any}", 513 "name": "actual", 514 "type": "any" 515 }, 516 { 517 "textRaw": "`expected` {any}", 518 "name": "expected", 519 "type": "any" 520 }, 521 { 522 "textRaw": "`message` {string|Error}", 523 "name": "message", 524 "type": "string|Error" 525 } 526 ] 527 } 528 ], 529 "desc": "<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.\n\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare recursively evaluated also by the following rules.</p>", 530 "modules": [ 531 { 532 "textRaw": "Comparison details", 533 "name": "comparison_details", 534 "desc": "<ul>\n<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>\n<li><a href=\"https://tc39.github.io/ecma262/#sec-object.prototype.tostring\">Type tags</a> of objects should be the same.</li>\n<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\nthe <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality\"><code>===</code> operator</a>.</li>\n<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>\n<li><a href=\"errors.html#class-error\"><code>Error</code></a> names and messages are always compared, even if these are not\nenumerable properties.</li>\n<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>\n<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>\n<li><code>Object</code> properties are compared unordered.</li>\n<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>\n<li>Recursion stops when both sides differ or both sides encounter a circular\nreference.</li>\n<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\nbelow for further details.</li>\n<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\nare not enumerable properties.</li>\n</ul>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\n// This fails because 1 !== '1'.\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// {\n// + a: 1\n// - a: '1'\n// }\n\n// The following objects don't have own properties\nconst date = new Date();\nconst object = {};\nconst fakeDate = {};\nObject.setPrototypeOf(fakeDate, Date.prototype);\n\n// Different [[Prototype]]:\nassert.deepStrictEqual(object, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + {}\n// - Date {}\n\n// Different type tags:\nassert.deepStrictEqual(date, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 2018-04-26T00:49:08.604Z\n// - Date {}\n\nassert.deepStrictEqual(NaN, NaN);\n// OK because Object.is(NaN, NaN) is true.\n\n// Different unwrapped numbers:\nassert.deepStrictEqual(new Number(1), new Number(2));\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + [Number: 1]\n// - [Number: 2]\n\nassert.deepStrictEqual(new String('foo'), Object('foo'));\n// OK because the object and the string are identical when unwrapped.\n\nassert.deepStrictEqual(-0, -0);\n// OK\n\n// Different zeros:\nassert.deepStrictEqual(0, -0);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 0\n// - -0\n\nconst symbol1 = Symbol();\nconst symbol2 = Symbol();\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });\n// OK, because it is the same symbol on both objects.\n\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });\n// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:\n//\n// {\n// [Symbol()]: 1\n// }\n\nconst weakMap1 = new WeakMap();\nconst weakMap2 = new WeakMap([[{}, {}]]);\nconst weakMap3 = new WeakMap();\nweakMap3.unequal = true;\n\nassert.deepStrictEqual(weakMap1, weakMap2);\n// OK, because it is impossible to compare the entries\n\n// Fails because weakMap3 has a property that weakMap1 does not contain:\nassert.deepStrictEqual(weakMap1, weakMap3);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// WeakMap {\n// + [items unknown]\n// - [items unknown],\n// - unequal: true\n// }\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\n// This fails because 1 !== '1'.\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// {\n// + a: 1\n// - a: '1'\n// }\n\n// The following objects don't have own properties\nconst date = new Date();\nconst object = {};\nconst fakeDate = {};\nObject.setPrototypeOf(fakeDate, Date.prototype);\n\n// Different [[Prototype]]:\nassert.deepStrictEqual(object, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + {}\n// - Date {}\n\n// Different type tags:\nassert.deepStrictEqual(date, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 2018-04-26T00:49:08.604Z\n// - Date {}\n\nassert.deepStrictEqual(NaN, NaN);\n// OK because Object.is(NaN, NaN) is true.\n\n// Different unwrapped numbers:\nassert.deepStrictEqual(new Number(1), new Number(2));\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + [Number: 1]\n// - [Number: 2]\n\nassert.deepStrictEqual(new String('foo'), Object('foo'));\n// OK because the object and the string are identical when unwrapped.\n\nassert.deepStrictEqual(-0, -0);\n// OK\n\n// Different zeros:\nassert.deepStrictEqual(0, -0);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 0\n// - -0\n\nconst symbol1 = Symbol();\nconst symbol2 = Symbol();\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });\n// OK, because it is the same symbol on both objects.\n\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });\n// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:\n//\n// {\n// [Symbol()]: 1\n// }\n\nconst weakMap1 = new WeakMap();\nconst weakMap2 = new WeakMap([[{}, {}]]);\nconst weakMap3 = new WeakMap();\nweakMap3.unequal = true;\n\nassert.deepStrictEqual(weakMap1, weakMap2);\n// OK, because it is impossible to compare the entries\n\n// Fails because weakMap3 has a property that weakMap1 does not contain:\nassert.deepStrictEqual(weakMap1, weakMap3);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// WeakMap {\n// + [items unknown]\n// - [items unknown],\n// - unequal: true\n// }\n</code></pre>\n<p>If the values are not equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>", 535 "type": "module", 536 "displayName": "Comparison details" 537 } 538 ] 539 }, 540 { 541 "textRaw": "`assert.doesNotMatch(string, regexp[, message])`", 542 "type": "method", 543 "name": "doesNotMatch", 544 "meta": { 545 "added": [ 546 "v13.6.0", 547 "v12.16.0" 548 ], 549 "changes": [ 550 { 551 "version": "v16.0.0", 552 "pr-url": "https://github.com/nodejs/node/pull/38111", 553 "description": "This API is no longer experimental." 554 } 555 ] 556 }, 557 "signatures": [ 558 { 559 "params": [ 560 { 561 "textRaw": "`string` {string}", 562 "name": "string", 563 "type": "string" 564 }, 565 { 566 "textRaw": "`regexp` {RegExp}", 567 "name": "regexp", 568 "type": "RegExp" 569 }, 570 { 571 "textRaw": "`message` {string|Error}", 572 "name": "message", 573 "type": "string|Error" 574 } 575 ] 576 } 577 ], 578 "desc": "<p>Expects the <code>string</code> input not to match the regular expression.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.doesNotMatch('I will fail', /fail/);\n// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...\n\nassert.doesNotMatch(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.doesNotMatch('I will pass', /different/);\n// OK\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.doesNotMatch('I will fail', /fail/);\n// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...\n\nassert.doesNotMatch(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.doesNotMatch('I will pass', /different/);\n// OK\n</code></pre>\n<p>If the values do match, or if the <code>string</code> argument is of another type than\n<code>string</code>, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal\nto the value of the <code>message</code> parameter. If the <code>message</code> parameter is\nundefined, a default error message is assigned. If the <code>message</code> parameter is an\ninstance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>" 579 }, 580 { 581 "textRaw": "`assert.doesNotReject(asyncFn[, error][, message])`", 582 "type": "method", 583 "name": "doesNotReject", 584 "meta": { 585 "added": [ 586 "v10.0.0" 587 ], 588 "changes": [] 589 }, 590 "signatures": [ 591 { 592 "params": [ 593 { 594 "textRaw": "`asyncFn` {Function|Promise}", 595 "name": "asyncFn", 596 "type": "Function|Promise" 597 }, 598 { 599 "textRaw": "`error` {RegExp|Function}", 600 "name": "error", 601 "type": "RegExp|Function" 602 }, 603 { 604 "textRaw": "`message` {string}", 605 "name": "message", 606 "type": "string" 607 } 608 ] 609 } 610 ], 611 "desc": "<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately\ncalls the function and awaits the returned promise to complete. It will then\ncheck that the promise is not rejected.</p>\n<p>If <code>asyncFn</code> is a function and it throws an error synchronously,\n<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If\nthe function does not return a promise, <code>assert.doesNotReject()</code> will return a\nrejected <code>Promise</code> with an <a href=\"errors.html#err_invalid_return_value\"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases\nthe error handler is skipped.</p>\n<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little\nbenefit in catching a rejection and then rejecting it again. Instead, consider\nadding a comment next to the specific code path that should not reject and keep\nerror messages as expressive as possible.</p>\n<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\nfunction. See <a href=\"#assertthrowsfn-error-message\"><code>assert.throws()</code></a> for more details.</p>\n<p>Besides the async nature to await the completion behaves identically to\n<a href=\"#assertdoesnotthrowfn-error-message\"><code>assert.doesNotThrow()</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nawait assert.doesNotReject(\n async () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\n(async () => {\n await assert.doesNotReject(\n async () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n );\n})();\n</code></pre>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.doesNotReject(Promise.reject(new TypeError('Wrong value')))\n .then(() => {\n // ...\n });\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.doesNotReject(Promise.reject(new TypeError('Wrong value')))\n .then(() => {\n // ...\n });\n</code></pre>" 612 }, 613 { 614 "textRaw": "`assert.doesNotThrow(fn[, error][, message])`", 615 "type": "method", 616 "name": "doesNotThrow", 617 "meta": { 618 "added": [ 619 "v0.1.21" 620 ], 621 "changes": [ 622 { 623 "version": [ 624 "v5.11.0", 625 "v4.4.5" 626 ], 627 "pr-url": "https://github.com/nodejs/node/pull/2407", 628 "description": "The `message` parameter is respected now." 629 }, 630 { 631 "version": "v4.2.0", 632 "pr-url": "https://github.com/nodejs/node/pull/3276", 633 "description": "The `error` parameter can now be an arrow function." 634 } 635 ] 636 }, 637 "signatures": [ 638 { 639 "params": [ 640 { 641 "textRaw": "`fn` {Function}", 642 "name": "fn", 643 "type": "Function" 644 }, 645 { 646 "textRaw": "`error` {RegExp|Function}", 647 "name": "error", 648 "type": "RegExp|Function" 649 }, 650 { 651 "textRaw": "`message` {string}", 652 "name": "message", 653 "type": "string" 654 } 655 ] 656 } 657 ], 658 "desc": "<p>Asserts that the function <code>fn</code> does not throw an error.</p>\n<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there\nis no benefit in catching an error and then rethrowing it. Instead, consider\nadding a comment next to the specific code path that should not throw and keep\nerror messages as expressive as possible.</p>\n<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code>\nfunction.</p>\n<p>If an error is thrown and it is the same type as that specified by the <code>error</code>\nparameter, then an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown. If the error is of a\ndifferent type, or if the <code>error</code> parameter is undefined, the error is\npropagated back to the caller.</p>\n<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\nfunction. See <a href=\"#assertthrowsfn-error-message\"><code>assert.throws()</code></a> for more details.</p>\n<p>The following, for instance, will throw the <a href=\"errors.html#class-typeerror\"><code>TypeError</code></a> because there is no\nmatching error type in the assertion:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n);\n</code></pre>\n<p>However, the following will result in an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> with the message\n'Got unwanted exception...':</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n TypeError,\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n TypeError,\n);\n</code></pre>\n<p>If an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown and a value is provided for the <code>message</code>\nparameter, the value of <code>message</code> will be appended to the <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>\nmessage:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n /Wrong value/,\n 'Whoops',\n);\n// Throws: AssertionError: Got unwanted exception: Whoops\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n /Wrong value/,\n 'Whoops',\n);\n// Throws: AssertionError: Got unwanted exception: Whoops\n</code></pre>" 659 }, 660 { 661 "textRaw": "`assert.equal(actual, expected[, message])`", 662 "type": "method", 663 "name": "equal", 664 "meta": { 665 "added": [ 666 "v0.1.21" 667 ], 668 "changes": [ 669 { 670 "version": [ 671 "v16.0.0", 672 "v14.18.0" 673 ], 674 "pr-url": "https://github.com/nodejs/node/pull/38113", 675 "description": "In Legacy assertion mode, changed status from Deprecated to Legacy." 676 }, 677 { 678 "version": "v14.0.0", 679 "pr-url": "https://github.com/nodejs/node/pull/30766", 680 "description": "NaN is now treated as being identical if both sides are NaN." 681 } 682 ] 683 }, 684 "signatures": [ 685 { 686 "params": [ 687 { 688 "textRaw": "`actual` {any}", 689 "name": "actual", 690 "type": "any" 691 }, 692 { 693 "textRaw": "`expected` {any}", 694 "name": "expected", 695 "type": "any" 696 }, 697 { 698 "textRaw": "`message` {string|Error}", 699 "name": "message", 700 "type": "string|Error" 701 } 702 ] 703 } 704 ], 705 "desc": "<p><strong>Strict assertion mode</strong></p>\n<p>An alias of <a href=\"#assertstrictequalactual-expected-message\"><code>assert.strictEqual()</code></a>.</p>\n<p><strong>Legacy assertion mode</strong></p>\n<blockquote>\n<p>Stability: 3 - Legacy: Use <a href=\"#assertstrictequalactual-expected-message\"><code>assert.strictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters\nusing 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\nand treated as being identical if both sides are <code>NaN</code>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\nassert.equal(1, 1);\n// OK, 1 == 1\nassert.equal(1, '1');\n// OK, 1 == '1'\nassert.equal(NaN, NaN);\n// OK\n\nassert.equal(1, 2);\n// AssertionError: 1 == 2\nassert.equal({ a: { b: 1 } }, { a: { b: 1 } });\n// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\nassert.equal(1, 1);\n// OK, 1 == 1\nassert.equal(1, '1');\n// OK, 1 == '1'\nassert.equal(NaN, NaN);\n// OK\n\nassert.equal(1, 2);\n// AssertionError: 1 == 2\nassert.equal({ a: { b: 1 } }, { a: { b: 1 } });\n// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }\n</code></pre>\n<p>If the values are not equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" 706 }, 707 { 708 "textRaw": "`assert.fail([message])`", 709 "type": "method", 710 "name": "fail", 711 "meta": { 712 "added": [ 713 "v0.1.21" 714 ], 715 "changes": [] 716 }, 717 "signatures": [ 718 { 719 "params": [ 720 { 721 "textRaw": "`message` {string|Error} **Default:** `'Failed'`", 722 "name": "message", 723 "type": "string|Error", 724 "default": "`'Failed'`" 725 } 726 ] 727 } 728 ], 729 "desc": "<p>Throws an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> with the provided error message or a default\nerror message. If the <code>message</code> parameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then\nit will be thrown instead of the <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail(new TypeError('need array'));\n// TypeError: need array\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail(new TypeError('need array'));\n// TypeError: need array\n</code></pre>\n<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated.\nSee below for further details.</p>" 730 }, 731 { 732 "textRaw": "`assert.fail(actual, expected[, message[, operator[, stackStartFn]]])`", 733 "type": "method", 734 "name": "fail", 735 "meta": { 736 "added": [ 737 "v0.1.21" 738 ], 739 "changes": [ 740 { 741 "version": "v10.0.0", 742 "pr-url": "https://github.com/nodejs/node/pull/18418", 743 "description": "Calling `assert.fail()` with more than one argument is deprecated and emits a warning." 744 } 745 ] 746 }, 747 "stability": 0, 748 "stabilityText": "Deprecated: Use `assert.fail([message])` or other assert functions instead.", 749 "signatures": [ 750 { 751 "params": [ 752 { 753 "textRaw": "`actual` {any}", 754 "name": "actual", 755 "type": "any" 756 }, 757 { 758 "textRaw": "`expected` {any}", 759 "name": "expected", 760 "type": "any" 761 }, 762 { 763 "textRaw": "`message` {string|Error}", 764 "name": "message", 765 "type": "string|Error" 766 }, 767 { 768 "textRaw": "`operator` {string} **Default:** `'!='`", 769 "name": "operator", 770 "type": "string", 771 "default": "`'!='`" 772 }, 773 { 774 "textRaw": "`stackStartFn` {Function} **Default:** `assert.fail`", 775 "name": "stackStartFn", 776 "type": "Function", 777 "default": "`assert.fail`" 778 } 779 ] 780 } 781 ], 782 "desc": "<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and\n<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and\n<code>expected</code> arguments are provided, <code>operator</code> will default to <code>'!='</code>. If\n<code>message</code> is provided as third argument it will be used as the error message and\nthe other arguments will be stored as properties on the thrown object. If\n<code>stackStartFn</code> is provided, all stack frames above that function will be\nremoved from stacktrace (see <a href=\"errors.html#errorcapturestacktracetargetobject-constructoropt\"><code>Error.captureStackTrace</code></a>). If no arguments are\ngiven, the default message <code>Failed</code> will be used.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.fail('a', 'b');\n// AssertionError [ERR_ASSERTION]: 'a' != 'b'\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError [ERR_ASSERTION]: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError [ERR_ASSERTION]: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError [ERR_ASSERTION]: whoops\n\nassert.fail(1, 2, new TypeError('need array'));\n// TypeError: need array\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.fail('a', 'b');\n// AssertionError [ERR_ASSERTION]: 'a' != 'b'\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError [ERR_ASSERTION]: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError [ERR_ASSERTION]: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError [ERR_ASSERTION]: whoops\n\nassert.fail(1, 2, new TypeError('need array'));\n// TypeError: need array\n</code></pre>\n<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no\ninfluence on the error message.</p>\n<p>Example use of <code>stackStartFn</code> for truncating the exception's stacktrace:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nfunction suppressFrame() {\n assert.fail('a', 'b', undefined, '!==', suppressFrame);\n}\nsuppressFrame();\n// AssertionError [ERR_ASSERTION]: 'a' !== 'b'\n// at repl:1:1\n// at ContextifyScript.Script.runInThisContext (vm.js:44:33)\n// ...\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nfunction suppressFrame() {\n assert.fail('a', 'b', undefined, '!==', suppressFrame);\n}\nsuppressFrame();\n// AssertionError [ERR_ASSERTION]: 'a' !== 'b'\n// at repl:1:1\n// at ContextifyScript.Script.runInThisContext (vm.js:44:33)\n// ...\n</code></pre>" 783 }, 784 { 785 "textRaw": "`assert.ifError(value)`", 786 "type": "method", 787 "name": "ifError", 788 "meta": { 789 "added": [ 790 "v0.1.97" 791 ], 792 "changes": [ 793 { 794 "version": "v10.0.0", 795 "pr-url": "https://github.com/nodejs/node/pull/18247", 796 "description": "Instead of throwing the original error it is now wrapped into an [`AssertionError`][] that contains the full stack trace." 797 }, 798 { 799 "version": "v10.0.0", 800 "pr-url": "https://github.com/nodejs/node/pull/18247", 801 "description": "Value may now only be `undefined` or `null`. Before all falsy values were handled the same as `null` and did not throw." 802 } 803 ] 804 }, 805 "signatures": [ 806 { 807 "params": [ 808 { 809 "textRaw": "`value` {any}", 810 "name": "value", 811 "type": "any" 812 } 813 ] 814 } 815 ], 816 "desc": "<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when\ntesting the <code>error</code> argument in callbacks. The stack trace contains all frames\nfrom the error passed to <code>ifError()</code> including the potential new frames for\n<code>ifError()</code> itself.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0\nassert.ifError('error');\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'\nassert.ifError(new Error());\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error\n\n// Create some random error frames.\nlet err;\n(function errorFrame() {\n err = new Error('test error');\n})();\n\n(function ifErrorFrame() {\n assert.ifError(err);\n})();\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error\n// at ifErrorFrame\n// at errorFrame\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0\nassert.ifError('error');\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'\nassert.ifError(new Error());\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error\n\n// Create some random error frames.\nlet err;\n(function errorFrame() {\n err = new Error('test error');\n})();\n\n(function ifErrorFrame() {\n assert.ifError(err);\n})();\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error\n// at ifErrorFrame\n// at errorFrame\n</code></pre>" 817 }, 818 { 819 "textRaw": "`assert.match(string, regexp[, message])`", 820 "type": "method", 821 "name": "match", 822 "meta": { 823 "added": [ 824 "v13.6.0", 825 "v12.16.0" 826 ], 827 "changes": [ 828 { 829 "version": "v16.0.0", 830 "pr-url": "https://github.com/nodejs/node/pull/38111", 831 "description": "This API is no longer experimental." 832 } 833 ] 834 }, 835 "signatures": [ 836 { 837 "params": [ 838 { 839 "textRaw": "`string` {string}", 840 "name": "string", 841 "type": "string" 842 }, 843 { 844 "textRaw": "`regexp` {RegExp}", 845 "name": "regexp", 846 "type": "RegExp" 847 }, 848 { 849 "textRaw": "`message` {string|Error}", 850 "name": "message", 851 "type": "string|Error" 852 } 853 ] 854 } 855 ], 856 "desc": "<p>Expects the <code>string</code> input to match the regular expression.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.match('I will fail', /pass/);\n// AssertionError [ERR_ASSERTION]: The input did not match the regular ...\n\nassert.match(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.match('I will pass', /pass/);\n// OK\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.match('I will fail', /pass/);\n// AssertionError [ERR_ASSERTION]: The input did not match the regular ...\n\nassert.match(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.match('I will pass', /pass/);\n// OK\n</code></pre>\n<p>If the values do not match, or if the <code>string</code> argument is of another type than\n<code>string</code>, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal\nto the value of the <code>message</code> parameter. If the <code>message</code> parameter is\nundefined, a default error message is assigned. If the <code>message</code> parameter is an\ninstance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>" 857 }, 858 { 859 "textRaw": "`assert.notDeepEqual(actual, expected[, message])`", 860 "type": "method", 861 "name": "notDeepEqual", 862 "meta": { 863 "added": [ 864 "v0.1.21" 865 ], 866 "changes": [ 867 { 868 "version": [ 869 "v16.0.0", 870 "v14.18.0" 871 ], 872 "pr-url": "https://github.com/nodejs/node/pull/38113", 873 "description": "In Legacy assertion mode, changed status from Deprecated to Legacy." 874 }, 875 { 876 "version": "v14.0.0", 877 "pr-url": "https://github.com/nodejs/node/pull/30766", 878 "description": "NaN is now treated as being identical if both sides are NaN." 879 }, 880 { 881 "version": "v9.0.0", 882 "pr-url": "https://github.com/nodejs/node/pull/15001", 883 "description": "The `Error` names and messages are now properly compared." 884 }, 885 { 886 "version": "v8.0.0", 887 "pr-url": "https://github.com/nodejs/node/pull/12142", 888 "description": "The `Set` and `Map` content is also compared." 889 }, 890 { 891 "version": [ 892 "v6.4.0", 893 "v4.7.1" 894 ], 895 "pr-url": "https://github.com/nodejs/node/pull/8002", 896 "description": "Typed array slices are handled correctly now." 897 }, 898 { 899 "version": [ 900 "v6.1.0", 901 "v4.5.0" 902 ], 903 "pr-url": "https://github.com/nodejs/node/pull/6432", 904 "description": "Objects with circular references can be used as inputs now." 905 }, 906 { 907 "version": [ 908 "v5.10.1", 909 "v4.4.3" 910 ], 911 "pr-url": "https://github.com/nodejs/node/pull/5910", 912 "description": "Handle non-`Uint8Array` typed arrays correctly." 913 } 914 ] 915 }, 916 "signatures": [ 917 { 918 "params": [ 919 { 920 "textRaw": "`actual` {any}", 921 "name": "actual", 922 "type": "any" 923 }, 924 { 925 "textRaw": "`expected` {any}", 926 "name": "expected", 927 "type": "any" 928 }, 929 { 930 "textRaw": "`message` {string|Error}", 931 "name": "message", 932 "type": "string|Error" 933 } 934 ] 935 } 936 ], 937 "desc": "<p><strong>Strict assertion mode</strong></p>\n<p>An alias of <a href=\"#assertnotdeepstrictequalactual-expected-message\"><code>assert.notDeepStrictEqual()</code></a>.</p>\n<p><strong>Legacy assertion mode</strong></p>\n<blockquote>\n<p>Stability: 3 - Legacy: Use <a href=\"#assertnotdeepstrictequalactual-expected-message\"><code>assert.notDeepStrictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests for any deep inequality. Opposite of <a href=\"#assertdeepequalactual-expected-message\"><code>assert.deepEqual()</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = Object.create(obj1);\n\nassert.notDeepEqual(obj1, obj1);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj2);\n// OK\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = Object.create(obj1);\n\nassert.notDeepEqual(obj1, obj1);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj2);\n// OK\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK\n</code></pre>\n<p>If the values are deeply equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a\n<code>message</code> property set equal to the value of the <code>message</code> parameter. If the\n<code>message</code> parameter is undefined, a default error message is assigned. If the\n<code>message</code> parameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown\ninstead of the <code>AssertionError</code>.</p>" 938 }, 939 { 940 "textRaw": "`assert.notDeepStrictEqual(actual, expected[, message])`", 941 "type": "method", 942 "name": "notDeepStrictEqual", 943 "meta": { 944 "added": [ 945 "v1.2.0" 946 ], 947 "changes": [ 948 { 949 "version": "v9.0.0", 950 "pr-url": "https://github.com/nodejs/node/pull/15398", 951 "description": "The `-0` and `+0` are not considered equal anymore." 952 }, 953 { 954 "version": "v9.0.0", 955 "pr-url": "https://github.com/nodejs/node/pull/15036", 956 "description": "The `NaN` is now compared using the [SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero) comparison." 957 }, 958 { 959 "version": "v9.0.0", 960 "pr-url": "https://github.com/nodejs/node/pull/15001", 961 "description": "The `Error` names and messages are now properly compared." 962 }, 963 { 964 "version": "v8.0.0", 965 "pr-url": "https://github.com/nodejs/node/pull/12142", 966 "description": "The `Set` and `Map` content is also compared." 967 }, 968 { 969 "version": [ 970 "v6.4.0", 971 "v4.7.1" 972 ], 973 "pr-url": "https://github.com/nodejs/node/pull/8002", 974 "description": "Typed array slices are handled correctly now." 975 }, 976 { 977 "version": "v6.1.0", 978 "pr-url": "https://github.com/nodejs/node/pull/6432", 979 "description": "Objects with circular references can be used as inputs now." 980 }, 981 { 982 "version": [ 983 "v5.10.1", 984 "v4.4.3" 985 ], 986 "pr-url": "https://github.com/nodejs/node/pull/5910", 987 "description": "Handle non-`Uint8Array` typed arrays correctly." 988 } 989 ] 990 }, 991 "signatures": [ 992 { 993 "params": [ 994 { 995 "textRaw": "`actual` {any}", 996 "name": "actual", 997 "type": "any" 998 }, 999 { 1000 "textRaw": "`expected` {any}", 1001 "name": "expected", 1002 "type": "any" 1003 }, 1004 { 1005 "textRaw": "`message` {string|Error}", 1006 "name": "message", 1007 "type": "string|Error" 1008 } 1009 ] 1010 } 1011 ], 1012 "desc": "<p>Tests for deep strict inequality. Opposite of <a href=\"#assertdeepstrictequalactual-expected-message\"><code>assert.deepStrictEqual()</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.notDeepStrictEqual({ a: 1 }, { a: '1' });\n// OK\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.notDeepStrictEqual({ a: 1 }, { a: '1' });\n// OK\n</code></pre>\n<p>If the values are deeply and strictly equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown\nwith a <code>message</code> property set equal to the value of the <code>message</code> parameter. If\nthe <code>message</code> parameter is undefined, a default error message is assigned. If\nthe <code>message</code> parameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown\ninstead of the <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>" 1013 }, 1014 { 1015 "textRaw": "`assert.notEqual(actual, expected[, message])`", 1016 "type": "method", 1017 "name": "notEqual", 1018 "meta": { 1019 "added": [ 1020 "v0.1.21" 1021 ], 1022 "changes": [ 1023 { 1024 "version": [ 1025 "v16.0.0", 1026 "v14.18.0" 1027 ], 1028 "pr-url": "https://github.com/nodejs/node/pull/38113", 1029 "description": "In Legacy assertion mode, changed status from Deprecated to Legacy." 1030 }, 1031 { 1032 "version": "v14.0.0", 1033 "pr-url": "https://github.com/nodejs/node/pull/30766", 1034 "description": "NaN is now treated as being identical if both sides are NaN." 1035 } 1036 ] 1037 }, 1038 "signatures": [ 1039 { 1040 "params": [ 1041 { 1042 "textRaw": "`actual` {any}", 1043 "name": "actual", 1044 "type": "any" 1045 }, 1046 { 1047 "textRaw": "`expected` {any}", 1048 "name": "expected", 1049 "type": "any" 1050 }, 1051 { 1052 "textRaw": "`message` {string|Error}", 1053 "name": "message", 1054 "type": "string|Error" 1055 } 1056 ] 1057 } 1058 ], 1059 "desc": "<p><strong>Strict assertion mode</strong></p>\n<p>An alias of <a href=\"#assertnotstrictequalactual-expected-message\"><code>assert.notStrictEqual()</code></a>.</p>\n<p><strong>Legacy assertion mode</strong></p>\n<blockquote>\n<p>Stability: 3 - Legacy: Use <a href=\"#assertnotstrictequalactual-expected-message\"><code>assert.notStrictEqual()</code></a> instead.</p>\n</blockquote>\n<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\nspecially handled and treated as being identical if both sides are <code>NaN</code>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\n\nassert.notEqual(1, 2);\n// OK\n\nassert.notEqual(1, 1);\n// AssertionError: 1 != 1\n\nassert.notEqual(1, '1');\n// AssertionError: 1 != '1'\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\nassert.notEqual(1, 2);\n// OK\n\nassert.notEqual(1, 1);\n// AssertionError: 1 != 1\n\nassert.notEqual(1, '1');\n// AssertionError: 1 != '1'\n</code></pre>\n<p>If the values are equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" 1060 }, 1061 { 1062 "textRaw": "`assert.notStrictEqual(actual, expected[, message])`", 1063 "type": "method", 1064 "name": "notStrictEqual", 1065 "meta": { 1066 "added": [ 1067 "v0.1.21" 1068 ], 1069 "changes": [ 1070 { 1071 "version": "v10.0.0", 1072 "pr-url": "https://github.com/nodejs/node/pull/17003", 1073 "description": "Used comparison changed from Strict Equality to `Object.is()`." 1074 } 1075 ] 1076 }, 1077 "signatures": [ 1078 { 1079 "params": [ 1080 { 1081 "textRaw": "`actual` {any}", 1082 "name": "actual", 1083 "type": "any" 1084 }, 1085 { 1086 "textRaw": "`expected` {any}", 1087 "name": "expected", 1088 "type": "any" 1089 }, 1090 { 1091 "textRaw": "`message` {string|Error}", 1092 "name": "message", 1093 "type": "string|Error" 1094 } 1095 ] 1096 } 1097 ], 1098 "desc": "<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as\ndetermined by <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is\"><code>Object.is()</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError [ERR_ASSERTION]: Expected \"actual\" to be strictly unequal to:\n//\n// 1\n\nassert.notStrictEqual(1, '1');\n// OK\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError [ERR_ASSERTION]: Expected \"actual\" to be strictly unequal to:\n//\n// 1\n\nassert.notStrictEqual(1, '1');\n// OK\n</code></pre>\n<p>If the values are strictly equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a\n<code>message</code> property set equal to the value of the <code>message</code> parameter. If the\n<code>message</code> parameter is undefined, a default error message is assigned. If the\n<code>message</code> parameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown\ninstead of the <code>AssertionError</code>.</p>" 1099 }, 1100 { 1101 "textRaw": "`assert.ok(value[, message])`", 1102 "type": "method", 1103 "name": "ok", 1104 "meta": { 1105 "added": [ 1106 "v0.1.21" 1107 ], 1108 "changes": [ 1109 { 1110 "version": "v10.0.0", 1111 "pr-url": "https://github.com/nodejs/node/pull/18319", 1112 "description": "The `assert.ok()` (no arguments) will now use a predefined error message." 1113 } 1114 ] 1115 }, 1116 "signatures": [ 1117 { 1118 "params": [ 1119 { 1120 "textRaw": "`value` {any}", 1121 "name": "value", 1122 "type": "any" 1123 }, 1124 { 1125 "textRaw": "`message` {string|Error}", 1126 "name": "message", 1127 "type": "string|Error" 1128 } 1129 ] 1130 } 1131 ], 1132 "desc": "<p>Tests if <code>value</code> is truthy. It is equivalent to\n<code>assert.equal(!!value, true, message)</code>.</p>\n<p>If <code>value</code> is not truthy, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.\nIf no arguments are passed in at all <code>message</code> will be set to the string:\n<code>'No value argument passed to `assert.ok()`'</code>.</p>\n<p>Be aware that in the <code>repl</code> the error message will be different to the one\nthrown in a file! See below for further details.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.ok(true);\n// OK\nassert.ok(1);\n// OK\n\nassert.ok();\n// AssertionError: No value argument passed to `assert.ok()`\n\nassert.ok(false, 'it\\'s false');\n// AssertionError: it's false\n\n// In the repl:\nassert.ok(typeof 123 === 'string');\n// AssertionError: false == true\n\n// In a file (e.g. test.js):\nassert.ok(typeof 123 === 'string');\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(typeof 123 === 'string')\n\nassert.ok(false);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(false)\n\nassert.ok(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(0)\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.ok(true);\n// OK\nassert.ok(1);\n// OK\n\nassert.ok();\n// AssertionError: No value argument passed to `assert.ok()`\n\nassert.ok(false, 'it\\'s false');\n// AssertionError: it's false\n\n// In the repl:\nassert.ok(typeof 123 === 'string');\n// AssertionError: false == true\n\n// In a file (e.g. test.js):\nassert.ok(typeof 123 === 'string');\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(typeof 123 === 'string')\n\nassert.ok(false);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(false)\n\nassert.ok(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(0)\n</code></pre>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\n// Using `assert()` works the same:\nassert(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert(0)\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\n\n// Using `assert()` works the same:\nassert(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert(0)\n</code></pre>" 1133 }, 1134 { 1135 "textRaw": "`assert.rejects(asyncFn[, error][, message])`", 1136 "type": "method", 1137 "name": "rejects", 1138 "meta": { 1139 "added": [ 1140 "v10.0.0" 1141 ], 1142 "changes": [] 1143 }, 1144 "signatures": [ 1145 { 1146 "params": [ 1147 { 1148 "textRaw": "`asyncFn` {Function|Promise}", 1149 "name": "asyncFn", 1150 "type": "Function|Promise" 1151 }, 1152 { 1153 "textRaw": "`error` {RegExp|Function|Object|Error}", 1154 "name": "error", 1155 "type": "RegExp|Function|Object|Error" 1156 }, 1157 { 1158 "textRaw": "`message` {string}", 1159 "name": "message", 1160 "type": "string" 1161 } 1162 ] 1163 } 1164 ], 1165 "desc": "<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately\ncalls the function and awaits the returned promise to complete. It will then\ncheck that the promise is rejected.</p>\n<p>If <code>asyncFn</code> is a function and it throws an error synchronously,\n<code>assert.rejects()</code> will return a rejected <code>Promise</code> with that error. If the\nfunction does not return a promise, <code>assert.rejects()</code> will return a rejected\n<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\nhandler is skipped.</p>\n<p>Besides the async nature to await the completion behaves identically to\n<a href=\"#assertthrowsfn-error-message\"><code>assert.throws()</code></a>.</p>\n<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,\nan object where each property will be tested for, or an instance of error where\neach property will be tested for including the non-enumerable <code>message</code> and\n<code>name</code> properties.</p>\n<p>If specified, <code>message</code> will be the message provided by the <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>\nif the <code>asyncFn</code> fails to reject.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nawait assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n },\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\n(async () => {\n await assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n },\n );\n})();\n</code></pre>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nawait assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n (err) => {\n assert.strictEqual(err.name, 'TypeError');\n assert.strictEqual(err.message, 'Wrong value');\n return true;\n },\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\n(async () => {\n await assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n (err) => {\n assert.strictEqual(err.name, 'TypeError');\n assert.strictEqual(err.message, 'Wrong value');\n return true;\n },\n );\n})();\n</code></pre>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.rejects(\n Promise.reject(new Error('Wrong value')),\n Error,\n).then(() => {\n // ...\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.rejects(\n Promise.reject(new Error('Wrong value')),\n Error,\n).then(() => {\n // ...\n});\n</code></pre>\n<p><code>error</code> cannot be a string. If a string is provided as the second\nargument, then <code>error</code> is assumed to be omitted and the string will be used for\n<code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the\nexample in <a href=\"#assertthrowsfn-error-message\"><code>assert.throws()</code></a> carefully if using a string as the second\nargument gets considered.</p>" 1166 }, 1167 { 1168 "textRaw": "`assert.strictEqual(actual, expected[, message])`", 1169 "type": "method", 1170 "name": "strictEqual", 1171 "meta": { 1172 "added": [ 1173 "v0.1.21" 1174 ], 1175 "changes": [ 1176 { 1177 "version": "v10.0.0", 1178 "pr-url": "https://github.com/nodejs/node/pull/17003", 1179 "description": "Used comparison changed from Strict Equality to `Object.is()`." 1180 } 1181 ] 1182 }, 1183 "signatures": [ 1184 { 1185 "params": [ 1186 { 1187 "textRaw": "`actual` {any}", 1188 "name": "actual", 1189 "type": "any" 1190 }, 1191 { 1192 "textRaw": "`expected` {any}", 1193 "name": "expected", 1194 "type": "any" 1195 }, 1196 { 1197 "textRaw": "`message` {string|Error}", 1198 "name": "message", 1199 "type": "string|Error" 1200 } 1201 ] 1202 } 1203 ], 1204 "desc": "<p>Tests strict equality between the <code>actual</code> and <code>expected</code> parameters as\ndetermined by <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is\"><code>Object.is()</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.strictEqual(1, 2);\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n//\n// 1 !== 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual('Hello foobar', 'Hello World!');\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n// + actual - expected\n//\n// + 'Hello foobar'\n// - 'Hello World!'\n// ^\n\nconst apples = 1;\nconst oranges = 2;\nassert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);\n// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2\n\nassert.strictEqual(1, '1', new TypeError('Inputs are not identical'));\n// TypeError: Inputs are not identical\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.strictEqual(1, 2);\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n//\n// 1 !== 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual('Hello foobar', 'Hello World!');\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n// + actual - expected\n//\n// + 'Hello foobar'\n// - 'Hello World!'\n// ^\n\nconst apples = 1;\nconst oranges = 2;\nassert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);\n// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2\n\nassert.strictEqual(1, '1', new TypeError('Inputs are not identical'));\n// TypeError: Inputs are not identical\n</code></pre>\n<p>If the values are not strictly equal, an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a> is thrown with a\n<code>message</code> property set equal to the value of the <code>message</code> parameter. If the\n<code>message</code> parameter is undefined, a default error message is assigned. If the\n<code>message</code> parameter is an instance of an <a href=\"errors.html#class-error\"><code>Error</code></a> then it will be thrown\ninstead of the <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>" 1205 }, 1206 { 1207 "textRaw": "`assert.throws(fn[, error][, message])`", 1208 "type": "method", 1209 "name": "throws", 1210 "meta": { 1211 "added": [ 1212 "v0.1.21" 1213 ], 1214 "changes": [ 1215 { 1216 "version": "v10.2.0", 1217 "pr-url": "https://github.com/nodejs/node/pull/20485", 1218 "description": "The `error` parameter can be an object containing regular expressions now." 1219 }, 1220 { 1221 "version": "v9.9.0", 1222 "pr-url": "https://github.com/nodejs/node/pull/17584", 1223 "description": "The `error` parameter can now be an object as well." 1224 }, 1225 { 1226 "version": "v4.2.0", 1227 "pr-url": "https://github.com/nodejs/node/pull/3276", 1228 "description": "The `error` parameter can now be an arrow function." 1229 } 1230 ] 1231 }, 1232 "signatures": [ 1233 { 1234 "params": [ 1235 { 1236 "textRaw": "`fn` {Function}", 1237 "name": "fn", 1238 "type": "Function" 1239 }, 1240 { 1241 "textRaw": "`error` {RegExp|Function|Object|Error}", 1242 "name": "error", 1243 "type": "RegExp|Function|Object|Error" 1244 }, 1245 { 1246 "textRaw": "`message` {string}", 1247 "name": "message", 1248 "type": "string" 1249 } 1250 ] 1251 } 1252 ], 1253 "desc": "<p>Expects the function <code>fn</code> to throw an error.</p>\n<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,\na validation object where each property will be tested for strict deep equality,\nor an instance of error where each property will be tested for strict deep\nequality including the non-enumerable <code>message</code> and <code>name</code> properties. When\nusing an object, it is also possible to use a regular expression, when\nvalidating against a string property. See below for examples.</p>\n<p>If specified, <code>message</code> will be appended to the message provided by the\n<code>AssertionError</code> if the <code>fn</code> call fails to throw or in case the error validation\nfails.</p>\n<p>Custom validation object/error instance:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nconst err = new TypeError('Wrong value');\nerr.code = 404;\nerr.foo = 'bar';\nerr.info = {\n nested: true,\n baz: 'text',\n};\nerr.reg = /abc/i;\n\nassert.throws(\n () => {\n throw err;\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n info: {\n nested: true,\n baz: 'text',\n },\n // Only properties on the validation object will be tested for.\n // Using nested objects requires all properties to be present. Otherwise\n // the validation is going to fail.\n },\n);\n\n// Using regular expressions to validate error properties:\nassert.throws(\n () => {\n throw err;\n },\n {\n // The `name` and `message` properties are strings and using regular\n // expressions on those will match against the string. If they fail, an\n // error is thrown.\n name: /^TypeError$/,\n message: /Wrong/,\n foo: 'bar',\n info: {\n nested: true,\n // It is not possible to use regular expressions for nested properties!\n baz: 'text',\n },\n // The `reg` property contains a regular expression and only if the\n // validation object contains an identical regular expression, it is going\n // to pass.\n reg: /abc/i,\n },\n);\n\n// Fails due to the different `message` and `name` properties:\nassert.throws(\n () => {\n const otherErr = new Error('Not found');\n // Copy all enumerable properties from `err` to `otherErr`.\n for (const [key, value] of Object.entries(err)) {\n otherErr[key] = value;\n }\n throw otherErr;\n },\n // The error's `message` and `name` properties will also be checked when using\n // an error as validation object.\n err,\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nconst err = new TypeError('Wrong value');\nerr.code = 404;\nerr.foo = 'bar';\nerr.info = {\n nested: true,\n baz: 'text',\n};\nerr.reg = /abc/i;\n\nassert.throws(\n () => {\n throw err;\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n info: {\n nested: true,\n baz: 'text',\n },\n // Only properties on the validation object will be tested for.\n // Using nested objects requires all properties to be present. Otherwise\n // the validation is going to fail.\n },\n);\n\n// Using regular expressions to validate error properties:\nassert.throws(\n () => {\n throw err;\n },\n {\n // The `name` and `message` properties are strings and using regular\n // expressions on those will match against the string. If they fail, an\n // error is thrown.\n name: /^TypeError$/,\n message: /Wrong/,\n foo: 'bar',\n info: {\n nested: true,\n // It is not possible to use regular expressions for nested properties!\n baz: 'text',\n },\n // The `reg` property contains a regular expression and only if the\n // validation object contains an identical regular expression, it is going\n // to pass.\n reg: /abc/i,\n },\n);\n\n// Fails due to the different `message` and `name` properties:\nassert.throws(\n () => {\n const otherErr = new Error('Not found');\n // Copy all enumerable properties from `err` to `otherErr`.\n for (const [key, value] of Object.entries(err)) {\n otherErr[key] = value;\n }\n throw otherErr;\n },\n // The error's `message` and `name` properties will also be checked when using\n // an error as validation object.\n err,\n);\n</code></pre>\n<p>Validate instanceof using constructor:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n Error,\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n Error,\n);\n</code></pre>\n<p>Validate error message using <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a>:</p>\n<p>Using a regular expression runs <code>.toString</code> on the error object, and will\ntherefore also include the error name.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n /^Error: Wrong value$/,\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n /^Error: Wrong value$/,\n);\n</code></pre>\n<p>Custom error validation:</p>\n<p>The function must return <code>true</code> to indicate all internal validations passed.\nIt will otherwise fail with an <a href=\"#class-assertassertionerror\"><code>AssertionError</code></a>.</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n (err) => {\n assert(err instanceof Error);\n assert(/value/.test(err));\n // Avoid returning anything from validation functions besides `true`.\n // Otherwise, it's not clear what part of the validation failed. Instead,\n // throw an error about the specific validation that failed (as done in this\n // example) and add as much helpful debugging information to that error as\n // possible.\n return true;\n },\n 'unexpected error',\n);\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n (err) => {\n assert(err instanceof Error);\n assert(/value/.test(err));\n // Avoid returning anything from validation functions besides `true`.\n // Otherwise, it's not clear what part of the validation failed. Instead,\n // throw an error about the specific validation that failed (as done in this\n // example) and add as much helpful debugging information to that error as\n // possible.\n return true;\n },\n 'unexpected error',\n);\n</code></pre>\n<p><code>error</code> cannot be a string. If a string is provided as the second\nargument, then <code>error</code> is assumed to be omitted and the string will be used for\n<code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same\nmessage as the thrown error message is going to result in an\n<code>ERR_AMBIGUOUS_ARGUMENT</code> error. Please read the example below carefully if using\na string as the second argument gets considered:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert/strict';\n\nfunction throwingFirst() {\n throw new Error('First');\n}\n\nfunction throwingSecond() {\n throw new Error('Second');\n}\n\nfunction notThrowing() {}\n\n// The second argument is a string and the input function threw an Error.\n// The first case will not throw as it does not match for the error message\n// thrown by the input function!\nassert.throws(throwingFirst, 'Second');\n// In the next example the message has no benefit over the message from the\n// error and since it is not clear if the user intended to actually match\n// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.\nassert.throws(throwingSecond, 'Second');\n// TypeError [ERR_AMBIGUOUS_ARGUMENT]\n\n// The string is only used (as message) in case the function does not throw:\nassert.throws(notThrowing, 'Second');\n// AssertionError [ERR_ASSERTION]: Missing expected exception: Second\n\n// If it was intended to match for the error message do this instead:\n// It does not throw because the error messages match.\nassert.throws(throwingSecond, /Second$/);\n\n// If the error message does not match, an AssertionError is thrown.\nassert.throws(throwingFirst, /Second$/);\n// AssertionError [ERR_ASSERTION]\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert/strict');\n\nfunction throwingFirst() {\n throw new Error('First');\n}\n\nfunction throwingSecond() {\n throw new Error('Second');\n}\n\nfunction notThrowing() {}\n\n// The second argument is a string and the input function threw an Error.\n// The first case will not throw as it does not match for the error message\n// thrown by the input function!\nassert.throws(throwingFirst, 'Second');\n// In the next example the message has no benefit over the message from the\n// error and since it is not clear if the user intended to actually match\n// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.\nassert.throws(throwingSecond, 'Second');\n// TypeError [ERR_AMBIGUOUS_ARGUMENT]\n\n// The string is only used (as message) in case the function does not throw:\nassert.throws(notThrowing, 'Second');\n// AssertionError [ERR_ASSERTION]: Missing expected exception: Second\n\n// If it was intended to match for the error message do this instead:\n// It does not throw because the error messages match.\nassert.throws(throwingSecond, /Second$/);\n\n// If the error message does not match, an AssertionError is thrown.\nassert.throws(throwingFirst, /Second$/);\n// AssertionError [ERR_ASSERTION]\n</code></pre>\n<p>Due to the confusing error-prone notation, avoid a string as the second\nargument.</p>" 1254 } 1255 ], 1256 "type": "module", 1257 "displayName": "Assert" 1258 } 1259 ] 1260}