CasperSecurity

Current Path : /usr/share/doc/nodejs/api/
Upload File :
Current File : //usr/share/doc/nodejs/api/assert.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <meta name="nodejs.org:node-version" content="v12.22.9">
  <title>Assert | Node.js v12.22.9 Documentation</title>
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/hljs.css">
  <link rel="canonical" href="assert.html">
</head>
<body class="alt apidoc" id="api-section-assert">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">About this documentation</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage and example</a></li>
</ul>
<hr class="line"/>

<ul>
<li><a class="nav-assert active" href="assert.html">Assertion testing</a></li>
<li><a class="nav-async_hooks" href="async_hooks.html">Async hooks</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C++ Addons</a></li>
<li><a class="nav-n-api" href="n-api.html">C/C++ Addons with N-API</a></li>
<li><a class="nav-embedding" href="embedding.html">C++ Embedder API</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command line options</a></li>
<li><a class="nav-console" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-deprecations" href="deprecations.html">Deprecated APIs</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File system</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-http2" href="http2.html">HTTP/2</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-inspector" href="inspector.html">Inspector</a></li>
<li><a class="nav-intl" href="intl.html">Internationalization</a></li>
<li><a class="nav-modules" href="modules.html">Modules: CommonJS modules</a></li>
<li><a class="nav-esm" href="esm.html">Modules: ECMAScript modules</a></li>
<li><a class="nav-module" href="module.html">Modules: <code>module</code> API</a></li>
<li><a class="nav-packages" href="packages.html">Modules: Packages</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-perf_hooks" href="perf_hooks.html">Performance hooks</a></li>
<li><a class="nav-policy" href="policy.html">Policies</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-report" href="report.html">Report</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tracing" href="tracing.html">Trace events</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-wasi" href="wasi.html">WASI</a></li>
<li><a class="nav-worker_threads" href="worker_threads.html">Worker threads</a></li>
<li><a class="nav-zlib" href="zlib.html">Zlib</a></li>
</ul>
<hr class="line"/>

<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">Code repository and issue tracker</a></li>
</ul>

    </div>

    <div id="column1" data-id="assert" class="interior">
      <header>
        <h1>Node.js v12.22.9 Documentation</h1>
        <div id="gtoc">
          <ul>
            <li>
              <a href="index.html">Index</a>
            </li>
            <li>
              <a href="all.html">View on single page</a>
            </li>
            <li>
              <a href="assert.json">View as JSON</a>
            </li>
            
            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/assert.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
          </ul>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>Table of Contents</h2>
        <ul>
<li><a href="#assert_assert">Assert</a><ul>
<li><a href="#assert_strict_assertion_mode">Strict assertion mode</a></li>
<li><a href="#assert_legacy_assertion_mode">Legacy assertion mode</a></li>
<li><a href="#assert_class_assert_assertionerror">Class: assert.AssertionError</a><ul>
<li><a href="#assert_new_assert_assertionerror_options"><code>new assert.AssertionError(options)</code></a></li>
</ul>
</li>
<li><a href="#assert_class_assert_calltracker">Class: <code>assert.CallTracker</code></a><ul>
<li><a href="#assert_new_assert_calltracker"><code>new assert.CallTracker()</code></a></li>
<li><a href="#assert_tracker_calls_fn_exact"><code>tracker.calls([fn][, exact])</code></a></li>
<li><a href="#assert_tracker_report"><code>tracker.report()</code></a></li>
<li><a href="#assert_tracker_verify"><code>tracker.verify()</code></a></li>
</ul>
</li>
<li><a href="#assert_assert_value_message"><code>assert(value[, message])</code></a></li>
<li><a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual(actual, expected[, message])</code></a><ul>
<li><a href="#assert_comparison_details">Comparison details</a></li>
</ul>
</li>
<li><a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual(actual, expected[, message])</code></a><ul>
<li><a href="#assert_comparison_details_1">Comparison details</a></li>
</ul>
</li>
<li><a href="#assert_assert_doesnotmatch_string_regexp_message"><code>assert.doesNotMatch(string, regexp[, message])</code></a></li>
<li><a href="#assert_assert_doesnotreject_asyncfn_error_message"><code>assert.doesNotReject(asyncFn[, error][, message])</code></a></li>
<li><a href="#assert_assert_doesnotthrow_fn_error_message"><code>assert.doesNotThrow(fn[, error][, message])</code></a></li>
<li><a href="#assert_assert_equal_actual_expected_message"><code>assert.equal(actual, expected[, message])</code></a></li>
<li><a href="#assert_assert_fail_message"><code>assert.fail([message])</code></a></li>
<li><a href="#assert_assert_fail_actual_expected_message_operator_stackstartfn"><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code></a></li>
<li><a href="#assert_assert_iferror_value"><code>assert.ifError(value)</code></a></li>
<li><a href="#assert_assert_match_string_regexp_message"><code>assert.match(string, regexp[, message])</code></a></li>
<li><a href="#assert_assert_notdeepequal_actual_expected_message"><code>assert.notDeepEqual(actual, expected[, message])</code></a></li>
<li><a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual(actual, expected[, message])</code></a></li>
<li><a href="#assert_assert_notequal_actual_expected_message"><code>assert.notEqual(actual, expected[, message])</code></a></li>
<li><a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual(actual, expected[, message])</code></a></li>
<li><a href="#assert_assert_ok_value_message"><code>assert.ok(value[, message])</code></a></li>
<li><a href="#assert_assert_rejects_asyncfn_error_message"><code>assert.rejects(asyncFn[, error][, message])</code></a></li>
<li><a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual(actual, expected[, message])</code></a></li>
<li><a href="#assert_assert_throws_fn_error_message"><code>assert.throws(fn[, error][, message])</code></a></li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Assert<span><a class="mark" href="#assert_assert" id="assert_assert">#</a></span></h1>
<!--introduced_in=v0.1.21-->

<blockquote>
<p>Stability: 2 - Stable</p>
</blockquote>
<!-- source_link=lib/assert.js -->

<p>The <code>assert</code> module provides a set of assertion functions for verifying
invariants.</p>
<h2>Strict assertion mode<span><a class="mark" href="#assert_strict_assertion_mode" id="assert_strict_assertion_mode">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v12.16.2</td>
<td><p>Changed &quot;strict mode&quot; to &quot;strict assertion mode&quot; and &quot;legacy mode&quot; to &quot;legacy assertion mode&quot; to avoid confusion with the more usual meaning of &quot;strict mode&quot;.</p>
</td></tr>
<tr><td>v9.9.0</td>
<td><p>Added error diffs to the strict assertion mode.</p>
</td></tr>
<tr><td>v9.9.0</td>
<td><p>Added strict assertion mode to the assert module.</p>
</td></tr>
<tr><td>v9.9.0</td>
<td><p><span>Added in: v9.9.0</span></p>
</td></tr>
</table>
</details>
</div><p>In strict assertion mode, non-strict methods behave like their corresponding
strict methods. For example, <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> will behave like
<a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p>
<p>In strict assertion mode, error messages for objects display a diff. In legacy
assertion mode, error messages for objects display the objects, often truncated.</p>
<p>To use strict assertion mode:</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;
</code></pre>
<p>Example error diff:</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, &#39;3&#39;]], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
//   [
//     [
// ...
//       2,
// +     3
// -     &#39;3&#39;
//     ],
// ...
//     5
//   ]
</code></pre>
<p>To deactivate the colors, use the <code>NO_COLOR</code> or <code>NODE_DISABLE_COLORS</code>
environment variables. This will also deactivate the colors in the REPL. For
more on color support in terminal environments, read the tty
<a href="tty.html#tty_writestream_getcolordepth_env">getColorDepth()</a> documentation.</p>
<h2>Legacy assertion mode<span><a class="mark" href="#assert_legacy_assertion_mode" id="assert_legacy_assertion_mode">#</a></span></h2>
<p>Legacy assertion mode uses the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> in:</p>
<ul>
<li><a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a></li>
<li><a href="#assert_assert_equal_actual_expected_message"><code>assert.equal()</code></a></li>
<li><a href="#assert_assert_notdeepequal_actual_expected_message"><code>assert.notDeepEqual()</code></a></li>
<li><a href="#assert_assert_notequal_actual_expected_message"><code>assert.notEqual()</code></a></li>
</ul>
<p>To use legacy assertion mode:</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);
</code></pre>
<p>Whenever possible, use the <a href="#assert_strict_assertion_mode">strict assertion mode</a> instead. Otherwise, the
<a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> may cause surprising results. This is
especially true for <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>, where the comparison rules are
lax:</p>
<pre><code class="language-js">// WARNING: This does not throw an AssertionError!
assert.deepEqual(/a/gi, new Date());
</code></pre>
<h2>Class: assert.AssertionError<span><a class="mark" href="#assert_class_assert_assertionerror" id="assert_class_assert_assertionerror">#</a></span></h2>
<ul>
<li>Extends: {errors.Error}</li>
</ul>
<p>Indicates the failure of an assertion. All errors thrown by the <code>assert</code> module
will be instances of the <code>AssertionError</code> class.</p>
<h3><code>new assert.AssertionError(options)</code><span><a class="mark" href="#assert_new_assert_assertionerror_options" id="assert_new_assert_assertionerror_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div><ul>
<li><code>options</code> {Object}<ul>
<li><code>message</code> {string} If provided, the error message is set to this value.</li>
<li><code>actual</code> {any} The <code>actual</code> property on the error instance.</li>
<li><code>expected</code> {any} The <code>expected</code> property on the error instance.</li>
<li><code>operator</code> {string} The <code>operator</code> property on the error instance.</li>
<li><code>stackStartFn</code> {Function} If provided, the generated stack trace omits
frames before this function.</li>
</ul>
</li>
</ul>
<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p>
<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>)
and:</p>
<ul>
<li><code>actual</code> {any} Set to the <code>actual</code> argument for methods such as
<a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</li>
<li><code>expected</code> {any} Set to the <code>expected</code> value for methods such as
<a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</li>
<li><code>generatedMessage</code> {boolean} Indicates if the message was auto-generated
(<code>true</code>) or not.</li>
<li><code>code</code> {string} Value is always <code>ERR_ASSERTION</code> to show that the error is an
assertion error.</li>
<li><code>operator</code> {string} Set to the passed in operator value.</li>
</ul>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

// Generate an AssertionError to compare the error message later:
const { message } = new assert.AssertionError({
  actual: 1,
  expected: 2,
  operator: &#39;strictEqual&#39;
});

// Verify error output:
try {
  assert.strictEqual(1, 2);
} catch (err) {
  assert(err instanceof assert.AssertionError);
  assert.strictEqual(err.message, message);
  assert.strictEqual(err.name, &#39;AssertionError&#39;);
  assert.strictEqual(err.actual, 1);
  assert.strictEqual(err.expected, 2);
  assert.strictEqual(err.code, &#39;ERR_ASSERTION&#39;);
  assert.strictEqual(err.operator, &#39;strictEqual&#39;);
  assert.strictEqual(err.generatedMessage, true);
}
</code></pre>
<h2>Class: <code>assert.CallTracker</code><span><a class="mark" href="#assert_class_assert_calltracker" id="assert_class_assert_calltracker">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v12.19.0</span>
</div><blockquote>
<p>Stability: 1 - Experimental</p>
</blockquote>
<p>This feature is currently experimental and behavior might still change.</p>
<h3><code>new assert.CallTracker()</code><span><a class="mark" href="#assert_new_assert_calltracker" id="assert_new_assert_calltracker">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v12.19.0</span>
</div><p>Creates a new <a href="#assert_class_assert_calltracker"><code>CallTracker</code></a> object which can be used to track if functions
were called a specific number of times. The <code>tracker.verify()</code> must be called
for the verification to take place. The usual pattern would be to call it in a
<a href="process.html#process_event_exit"><code>process.on(&#39;exit&#39;)</code></a> handler.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

const tracker = new assert.CallTracker();

function func() {}

// callsfunc() must be called exactly 1 time before tracker.verify().
const callsfunc = tracker.calls(func, 1);

callsfunc();

// Calls tracker.verify() and verifies if all tracker.calls() functions have
// been called exact times.
process.on(&#39;exit&#39;, () =&gt; {
  tracker.verify();
});
</code></pre>
<h3><code>tracker.calls([fn][, exact])</code><span><a class="mark" href="#assert_tracker_calls_fn_exact" id="assert_tracker_calls_fn_exact">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v12.19.0</span>
</div><ul>
<li><code>fn</code> {Function} <strong>Default</strong> A no-op function.</li>
<li><code>exact</code> {number} <strong>Default</strong> <code>1</code>.</li>
<li>Returns: {Function} that wraps <code>fn</code>.</li>
</ul>
<p>The wrapper function is expected to be called exactly <code>exact</code> times. If the
function has not been called exactly <code>exact</code> times when
<a href="#assert_tracker_verify"><code>tracker.verify()</code></a> is called, then <a href="#assert_tracker_verify"><code>tracker.verify()</code></a> will throw an
error.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

// Creates call tracker.
const tracker = new assert.CallTracker();

function func() {}

// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func);
</code></pre>
<h3><code>tracker.report()</code><span><a class="mark" href="#assert_tracker_report" id="assert_tracker_report">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v12.19.0</span>
</div><ul>
<li>Returns: {Array} of objects containing information about the wrapper functions
returned by <a href="#assert_tracker_calls_fn_exact"><code>tracker.calls()</code></a>.</li>
<li>Object {Object}<ul>
<li><code>message</code> {string}</li>
<li><code>actual</code> {number} The actual number of times the function was called.</li>
<li><code>expected</code> {number} The number of times the function was expected to be
called.</li>
<li><code>operator</code> {string} The name of the function that is wrapped.</li>
<li><code>stack</code> {Object} A stack trace of the function.</li>
</ul>
</li>
</ul>
<p>The arrays contains information about the expected and actual number of calls of
the functions that have not been called the expected number of times.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

// Creates call tracker.
const tracker = new assert.CallTracker();

function func() {}

function foo() {}

// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);

// Returns an array containing information on callsfunc()
tracker.report();
// [
//  {
//    message: &#39;Expected the func function to be executed 2 time(s) but was
//    executed 0 time(s).&#39;,
//    actual: 0,
//    expected: 2,
//    operator: &#39;func&#39;,
//    stack: stack trace
//  }
// ]
</code></pre>
<h3><code>tracker.verify()</code><span><a class="mark" href="#assert_tracker_verify" id="assert_tracker_verify">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v12.19.0</span>
</div><p>Iterates through the list of functions passed to
<a href="#assert_tracker_calls_fn_exact"><code>tracker.calls()</code></a> and will throw an error for functions that
have not been called the expected number of times.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

// Creates call tracker.
const tracker = new assert.CallTracker();

function func() {}

// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);

callsfunc();

// Will throw an error since callsfunc() was only called once.
tracker.verify();
</code></pre>
<h2><code>assert(value[, message])</code><span><a class="mark" href="#assert_assert_value_message" id="assert_assert_value_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div><ul>
<li><code>value</code> {any} The input that is checked for being truthy.</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p>An alias of <a href="#assert_assert_ok_value_message"><code>assert.ok()</code></a>.</p>
<h2><code>assert.deepEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_deepequal_actual_expected_message" id="assert_assert_deepequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v12.0.0</td>
<td><p>The type tags are now properly compared and there are a couple minor comparison adjustments to make the check less surprising.</p>
</td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p>
</td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p>
</td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p>
</td></tr>
<tr><td>v6.1.0, v4.5.0</td>
<td><p>Objects with circular references can be used as inputs now.</p>
</td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p><strong>Strict assertion mode</strong></p>
<p>An alias of <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p>
<p><strong>Legacy assertion mode</strong></p>
<blockquote>
<p>Stability: 0 - Deprecated: Use <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> instead.</p>
</blockquote>
<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters. Consider
using <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> instead. <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> can have
surprising results.</p>
<p><em>Deep equality</em> means that the enumerable &quot;own&quot; properties of child objects
are also recursively evaluated by the following rules.</p>
<h3>Comparison details<span><a class="mark" href="#assert_comparison_details" id="assert_comparison_details">#</a></span></h3>
<ul>
<li>Primitive values are compared with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a>
( <code>==</code> ).</li>
<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li>
<li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable &quot;own&quot; properties</a> are considered.</li>
<li><a href="errors.html#errors_class_error"><code>Error</code></a> names and messages are always compared, even if these are not
enumerable properties.</li>
<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>
<li><code>Object</code> properties are compared unordered.</li>
<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>
<li>Recursion stops when both sides differ or both sides encounter a circular
reference.</li>
<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
objects.</li>
<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>
<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>
</ul>
<p>The following example does not throw an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> because the
primitives are considered equal by the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a>
( <code>==</code> ).</p>
<pre><code class="language-js">// WARNING: This does not throw an AssertionError!
assert.deepEqual(&#39;+00000000&#39;, false);
</code></pre>
<p>&quot;Deep&quot; equality means that the enumerable &quot;own&quot; properties of child objects
are evaluated also:</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

const obj1 = {
  a: {
    b: 1
  }
};
const obj2 = {
  a: {
    b: 2
  }
};
const obj3 = {
  a: {
    b: 1
  }
};
const obj4 = Object.create(obj1);

assert.deepEqual(obj1, obj1);
// OK

// Values of b are different:
assert.deepEqual(obj1, obj2);
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }

assert.deepEqual(obj1, obj3);
// OK

// Prototypes are ignored:
assert.deepEqual(obj1, obj4);
// AssertionError: { a: { b: 1 } } deepEqual {}
</code></pre>
<p>If the values are not equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<h2><code>assert.deepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_deepstrictequal_actual_expected_message" id="assert_assert_deepstrictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>Enumerable symbol properties are now compared.</p>
</td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-samevaluezero">SameValueZero</a> comparison.</p>
</td></tr>
<tr><td>v8.5.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p>
</td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p>
</td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p>
</td></tr>
<tr><td>v6.1.0</td>
<td><p>Objects with circular references can be used as inputs now.</p>
</td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p>
</td></tr>
<tr><td>v1.2.0</td>
<td><p><span>Added in: v1.2.0</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.
&quot;Deep&quot; equality means that the enumerable &quot;own&quot; properties of child objects
are recursively evaluated also by the following rules.</p>
<h3>Comparison details<span><a class="mark" href="#assert_comparison_details_1" id="assert_comparison_details_1">#</a></span></h3>
<ul>
<li>Primitive values are compared using the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>, used by
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</li>
<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li>
<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
the <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Strict Equality Comparison</a>.</li>
<li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable &quot;own&quot; properties</a> are considered.</li>
<li><a href="errors.html#errors_class_error"><code>Error</code></a> names and messages are always compared, even if these are not
enumerable properties.</li>
<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>
<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>
<li><code>Object</code> properties are compared unordered.</li>
<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>
<li>Recursion stops when both sides differ or both sides encounter a circular
reference.</li>
<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
below for further details.</li>
</ul>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

// This fails because 1 !== &#39;1&#39;.
assert.deepStrictEqual({ a: 1 }, { a: &#39;1&#39; });
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
//   {
// +   a: 1
// -   a: &#39;1&#39;
//   }

// The following objects don&#39;t have own properties
const date = new Date();
const object = {};
const fakeDate = {};
Object.setPrototypeOf(fakeDate, Date.prototype);

// Different [[Prototype]]:
assert.deepStrictEqual(object, fakeDate);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + {}
// - Date {}

// Different type tags:
assert.deepStrictEqual(date, fakeDate);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + 2018-04-26T00:49:08.604Z
// - Date {}

assert.deepStrictEqual(NaN, NaN);
// OK, because of the SameValue comparison

// Different unwrapped numbers:
assert.deepStrictEqual(new Number(1), new Number(2));
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + [Number: 1]
// - [Number: 2]

assert.deepStrictEqual(new String(&#39;foo&#39;), Object(&#39;foo&#39;));
// OK because the object and the string are identical when unwrapped.

assert.deepStrictEqual(-0, -0);
// OK

// Different zeros using the SameValue Comparison:
assert.deepStrictEqual(0, -0);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + 0
// - -0

const symbol1 = Symbol();
const symbol2 = Symbol();
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
// OK, because it is the same symbol on both objects.

assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
//
// {
//   [Symbol()]: 1
// }

const weakMap1 = new WeakMap();
const weakMap2 = new WeakMap([[{}, {}]]);
const weakMap3 = new WeakMap();
weakMap3.unequal = true;

assert.deepStrictEqual(weakMap1, weakMap2);
// OK, because it is impossible to compare the entries

// Fails because weakMap3 has a property that weakMap1 does not contain:
assert.deepStrictEqual(weakMap1, weakMap3);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
//   WeakMap {
// +   [items unknown]
// -   [items unknown],
// -   unequal: true
//   }
</code></pre>
<p>If the values are not equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2><code>assert.doesNotMatch(string, regexp[, message])</code><span><a class="mark" href="#assert_assert_doesnotmatch_string_regexp_message" id="assert_assert_doesnotmatch_string_regexp_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v12.16.0</span>
</div><ul>
<li><code>string</code> {string}</li>
<li><code>regexp</code> {RegExp}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<blockquote>
<p>Stability: 1 - Experimental</p>
</blockquote>
<p>Expects the <code>string</code> input not to match the regular expression.</p>
<p>This feature is currently experimental and the name might change or it might be
completely removed again.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.doesNotMatch(&#39;I will fail&#39;, /fail/);
// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...

assert.doesNotMatch(123, /pass/);
// AssertionError [ERR_ASSERTION]: The &quot;string&quot; argument must be of type string.

assert.doesNotMatch(&#39;I will pass&#39;, /different/);
// OK
</code></pre>
<p>If the values do match, or if the <code>string</code> argument is of another type than
<code>string</code>, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal
to the value of the <code>message</code> parameter. If the <code>message</code> parameter is
undefined, a default error message is assigned. If the <code>message</code> parameter is an
instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<h2><code>assert.doesNotReject(asyncFn[, error][, message])</code><span><a class="mark" href="#assert_assert_doesnotreject_asyncfn_error_message" id="assert_assert_doesnotreject_asyncfn_error_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div><ul>
<li><code>asyncFn</code> {Function|Promise}</li>
<li><code>error</code> {RegExp|Function}</li>
<li><code>message</code> {string}</li>
</ul>
<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately
calls the function and awaits the returned promise to complete. It will then
check that the promise is not rejected.</p>
<p>If <code>asyncFn</code> is a function and it throws an error synchronously,
<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If
the function does not return a promise, <code>assert.doesNotReject()</code> will return a
rejected <code>Promise</code> with an <a href="errors.html#errors_err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases
the error handler is skipped.</p>
<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little
benefit in catching a rejection and then rejecting it again. Instead, consider
adding a comment next to the specific code path that should not reject and keep
error messages as expressive as possible.</p>
<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
function. See <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> for more details.</p>
<p>Besides the async nature to await the completion behaves identically to
<a href="#assert_assert_doesnotthrow_fn_error_message"><code>assert.doesNotThrow()</code></a>.</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">(async () =&gt; {
  await assert.doesNotReject(
    async () =&gt; {
      throw new TypeError(&#39;Wrong value&#39;);
    },
    SyntaxError
  );
})();
</code></pre>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotReject(Promise.reject(new TypeError(&#39;Wrong value&#39;)))
  .then(() =&gt; {
    // ...
  });
</code></pre>
<h2><code>assert.doesNotThrow(fn[, error][, message])</code><span><a class="mark" href="#assert_assert_doesnotthrow_fn_error_message" id="assert_assert_doesnotthrow_fn_error_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v5.11.0, v4.4.5</td>
<td><p>The <code>message</code> parameter is respected now.</p>
</td></tr>
<tr><td>v4.2.0</td>
<td><p>The <code>error</code> parameter can now be an arrow function.</p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>fn</code> {Function}</li>
<li><code>error</code> {RegExp|Function}</li>
<li><code>message</code> {string}</li>
</ul>
<p>Asserts that the function <code>fn</code> does not throw an error.</p>
<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there
is no benefit in catching an error and then rethrowing it. Instead, consider
adding a comment next to the specific code path that should not throw and keep
error messages as expressive as possible.</p>
<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code>
function.</p>
<p>If an error is thrown and it is the same type as that specified by the <code>error</code>
parameter, then an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown. If the error is of a
different type, or if the <code>error</code> parameter is undefined, the error is
propagated back to the caller.</p>
<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
function. See <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> for more details.</p>
<p>The following, for instance, will throw the <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> because there is no
matching error type in the assertion:</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotThrow(
  () =&gt; {
    throw new TypeError(&#39;Wrong value&#39;);
  },
  SyntaxError
);
</code></pre>
<p>However, the following will result in an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> with the message
&#39;Got unwanted exception...&#39;:</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotThrow(
  () =&gt; {
    throw new TypeError(&#39;Wrong value&#39;);
  },
  TypeError
);
</code></pre>
<p>If an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown and a value is provided for the <code>message</code>
parameter, the value of <code>message</code> will be appended to the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>
message:</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotThrow(
  () =&gt; {
    throw new TypeError(&#39;Wrong value&#39;);
  },
  /Wrong value/,
  &#39;Whoops&#39;
);
// Throws: AssertionError: Got unwanted exception: Whoops
</code></pre>
<h2><code>assert.equal(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_equal_actual_expected_message" id="assert_assert_equal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p><strong>Strict assertion mode</strong></p>
<p>An alias of <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</p>
<p><strong>Legacy assertion mode</strong></p>
<blockquote>
<p>Stability: 0 - Deprecated: Use <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> instead.</p>
</blockquote>
<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters
using the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> ( <code>==</code> ).</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

assert.equal(1, 1);
// OK, 1 == 1
assert.equal(1, &#39;1&#39;);
// OK, 1 == &#39;1&#39;

assert.equal(1, 2);
// AssertionError: 1 == 2
assert.equal({ a: { b: 1 } }, { a: { b: 1 } });
// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
</code></pre>
<p>If the values are not equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2><code>assert.fail([message])</code><span><a class="mark" href="#assert_assert_fail_message" id="assert_assert_fail_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div><ul>
<li><code>message</code> {string|Error} <strong>Default:</strong> <code>&#39;Failed&#39;</code></li>
</ul>
<p>Throws an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> with the provided error message or a default
error message. If the <code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then
it will be thrown instead of the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.fail();
// AssertionError [ERR_ASSERTION]: Failed

assert.fail(&#39;boom&#39;);
// AssertionError [ERR_ASSERTION]: boom

assert.fail(new TypeError(&#39;need array&#39;));
// TypeError: need array
</code></pre>
<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated.
See below for further details.</p>
<h2><code>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</code><span><a class="mark" href="#assert_assert_fail_actual_expected_message_operator_stackstartfn" id="assert_assert_fail_actual_expected_message_operator_stackstartfn">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Calling <code>assert.fail()</code> with more than one argument is deprecated and emits a warning.</p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><blockquote>
<p>Stability: 0 - Deprecated: Use <code>assert.fail([message])</code> or other assert
functions instead.</p>
</blockquote>
<ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
<li><code>operator</code> {string} <strong>Default:</strong> <code>&#39;!=&#39;</code></li>
<li><code>stackStartFn</code> {Function} <strong>Default:</strong> <code>assert.fail</code></li>
</ul>
<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and
<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and
<code>expected</code> arguments are provided, <code>operator</code> will default to <code>&#39;!=&#39;</code>. If
<code>message</code> is provided as third argument it will be used as the error message and
the other arguments will be stored as properties on the thrown object. If
<code>stackStartFn</code> is provided, all stack frames above that function will be
removed from stacktrace (see <a href="errors.html#errors_error_capturestacktrace_targetobject_constructoropt"><code>Error.captureStackTrace</code></a>). If no arguments are
given, the default message <code>Failed</code> will be used.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.fail(&#39;a&#39;, &#39;b&#39;);
// AssertionError [ERR_ASSERTION]: &#39;a&#39; != &#39;b&#39;

assert.fail(1, 2, undefined, &#39;&gt;&#39;);
// AssertionError [ERR_ASSERTION]: 1 &gt; 2

assert.fail(1, 2, &#39;fail&#39;);
// AssertionError [ERR_ASSERTION]: fail

assert.fail(1, 2, &#39;whoops&#39;, &#39;&gt;&#39;);
// AssertionError [ERR_ASSERTION]: whoops

assert.fail(1, 2, new TypeError(&#39;need array&#39;));
// TypeError: need array
</code></pre>
<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no
influence on the error message.</p>
<p>Example use of <code>stackStartFn</code> for truncating the exception&#39;s stacktrace:</p>
<pre><code class="language-js">function suppressFrame() {
  assert.fail(&#39;a&#39;, &#39;b&#39;, undefined, &#39;!==&#39;, suppressFrame);
}
suppressFrame();
// AssertionError [ERR_ASSERTION]: &#39;a&#39; !== &#39;b&#39;
//     at repl:1:1
//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)
//     ...
</code></pre>
<h2><code>assert.ifError(value)</code><span><a class="mark" href="#assert_assert_iferror_value" id="assert_assert_iferror_value">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Instead of throwing the original error it is now wrapped into an [<code>AssertionError</code>][] that contains the full stack trace.</p>
</td></tr>
<tr><td>v10.0.0</td>
<td><p>Value may now only be <code>undefined</code> or <code>null</code>. Before all falsy values were handled the same as <code>null</code> and did not throw.</p>
</td></tr>
<tr><td>v0.1.97</td>
<td><p><span>Added in: v0.1.97</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>value</code> {any}</li>
</ul>
<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when
testing the <code>error</code> argument in callbacks. The stack trace contains all frames
from the error passed to <code>ifError()</code> including the potential new frames for
<code>ifError()</code> itself.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.ifError(null);
// OK
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError(&#39;error&#39;);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: &#39;error&#39;
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error

// Create some random error frames.
let err;
(function errorFrame() {
  err = new Error(&#39;test error&#39;);
})();

(function ifErrorFrame() {
  assert.ifError(err);
})();
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
//     at ifErrorFrame
//     at errorFrame
</code></pre>
<h2><code>assert.match(string, regexp[, message])</code><span><a class="mark" href="#assert_assert_match_string_regexp_message" id="assert_assert_match_string_regexp_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v12.16.0</span>
</div><ul>
<li><code>string</code> {string}</li>
<li><code>regexp</code> {RegExp}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<blockquote>
<p>Stability: 1 - Experimental</p>
</blockquote>
<p>Expects the <code>string</code> input to match the regular expression.</p>
<p>This feature is currently experimental and the name might change or it might be
completely removed again.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.match(&#39;I will fail&#39;, /pass/);
// AssertionError [ERR_ASSERTION]: The input did not match the regular ...

assert.match(123, /pass/);
// AssertionError [ERR_ASSERTION]: The &quot;string&quot; argument must be of type string.

assert.match(&#39;I will pass&#39;, /pass/);
// OK
</code></pre>
<p>If the values do not match, or if the <code>string</code> argument is of another type than
<code>string</code>, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code> property set equal
to the value of the <code>message</code> parameter. If the <code>message</code> parameter is
undefined, a default error message is assigned. If the <code>message</code> parameter is an
instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<h2><code>assert.notDeepEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notdeepequal_actual_expected_message" id="assert_assert_notdeepequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p>
</td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p>
</td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p>
</td></tr>
<tr><td>v6.1.0, v4.5.0</td>
<td><p>Objects with circular references can be used as inputs now.</p>
</td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p><strong>Strict assertion mode</strong></p>
<p>An alias of <a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual()</code></a>.</p>
<p><strong>Legacy assertion mode</strong></p>
<blockquote>
<p>Stability: 0 - Deprecated: Use <a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual()</code></a> instead.</p>
</blockquote>
<p>Tests for any deep inequality. Opposite of <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

const obj1 = {
  a: {
    b: 1
  }
};
const obj2 = {
  a: {
    b: 2
  }
};
const obj3 = {
  a: {
    b: 1
  }
};
const obj4 = Object.create(obj1);

assert.notDeepEqual(obj1, obj1);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }

assert.notDeepEqual(obj1, obj2);
// OK

assert.notDeepEqual(obj1, obj3);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }

assert.notDeepEqual(obj1, obj4);
// OK
</code></pre>
<p>If the values are deeply equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a
<code>message</code> property set equal to the value of the <code>message</code> parameter. If the
<code>message</code> parameter is undefined, a default error message is assigned. If the
<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown
instead of the <code>AssertionError</code>.</p>
<h2><code>assert.notDeepStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notdeepstrictequal_actual_expected_message" id="assert_assert_notdeepstrictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p>
</td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-samevaluezero">SameValueZero</a> comparison.</p>
</td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p>
</td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p>
</td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p>
</td></tr>
<tr><td>v6.1.0</td>
<td><p>Objects with circular references can be used as inputs now.</p>
</td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p>
</td></tr>
<tr><td>v1.2.0</td>
<td><p><span>Added in: v1.2.0</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p>Tests for deep strict inequality. Opposite of <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.notDeepStrictEqual({ a: 1 }, { a: &#39;1&#39; });
// OK
</code></pre>
<p>If the values are deeply and strictly equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown
with a <code>message</code> property set equal to the value of the <code>message</code> parameter. If
the <code>message</code> parameter is undefined, a default error message is assigned. If
the <code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown
instead of the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<h2><code>assert.notEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notequal_actual_expected_message" id="assert_assert_notequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p><strong>Strict assertion mode</strong></p>
<p>An alias of <a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual()</code></a>.</p>
<p><strong>Legacy assertion mode</strong></p>
<blockquote>
<p>Stability: 0 - Deprecated: Use <a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual()</code></a> instead.</p>
</blockquote>
<p>Tests shallow, coercive inequality with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a>
( <code>!=</code> ).</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;);

assert.notEqual(1, 2);
// OK

assert.notEqual(1, 1);
// AssertionError: 1 != 1

assert.notEqual(1, &#39;1&#39;);
// AssertionError: 1 != &#39;1&#39;
</code></pre>
<p>If the values are equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2><code>assert.notStrictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_notstrictequal_actual_expected_message" id="assert_assert_notstrictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code></p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as
determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.notStrictEqual(1, 2);
// OK

assert.notStrictEqual(1, 1);
// AssertionError [ERR_ASSERTION]: Expected &quot;actual&quot; to be strictly unequal to:
//
// 1

assert.notStrictEqual(1, &#39;1&#39;);
// OK
</code></pre>
<p>If the values are strictly equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a
<code>message</code> property set equal to the value of the <code>message</code> parameter. If the
<code>message</code> parameter is undefined, a default error message is assigned. If the
<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown
instead of the <code>AssertionError</code>.</p>
<h2><code>assert.ok(value[, message])</code><span><a class="mark" href="#assert_assert_ok_value_message" id="assert_assert_ok_value_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>assert.ok()</code> (no arguments) will now use a predefined error message.</p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>value</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p>Tests if <code>value</code> is truthy. It is equivalent to
<code>assert.equal(!!value, true, message)</code>.</p>
<p>If <code>value</code> is not truthy, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.
If no arguments are passed in at all <code>message</code> will be set to the string:
<code>&#39;No value argument passed to `assert.ok()`&#39;</code>.</p>
<p>Be aware that in the <code>repl</code> the error message will be different to the one
thrown in a file! See below for further details.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.ok(true);
// OK
assert.ok(1);
// OK

assert.ok();
// AssertionError: No value argument passed to `assert.ok()`

assert.ok(false, &#39;it\&#39;s false&#39;);
// AssertionError: it&#39;s false

// In the repl:
assert.ok(typeof 123 === &#39;string&#39;);
// AssertionError: false == true

// In a file (e.g. test.js):
assert.ok(typeof 123 === &#39;string&#39;);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert.ok(typeof 123 === &#39;string&#39;)

assert.ok(false);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert.ok(false)

assert.ok(0);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert.ok(0)

// Using `assert()` works the same:
assert(0);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert(0)
</code></pre>
<h2><code>assert.rejects(asyncFn[, error][, message])</code><span><a class="mark" href="#assert_assert_rejects_asyncfn_error_message" id="assert_assert_rejects_asyncfn_error_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div><ul>
<li><code>asyncFn</code> {Function|Promise}</li>
<li><code>error</code> {RegExp|Function|Object|Error}</li>
<li><code>message</code> {string}</li>
</ul>
<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately
calls the function and awaits the returned promise to complete. It will then
check that the promise is rejected.</p>
<p>If <code>asyncFn</code> is a function and it throws an error synchronously,
<code>assert.rejects()</code> will return a rejected <code>Promise</code> with that error. If the
function does not return a promise, <code>assert.rejects()</code> will return a rejected
<code>Promise</code> with an <a href="errors.html#errors_err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases the error
handler is skipped.</p>
<p>Besides the async nature to await the completion behaves identically to
<a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a>.</p>
<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,
an object where each property will be tested for, or an instance of error where
each property will be tested for including the non-enumerable <code>message</code> and
<code>name</code> properties.</p>
<p>If specified, <code>message</code> will be the message provided by the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>
if the <code>asyncFn</code> fails to reject.</p>
<pre><code class="language-js">(async () =&gt; {
  await assert.rejects(
    async () =&gt; {
      throw new TypeError(&#39;Wrong value&#39;);
    },
    {
      name: &#39;TypeError&#39;,
      message: &#39;Wrong value&#39;
    }
  );
})();
</code></pre>
<pre><code class="language-js">(async () =&gt; {
  await assert.rejects(
    async () =&gt; {
      throw new TypeError(&#39;Wrong value&#39;);
    },
    (err) =&gt; {
      assert.strictEqual(err.name, &#39;TypeError&#39;);
      assert.strictEqual(err.message, &#39;Wrong value&#39;);
      return true;
    }
  );
})();
</code></pre>
<pre><code class="language-js">assert.rejects(
  Promise.reject(new Error(&#39;Wrong value&#39;)),
  Error
).then(() =&gt; {
  // ...
});
</code></pre>
<p><code>error</code> cannot be a string. If a string is provided as the second
argument, then <code>error</code> is assumed to be omitted and the string will be used for
<code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the
example in <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> carefully if using a string as the second
argument gets considered.</p>
<h2><code>assert.strictEqual(actual, expected[, message])</code><span><a class="mark" href="#assert_assert_strictequal_actual_expected_message" id="assert_assert_strictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code></p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>actual</code> {any}</li>
<li><code>expected</code> {any}</li>
<li><code>message</code> {string|Error}</li>
</ul>
<p>Tests strict equality between the <code>actual</code> and <code>expected</code> parameters as
determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.</p>
<pre><code class="language-js">const assert = require(&#39;assert&#39;).strict;

assert.strictEqual(1, 2);
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
//
// 1 !== 2

assert.strictEqual(1, 1);
// OK

assert.strictEqual(&#39;Hello foobar&#39;, &#39;Hello World!&#39;);
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
// + actual - expected
//
// + &#39;Hello foobar&#39;
// - &#39;Hello World!&#39;
//          ^

const apples = 1;
const oranges = 2;
assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2

assert.strictEqual(1, &#39;1&#39;, new TypeError(&#39;Inputs are not identical&#39;));
// TypeError: Inputs are not identical
</code></pre>
<p>If the values are not strictly equal, an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a> is thrown with a
<code>message</code> property set equal to the value of the <code>message</code> parameter. If the
<code>message</code> parameter is undefined, a default error message is assigned. If the
<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown
instead of the <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<h2><code>assert.throws(fn[, error][, message])</code><span><a class="mark" href="#assert_assert_throws_fn_error_message" id="assert_assert_throws_fn_error_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.2.0</td>
<td><p>The <code>error</code> parameter can be an object containing regular expressions now.</p>
</td></tr>
<tr><td>v9.9.0</td>
<td><p>The <code>error</code> parameter can now be an object as well.</p>
</td></tr>
<tr><td>v4.2.0</td>
<td><p>The <code>error</code> parameter can now be an arrow function.</p>
</td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p>
</td></tr>
</table>
</details>
</div><ul>
<li><code>fn</code> {Function}</li>
<li><code>error</code> {RegExp|Function|Object|Error}</li>
<li><code>message</code> {string}</li>
</ul>
<p>Expects the function <code>fn</code> to throw an error.</p>
<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,
a validation object where each property will be tested for strict deep equality,
or an instance of error where each property will be tested for strict deep
equality including the non-enumerable <code>message</code> and <code>name</code> properties. When
using an object, it is also possible to use a regular expression, when
validating against a string property. See below for examples.</p>
<p>If specified, <code>message</code> will be appended to the message provided by the
<code>AssertionError</code> if the <code>fn</code> call fails to throw or in case the error validation
fails.</p>
<p>Custom validation object/error instance:</p>
<pre><code class="language-js">const err = new TypeError(&#39;Wrong value&#39;);
err.code = 404;
err.foo = &#39;bar&#39;;
err.info = {
  nested: true,
  baz: &#39;text&#39;
};
err.reg = /abc/i;

assert.throws(
  () =&gt; {
    throw err;
  },
  {
    name: &#39;TypeError&#39;,
    message: &#39;Wrong value&#39;,
    info: {
      nested: true,
      baz: &#39;text&#39;
    }
    // Only properties on the validation object will be tested for.
    // Using nested objects requires all properties to be present. Otherwise
    // the validation is going to fail.
  }
);

// Using regular expressions to validate error properties:
assert.throws(
  () =&gt; {
    throw err;
  },
  {
    // The `name` and `message` properties are strings and using regular
    // expressions on those will match against the string. If they fail, an
    // error is thrown.
    name: /^TypeError$/,
    message: /Wrong/,
    foo: &#39;bar&#39;,
    info: {
      nested: true,
      // It is not possible to use regular expressions for nested properties!
      baz: &#39;text&#39;
    },
    // The `reg` property contains a regular expression and only if the
    // validation object contains an identical regular expression, it is going
    // to pass.
    reg: /abc/i
  }
);

// Fails due to the different `message` and `name` properties:
assert.throws(
  () =&gt; {
    const otherErr = new Error(&#39;Not found&#39;);
    // Copy all enumerable properties from `err` to `otherErr`.
    for (const [key, value] of Object.entries(err)) {
      otherErr[key] = value;
    }
    throw otherErr;
  },
  // The error&#39;s `message` and `name` properties will also be checked when using
  // an error as validation object.
  err
);
</code></pre>
<p>Validate instanceof using constructor:</p>
<pre><code class="language-js">assert.throws(
  () =&gt; {
    throw new Error(&#39;Wrong value&#39;);
  },
  Error
);
</code></pre>
<p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a>:</p>
<p>Using a regular expression runs <code>.toString</code> on the error object, and will
therefore also include the error name.</p>
<pre><code class="language-js">assert.throws(
  () =&gt; {
    throw new Error(&#39;Wrong value&#39;);
  },
  /^Error: Wrong value$/
);
</code></pre>
<p>Custom error validation:</p>
<p>The function must return <code>true</code> to indicate all internal validations passed.
It will otherwise fail with an <a href="#assert_class_assert_assertionerror"><code>AssertionError</code></a>.</p>
<pre><code class="language-js">assert.throws(
  () =&gt; {
    throw new Error(&#39;Wrong value&#39;);
  },
  (err) =&gt; {
    assert(err instanceof Error);
    assert(/value/.test(err));
    // Avoid returning anything from validation functions besides `true`.
    // Otherwise, it&#39;s not clear what part of the validation failed. Instead,
    // throw an error about the specific validation that failed (as done in this
    // example) and add as much helpful debugging information to that error as
    // possible.
    return true;
  },
  &#39;unexpected error&#39;
);
</code></pre>
<p><code>error</code> cannot be a string. If a string is provided as the second
argument, then <code>error</code> is assumed to be omitted and the string will be used for
<code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same
message as the thrown error message is going to result in an
<code>ERR_AMBIGUOUS_ARGUMENT</code> error. Please read the example below carefully if using
a string as the second argument gets considered:</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">function throwingFirst() {
  throw new Error(&#39;First&#39;);
}

function throwingSecond() {
  throw new Error(&#39;Second&#39;);
}

function notThrowing() {}

// The second argument is a string and the input function threw an Error.
// The first case will not throw as it does not match for the error message
// thrown by the input function!
assert.throws(throwingFirst, &#39;Second&#39;);
// In the next example the message has no benefit over the message from the
// error and since it is not clear if the user intended to actually match
// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.
assert.throws(throwingSecond, &#39;Second&#39;);
// TypeError [ERR_AMBIGUOUS_ARGUMENT]

// The string is only used (as message) in case the function does not throw:
assert.throws(notThrowing, &#39;Second&#39;);
// AssertionError [ERR_ASSERTION]: Missing expected exception: Second

// If it was intended to match for the error message do this instead:
// It does not throw because the error messages match.
assert.throws(throwingSecond, /Second$/);

// If the error message does not match, an AssertionError is thrown.
assert.throws(throwingFirst, /Second$/);
// AssertionError [ERR_ASSERTION]
</code></pre>
<p>Due to the confusing error-prone notation, avoid a string as the second
argument.</p>

        <!-- API END -->
      </div>
    </div>
  </div>
</body>
</html>

Hacker Blog, Shell İndir, Sql İnjection, XSS Attacks, LFI Attacks, Social Hacking, Exploit Bot, Proxy Tools, Web Shell, PHP Shell, Alfa Shell İndir, Hacking Training Set, DDoS Script, Denial Of Service, Botnet, RFI Attacks, Encryption
Telegram @BIBIL_0DAY