11cb0ef41Sopenharmony_ci# Assert
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.1.21-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/assert.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:assert` module provides a set of assertion functions for verifying
101cb0ef41Sopenharmony_ciinvariants.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci## Strict assertion mode
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci<!-- YAML
151cb0ef41Sopenharmony_ciadded: v9.9.0
161cb0ef41Sopenharmony_cichanges:
171cb0ef41Sopenharmony_ci  - version: v15.0.0
181cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34001
191cb0ef41Sopenharmony_ci    description: Exposed as `require('node:assert/strict')`.
201cb0ef41Sopenharmony_ci  - version:
211cb0ef41Sopenharmony_ci      - v13.9.0
221cb0ef41Sopenharmony_ci      - v12.16.2
231cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/31635
241cb0ef41Sopenharmony_ci    description: Changed "strict mode" to "strict assertion mode" and "legacy
251cb0ef41Sopenharmony_ci                 mode" to "legacy assertion mode" to avoid confusion with the
261cb0ef41Sopenharmony_ci                 more usual meaning of "strict mode".
271cb0ef41Sopenharmony_ci  - version: v9.9.0
281cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17615
291cb0ef41Sopenharmony_ci    description: Added error diffs to the strict assertion mode.
301cb0ef41Sopenharmony_ci  - version: v9.9.0
311cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17002
321cb0ef41Sopenharmony_ci    description: Added strict assertion mode to the assert module.
331cb0ef41Sopenharmony_ci-->
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciIn strict assertion mode, non-strict methods behave like their corresponding
361cb0ef41Sopenharmony_cistrict methods. For example, [`assert.deepEqual()`][] will behave like
371cb0ef41Sopenharmony_ci[`assert.deepStrictEqual()`][].
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciIn strict assertion mode, error messages for objects display a diff. In legacy
401cb0ef41Sopenharmony_ciassertion mode, error messages for objects display the objects, often truncated.
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciTo use strict assertion mode:
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci```mjs
451cb0ef41Sopenharmony_ciimport { strict as assert } from 'node:assert';
461cb0ef41Sopenharmony_ci```
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```cjs
491cb0ef41Sopenharmony_ciconst assert = require('node:assert').strict;
501cb0ef41Sopenharmony_ci```
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci```mjs
531cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
541cb0ef41Sopenharmony_ci```
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci```cjs
571cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
581cb0ef41Sopenharmony_ci```
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciExample error diff:
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci```mjs
631cb0ef41Sopenharmony_ciimport { strict as assert } from 'node:assert';
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
661cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
671cb0ef41Sopenharmony_ci// + actual - expected ... Lines skipped
681cb0ef41Sopenharmony_ci//
691cb0ef41Sopenharmony_ci//   [
701cb0ef41Sopenharmony_ci//     [
711cb0ef41Sopenharmony_ci// ...
721cb0ef41Sopenharmony_ci//       2,
731cb0ef41Sopenharmony_ci// +     3
741cb0ef41Sopenharmony_ci// -     '3'
751cb0ef41Sopenharmony_ci//     ],
761cb0ef41Sopenharmony_ci// ...
771cb0ef41Sopenharmony_ci//     5
781cb0ef41Sopenharmony_ci//   ]
791cb0ef41Sopenharmony_ci```
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci```cjs
821cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
851cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
861cb0ef41Sopenharmony_ci// + actual - expected ... Lines skipped
871cb0ef41Sopenharmony_ci//
881cb0ef41Sopenharmony_ci//   [
891cb0ef41Sopenharmony_ci//     [
901cb0ef41Sopenharmony_ci// ...
911cb0ef41Sopenharmony_ci//       2,
921cb0ef41Sopenharmony_ci// +     3
931cb0ef41Sopenharmony_ci// -     '3'
941cb0ef41Sopenharmony_ci//     ],
951cb0ef41Sopenharmony_ci// ...
961cb0ef41Sopenharmony_ci//     5
971cb0ef41Sopenharmony_ci//   ]
981cb0ef41Sopenharmony_ci```
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ciTo deactivate the colors, use the `NO_COLOR` or `NODE_DISABLE_COLORS`
1011cb0ef41Sopenharmony_cienvironment variables. This will also deactivate the colors in the REPL. For
1021cb0ef41Sopenharmony_cimore on color support in terminal environments, read the tty
1031cb0ef41Sopenharmony_ci[`getColorDepth()`][] documentation.
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci## Legacy assertion mode
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciLegacy assertion mode uses the [`==` operator][] in:
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci* [`assert.deepEqual()`][]
1101cb0ef41Sopenharmony_ci* [`assert.equal()`][]
1111cb0ef41Sopenharmony_ci* [`assert.notDeepEqual()`][]
1121cb0ef41Sopenharmony_ci* [`assert.notEqual()`][]
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciTo use legacy assertion mode:
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci```mjs
1171cb0ef41Sopenharmony_ciimport assert from 'node:assert';
1181cb0ef41Sopenharmony_ci```
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci```cjs
1211cb0ef41Sopenharmony_ciconst assert = require('node:assert');
1221cb0ef41Sopenharmony_ci```
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ciLegacy assertion mode may have surprising results, especially when using
1251cb0ef41Sopenharmony_ci[`assert.deepEqual()`][]:
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci```cjs
1281cb0ef41Sopenharmony_ci// WARNING: This does not throw an AssertionError in legacy assertion mode!
1291cb0ef41Sopenharmony_ciassert.deepEqual(/a/gi, new Date());
1301cb0ef41Sopenharmony_ci```
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci## Class: assert.AssertionError
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci* Extends: {errors.Error}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciIndicates the failure of an assertion. All errors thrown by the `node:assert`
1371cb0ef41Sopenharmony_cimodule will be instances of the `AssertionError` class.
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci### `new assert.AssertionError(options)`
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci<!-- YAML
1421cb0ef41Sopenharmony_ciadded: v0.1.21
1431cb0ef41Sopenharmony_ci-->
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci* `options` {Object}
1461cb0ef41Sopenharmony_ci  * `message` {string} If provided, the error message is set to this value.
1471cb0ef41Sopenharmony_ci  * `actual` {any} The `actual` property on the error instance.
1481cb0ef41Sopenharmony_ci  * `expected` {any} The `expected` property on the error instance.
1491cb0ef41Sopenharmony_ci  * `operator` {string} The `operator` property on the error instance.
1501cb0ef41Sopenharmony_ci  * `stackStartFn` {Function} If provided, the generated stack trace omits
1511cb0ef41Sopenharmony_ci    frames before this function.
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ciA subclass of `Error` that indicates the failure of an assertion.
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ciAll instances contain the built-in `Error` properties (`message` and `name`)
1561cb0ef41Sopenharmony_ciand:
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci* `actual` {any} Set to the `actual` argument for methods such as
1591cb0ef41Sopenharmony_ci  [`assert.strictEqual()`][].
1601cb0ef41Sopenharmony_ci* `expected` {any} Set to the `expected` value for methods such as
1611cb0ef41Sopenharmony_ci  [`assert.strictEqual()`][].
1621cb0ef41Sopenharmony_ci* `generatedMessage` {boolean} Indicates if the message was auto-generated
1631cb0ef41Sopenharmony_ci  (`true`) or not.
1641cb0ef41Sopenharmony_ci* `code` {string} Value is always `ERR_ASSERTION` to show that the error is an
1651cb0ef41Sopenharmony_ci  assertion error.
1661cb0ef41Sopenharmony_ci* `operator` {string} Set to the passed in operator value.
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci```mjs
1691cb0ef41Sopenharmony_ciimport assert from 'node:assert';
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci// Generate an AssertionError to compare the error message later:
1721cb0ef41Sopenharmony_ciconst { message } = new assert.AssertionError({
1731cb0ef41Sopenharmony_ci  actual: 1,
1741cb0ef41Sopenharmony_ci  expected: 2,
1751cb0ef41Sopenharmony_ci  operator: 'strictEqual',
1761cb0ef41Sopenharmony_ci});
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci// Verify error output:
1791cb0ef41Sopenharmony_citry {
1801cb0ef41Sopenharmony_ci  assert.strictEqual(1, 2);
1811cb0ef41Sopenharmony_ci} catch (err) {
1821cb0ef41Sopenharmony_ci  assert(err instanceof assert.AssertionError);
1831cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, message);
1841cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'AssertionError');
1851cb0ef41Sopenharmony_ci  assert.strictEqual(err.actual, 1);
1861cb0ef41Sopenharmony_ci  assert.strictEqual(err.expected, 2);
1871cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'ERR_ASSERTION');
1881cb0ef41Sopenharmony_ci  assert.strictEqual(err.operator, 'strictEqual');
1891cb0ef41Sopenharmony_ci  assert.strictEqual(err.generatedMessage, true);
1901cb0ef41Sopenharmony_ci}
1911cb0ef41Sopenharmony_ci```
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci```cjs
1941cb0ef41Sopenharmony_ciconst assert = require('node:assert');
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci// Generate an AssertionError to compare the error message later:
1971cb0ef41Sopenharmony_ciconst { message } = new assert.AssertionError({
1981cb0ef41Sopenharmony_ci  actual: 1,
1991cb0ef41Sopenharmony_ci  expected: 2,
2001cb0ef41Sopenharmony_ci  operator: 'strictEqual',
2011cb0ef41Sopenharmony_ci});
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci// Verify error output:
2041cb0ef41Sopenharmony_citry {
2051cb0ef41Sopenharmony_ci  assert.strictEqual(1, 2);
2061cb0ef41Sopenharmony_ci} catch (err) {
2071cb0ef41Sopenharmony_ci  assert(err instanceof assert.AssertionError);
2081cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, message);
2091cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'AssertionError');
2101cb0ef41Sopenharmony_ci  assert.strictEqual(err.actual, 1);
2111cb0ef41Sopenharmony_ci  assert.strictEqual(err.expected, 2);
2121cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'ERR_ASSERTION');
2131cb0ef41Sopenharmony_ci  assert.strictEqual(err.operator, 'strictEqual');
2141cb0ef41Sopenharmony_ci  assert.strictEqual(err.generatedMessage, true);
2151cb0ef41Sopenharmony_ci}
2161cb0ef41Sopenharmony_ci```
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci## Class: `assert.CallTracker`
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci<!-- YAML
2211cb0ef41Sopenharmony_ciadded:
2221cb0ef41Sopenharmony_ci  - v14.2.0
2231cb0ef41Sopenharmony_ci  - v12.19.0
2241cb0ef41Sopenharmony_ci-->
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ciThis feature is currently experimental and behavior might still change.
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci### `new assert.CallTracker()`
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci<!-- YAML
2331cb0ef41Sopenharmony_ciadded:
2341cb0ef41Sopenharmony_ci  - v14.2.0
2351cb0ef41Sopenharmony_ci  - v12.19.0
2361cb0ef41Sopenharmony_ci-->
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ciCreates a new [`CallTracker`][] object which can be used to track if functions
2391cb0ef41Sopenharmony_ciwere called a specific number of times. The `tracker.verify()` must be called
2401cb0ef41Sopenharmony_cifor the verification to take place. The usual pattern would be to call it in a
2411cb0ef41Sopenharmony_ci[`process.on('exit')`][] handler.
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci```mjs
2441cb0ef41Sopenharmony_ciimport assert from 'node:assert';
2451cb0ef41Sopenharmony_ciimport process from 'node:process';
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
2481cb0ef41Sopenharmony_ci
2491cb0ef41Sopenharmony_cifunction func() {}
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci// callsfunc() must be called exactly 1 time before tracker.verify().
2521cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 1);
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_cicallsfunc();
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci// Calls tracker.verify() and verifies if all tracker.calls() functions have
2571cb0ef41Sopenharmony_ci// been called exact times.
2581cb0ef41Sopenharmony_ciprocess.on('exit', () => {
2591cb0ef41Sopenharmony_ci  tracker.verify();
2601cb0ef41Sopenharmony_ci});
2611cb0ef41Sopenharmony_ci```
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci```cjs
2641cb0ef41Sopenharmony_ciconst assert = require('node:assert');
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_cifunction func() {}
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci// callsfunc() must be called exactly 1 time before tracker.verify().
2711cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 1);
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_cicallsfunc();
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci// Calls tracker.verify() and verifies if all tracker.calls() functions have
2761cb0ef41Sopenharmony_ci// been called exact times.
2771cb0ef41Sopenharmony_ciprocess.on('exit', () => {
2781cb0ef41Sopenharmony_ci  tracker.verify();
2791cb0ef41Sopenharmony_ci});
2801cb0ef41Sopenharmony_ci```
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_ci### `tracker.calls([fn][, exact])`
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci<!-- YAML
2851cb0ef41Sopenharmony_ciadded:
2861cb0ef41Sopenharmony_ci  - v14.2.0
2871cb0ef41Sopenharmony_ci  - v12.19.0
2881cb0ef41Sopenharmony_ci-->
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci* `fn` {Function} **Default:** A no-op function.
2911cb0ef41Sopenharmony_ci* `exact` {number} **Default:** `1`.
2921cb0ef41Sopenharmony_ci* Returns: {Function} that wraps `fn`.
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ciThe wrapper function is expected to be called exactly `exact` times. If the
2951cb0ef41Sopenharmony_cifunction has not been called exactly `exact` times when
2961cb0ef41Sopenharmony_ci[`tracker.verify()`][] is called, then [`tracker.verify()`][] will throw an
2971cb0ef41Sopenharmony_cierror.
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci```mjs
3001cb0ef41Sopenharmony_ciimport assert from 'node:assert';
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci// Creates call tracker.
3031cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_cifunction func() {}
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_ci// Returns a function that wraps func() that must be called exact times
3081cb0ef41Sopenharmony_ci// before tracker.verify().
3091cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func);
3101cb0ef41Sopenharmony_ci```
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci```cjs
3131cb0ef41Sopenharmony_ciconst assert = require('node:assert');
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ci// Creates call tracker.
3161cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
3171cb0ef41Sopenharmony_ci
3181cb0ef41Sopenharmony_cifunction func() {}
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci// Returns a function that wraps func() that must be called exact times
3211cb0ef41Sopenharmony_ci// before tracker.verify().
3221cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func);
3231cb0ef41Sopenharmony_ci```
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci### `tracker.getCalls(fn)`
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ci<!-- YAML
3281cb0ef41Sopenharmony_ciadded: v18.8.0
3291cb0ef41Sopenharmony_ci-->
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci* `fn` {Function}.
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_ci* Returns: {Array} with all the calls to a tracked function.
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ci* Object {Object}
3361cb0ef41Sopenharmony_ci  * `thisArg` {Object}
3371cb0ef41Sopenharmony_ci  * `arguments` {Array} the arguments passed to the tracked function
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci```mjs
3401cb0ef41Sopenharmony_ciimport assert from 'node:assert';
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_cifunction func() {}
3451cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func);
3461cb0ef41Sopenharmony_cicallsfunc(1, 2, 3);
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ciassert.deepStrictEqual(tracker.getCalls(callsfunc),
3491cb0ef41Sopenharmony_ci                       [{ thisArg: undefined, arguments: [1, 2, 3] }]);
3501cb0ef41Sopenharmony_ci```
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ci```cjs
3531cb0ef41Sopenharmony_ciconst assert = require('node:assert');
3541cb0ef41Sopenharmony_ci
3551cb0ef41Sopenharmony_ci// Creates call tracker.
3561cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_cifunction func() {}
3591cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func);
3601cb0ef41Sopenharmony_cicallsfunc(1, 2, 3);
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ciassert.deepStrictEqual(tracker.getCalls(callsfunc),
3631cb0ef41Sopenharmony_ci                       [{ thisArg: undefined, arguments: [1, 2, 3] }]);
3641cb0ef41Sopenharmony_ci```
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci### `tracker.report()`
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci<!-- YAML
3691cb0ef41Sopenharmony_ciadded:
3701cb0ef41Sopenharmony_ci  - v14.2.0
3711cb0ef41Sopenharmony_ci  - v12.19.0
3721cb0ef41Sopenharmony_ci-->
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ci* Returns: {Array} of objects containing information about the wrapper functions
3751cb0ef41Sopenharmony_ci  returned by [`tracker.calls()`][].
3761cb0ef41Sopenharmony_ci* Object {Object}
3771cb0ef41Sopenharmony_ci  * `message` {string}
3781cb0ef41Sopenharmony_ci  * `actual` {number} The actual number of times the function was called.
3791cb0ef41Sopenharmony_ci  * `expected` {number} The number of times the function was expected to be
3801cb0ef41Sopenharmony_ci    called.
3811cb0ef41Sopenharmony_ci  * `operator` {string} The name of the function that is wrapped.
3821cb0ef41Sopenharmony_ci  * `stack` {Object} A stack trace of the function.
3831cb0ef41Sopenharmony_ci
3841cb0ef41Sopenharmony_ciThe arrays contains information about the expected and actual number of calls of
3851cb0ef41Sopenharmony_cithe functions that have not been called the expected number of times.
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci```mjs
3881cb0ef41Sopenharmony_ciimport assert from 'node:assert';
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci// Creates call tracker.
3911cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
3921cb0ef41Sopenharmony_ci
3931cb0ef41Sopenharmony_cifunction func() {}
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci// Returns a function that wraps func() that must be called exact times
3961cb0ef41Sopenharmony_ci// before tracker.verify().
3971cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 2);
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ci// Returns an array containing information on callsfunc()
4001cb0ef41Sopenharmony_ciconsole.log(tracker.report());
4011cb0ef41Sopenharmony_ci// [
4021cb0ef41Sopenharmony_ci//  {
4031cb0ef41Sopenharmony_ci//    message: 'Expected the func function to be executed 2 time(s) but was
4041cb0ef41Sopenharmony_ci//    executed 0 time(s).',
4051cb0ef41Sopenharmony_ci//    actual: 0,
4061cb0ef41Sopenharmony_ci//    expected: 2,
4071cb0ef41Sopenharmony_ci//    operator: 'func',
4081cb0ef41Sopenharmony_ci//    stack: stack trace
4091cb0ef41Sopenharmony_ci//  }
4101cb0ef41Sopenharmony_ci// ]
4111cb0ef41Sopenharmony_ci```
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ci```cjs
4141cb0ef41Sopenharmony_ciconst assert = require('node:assert');
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci// Creates call tracker.
4171cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_cifunction func() {}
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ci// Returns a function that wraps func() that must be called exact times
4221cb0ef41Sopenharmony_ci// before tracker.verify().
4231cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 2);
4241cb0ef41Sopenharmony_ci
4251cb0ef41Sopenharmony_ci// Returns an array containing information on callsfunc()
4261cb0ef41Sopenharmony_ciconsole.log(tracker.report());
4271cb0ef41Sopenharmony_ci// [
4281cb0ef41Sopenharmony_ci//  {
4291cb0ef41Sopenharmony_ci//    message: 'Expected the func function to be executed 2 time(s) but was
4301cb0ef41Sopenharmony_ci//    executed 0 time(s).',
4311cb0ef41Sopenharmony_ci//    actual: 0,
4321cb0ef41Sopenharmony_ci//    expected: 2,
4331cb0ef41Sopenharmony_ci//    operator: 'func',
4341cb0ef41Sopenharmony_ci//    stack: stack trace
4351cb0ef41Sopenharmony_ci//  }
4361cb0ef41Sopenharmony_ci// ]
4371cb0ef41Sopenharmony_ci```
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ci### `tracker.reset([fn])`
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ci<!-- YAML
4421cb0ef41Sopenharmony_ciadded: v18.8.0
4431cb0ef41Sopenharmony_ci-->
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci* `fn` {Function} a tracked function to reset.
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ciReset calls of the call tracker.
4481cb0ef41Sopenharmony_ciIf a tracked function is passed as an argument, the calls will be reset for it.
4491cb0ef41Sopenharmony_ciIf no arguments are passed, all tracked functions will be reset
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci```mjs
4521cb0ef41Sopenharmony_ciimport assert from 'node:assert';
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_cifunction func() {}
4571cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func);
4581cb0ef41Sopenharmony_ci
4591cb0ef41Sopenharmony_cicallsfunc();
4601cb0ef41Sopenharmony_ci// Tracker was called once
4611cb0ef41Sopenharmony_ciassert.strictEqual(tracker.getCalls(callsfunc).length, 1);
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_citracker.reset(callsfunc);
4641cb0ef41Sopenharmony_ciassert.strictEqual(tracker.getCalls(callsfunc).length, 0);
4651cb0ef41Sopenharmony_ci```
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci```cjs
4681cb0ef41Sopenharmony_ciconst assert = require('node:assert');
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
4711cb0ef41Sopenharmony_ci
4721cb0ef41Sopenharmony_cifunction func() {}
4731cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func);
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_cicallsfunc();
4761cb0ef41Sopenharmony_ci// Tracker was called once
4771cb0ef41Sopenharmony_ciassert.strictEqual(tracker.getCalls(callsfunc).length, 1);
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_citracker.reset(callsfunc);
4801cb0ef41Sopenharmony_ciassert.strictEqual(tracker.getCalls(callsfunc).length, 0);
4811cb0ef41Sopenharmony_ci```
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci### `tracker.verify()`
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_ci<!-- YAML
4861cb0ef41Sopenharmony_ciadded:
4871cb0ef41Sopenharmony_ci  - v14.2.0
4881cb0ef41Sopenharmony_ci  - v12.19.0
4891cb0ef41Sopenharmony_ci-->
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_ciIterates through the list of functions passed to
4921cb0ef41Sopenharmony_ci[`tracker.calls()`][] and will throw an error for functions that
4931cb0ef41Sopenharmony_cihave not been called the expected number of times.
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci```mjs
4961cb0ef41Sopenharmony_ciimport assert from 'node:assert';
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci// Creates call tracker.
4991cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_cifunction func() {}
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ci// Returns a function that wraps func() that must be called exact times
5041cb0ef41Sopenharmony_ci// before tracker.verify().
5051cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 2);
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_cicallsfunc();
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci// Will throw an error since callsfunc() was only called once.
5101cb0ef41Sopenharmony_citracker.verify();
5111cb0ef41Sopenharmony_ci```
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci```cjs
5141cb0ef41Sopenharmony_ciconst assert = require('node:assert');
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci// Creates call tracker.
5171cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_cifunction func() {}
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci// Returns a function that wraps func() that must be called exact times
5221cb0ef41Sopenharmony_ci// before tracker.verify().
5231cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 2);
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_cicallsfunc();
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci// Will throw an error since callsfunc() was only called once.
5281cb0ef41Sopenharmony_citracker.verify();
5291cb0ef41Sopenharmony_ci```
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_ci## `assert(value[, message])`
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_ci<!-- YAML
5341cb0ef41Sopenharmony_ciadded: v0.5.9
5351cb0ef41Sopenharmony_ci-->
5361cb0ef41Sopenharmony_ci
5371cb0ef41Sopenharmony_ci* `value` {any} The input that is checked for being truthy.
5381cb0ef41Sopenharmony_ci* `message` {string|Error}
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_ciAn alias of [`assert.ok()`][].
5411cb0ef41Sopenharmony_ci
5421cb0ef41Sopenharmony_ci## `assert.deepEqual(actual, expected[, message])`
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci<!-- YAML
5451cb0ef41Sopenharmony_ciadded: v0.1.21
5461cb0ef41Sopenharmony_cichanges:
5471cb0ef41Sopenharmony_ci  - version: v18.0.0
5481cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41020
5491cb0ef41Sopenharmony_ci    description: Regular expressions lastIndex property is now compared as well.
5501cb0ef41Sopenharmony_ci  - version:
5511cb0ef41Sopenharmony_ci      - v16.0.0
5521cb0ef41Sopenharmony_ci      - v14.18.0
5531cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/38113
5541cb0ef41Sopenharmony_ci    description: In Legacy assertion mode, changed status from Deprecated to
5551cb0ef41Sopenharmony_ci                 Legacy.
5561cb0ef41Sopenharmony_ci  - version: v14.0.0
5571cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30766
5581cb0ef41Sopenharmony_ci    description: NaN is now treated as being identical if both sides are
5591cb0ef41Sopenharmony_ci                 NaN.
5601cb0ef41Sopenharmony_ci  - version: v12.0.0
5611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/25008
5621cb0ef41Sopenharmony_ci    description: The type tags are now properly compared and there are a couple
5631cb0ef41Sopenharmony_ci                 minor comparison adjustments to make the check less surprising.
5641cb0ef41Sopenharmony_ci  - version: v9.0.0
5651cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15001
5661cb0ef41Sopenharmony_ci    description: The `Error` names and messages are now properly compared.
5671cb0ef41Sopenharmony_ci  - version: v8.0.0
5681cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/12142
5691cb0ef41Sopenharmony_ci    description: The `Set` and `Map` content is also compared.
5701cb0ef41Sopenharmony_ci  - version:
5711cb0ef41Sopenharmony_ci      - v6.4.0
5721cb0ef41Sopenharmony_ci      - v4.7.1
5731cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8002
5741cb0ef41Sopenharmony_ci    description: Typed array slices are handled correctly now.
5751cb0ef41Sopenharmony_ci  - version:
5761cb0ef41Sopenharmony_ci      - v6.1.0
5771cb0ef41Sopenharmony_ci      - v4.5.0
5781cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6432
5791cb0ef41Sopenharmony_ci    description: Objects with circular references can be used as inputs now.
5801cb0ef41Sopenharmony_ci  - version:
5811cb0ef41Sopenharmony_ci      - v5.10.1
5821cb0ef41Sopenharmony_ci      - v4.4.3
5831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5910
5841cb0ef41Sopenharmony_ci    description: Handle non-`Uint8Array` typed arrays correctly.
5851cb0ef41Sopenharmony_ci-->
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ci* `actual` {any}
5881cb0ef41Sopenharmony_ci* `expected` {any}
5891cb0ef41Sopenharmony_ci* `message` {string|Error}
5901cb0ef41Sopenharmony_ci
5911cb0ef41Sopenharmony_ci**Strict assertion mode**
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ciAn alias of [`assert.deepStrictEqual()`][].
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_ci**Legacy assertion mode**
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`assert.deepStrictEqual()`][] instead.
5981cb0ef41Sopenharmony_ci
5991cb0ef41Sopenharmony_ciTests for deep equality between the `actual` and `expected` parameters. Consider
6001cb0ef41Sopenharmony_ciusing [`assert.deepStrictEqual()`][] instead. [`assert.deepEqual()`][] can have
6011cb0ef41Sopenharmony_cisurprising results.
6021cb0ef41Sopenharmony_ci
6031cb0ef41Sopenharmony_ci_Deep equality_ means that the enumerable "own" properties of child objects
6041cb0ef41Sopenharmony_ciare also recursively evaluated by the following rules.
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ci### Comparison details
6071cb0ef41Sopenharmony_ci
6081cb0ef41Sopenharmony_ci* Primitive values are compared with the [`==` operator][],
6091cb0ef41Sopenharmony_ci  with the exception of `NaN`. It is treated as being identical in case
6101cb0ef41Sopenharmony_ci  both sides are `NaN`.
6111cb0ef41Sopenharmony_ci* [Type tags][Object.prototype.toString()] of objects should be the same.
6121cb0ef41Sopenharmony_ci* Only [enumerable "own" properties][] are considered.
6131cb0ef41Sopenharmony_ci* [`Error`][] names and messages are always compared, even if these are not
6141cb0ef41Sopenharmony_ci  enumerable properties.
6151cb0ef41Sopenharmony_ci* [Object wrappers][] are compared both as objects and unwrapped values.
6161cb0ef41Sopenharmony_ci* `Object` properties are compared unordered.
6171cb0ef41Sopenharmony_ci* [`Map`][] keys and [`Set`][] items are compared unordered.
6181cb0ef41Sopenharmony_ci* Recursion stops when both sides differ or both sides encounter a circular
6191cb0ef41Sopenharmony_ci  reference.
6201cb0ef41Sopenharmony_ci* Implementation does not test the [`[[Prototype]]`][prototype-spec] of
6211cb0ef41Sopenharmony_ci  objects.
6221cb0ef41Sopenharmony_ci* [`Symbol`][] properties are not compared.
6231cb0ef41Sopenharmony_ci* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
6241cb0ef41Sopenharmony_ci* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
6251cb0ef41Sopenharmony_ci  are not enumerable properties.
6261cb0ef41Sopenharmony_ci
6271cb0ef41Sopenharmony_ciThe following example does not throw an [`AssertionError`][] because the
6281cb0ef41Sopenharmony_ciprimitives are compared using the [`==` operator][].
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ci```mjs
6311cb0ef41Sopenharmony_ciimport assert from 'node:assert';
6321cb0ef41Sopenharmony_ci// WARNING: This does not throw an AssertionError!
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ciassert.deepEqual('+00000000', false);
6351cb0ef41Sopenharmony_ci```
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ci```cjs
6381cb0ef41Sopenharmony_ciconst assert = require('node:assert');
6391cb0ef41Sopenharmony_ci// WARNING: This does not throw an AssertionError!
6401cb0ef41Sopenharmony_ci
6411cb0ef41Sopenharmony_ciassert.deepEqual('+00000000', false);
6421cb0ef41Sopenharmony_ci```
6431cb0ef41Sopenharmony_ci
6441cb0ef41Sopenharmony_ci"Deep" equality means that the enumerable "own" properties of child objects
6451cb0ef41Sopenharmony_ciare evaluated also:
6461cb0ef41Sopenharmony_ci
6471cb0ef41Sopenharmony_ci```mjs
6481cb0ef41Sopenharmony_ciimport assert from 'node:assert';
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_ciconst obj1 = {
6511cb0ef41Sopenharmony_ci  a: {
6521cb0ef41Sopenharmony_ci    b: 1,
6531cb0ef41Sopenharmony_ci  },
6541cb0ef41Sopenharmony_ci};
6551cb0ef41Sopenharmony_ciconst obj2 = {
6561cb0ef41Sopenharmony_ci  a: {
6571cb0ef41Sopenharmony_ci    b: 2,
6581cb0ef41Sopenharmony_ci  },
6591cb0ef41Sopenharmony_ci};
6601cb0ef41Sopenharmony_ciconst obj3 = {
6611cb0ef41Sopenharmony_ci  a: {
6621cb0ef41Sopenharmony_ci    b: 1,
6631cb0ef41Sopenharmony_ci  },
6641cb0ef41Sopenharmony_ci};
6651cb0ef41Sopenharmony_ciconst obj4 = Object.create(obj1);
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj1);
6681cb0ef41Sopenharmony_ci// OK
6691cb0ef41Sopenharmony_ci
6701cb0ef41Sopenharmony_ci// Values of b are different:
6711cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj2);
6721cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
6731cb0ef41Sopenharmony_ci
6741cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj3);
6751cb0ef41Sopenharmony_ci// OK
6761cb0ef41Sopenharmony_ci
6771cb0ef41Sopenharmony_ci// Prototypes are ignored:
6781cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj4);
6791cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } deepEqual {}
6801cb0ef41Sopenharmony_ci```
6811cb0ef41Sopenharmony_ci
6821cb0ef41Sopenharmony_ci```cjs
6831cb0ef41Sopenharmony_ciconst assert = require('node:assert');
6841cb0ef41Sopenharmony_ci
6851cb0ef41Sopenharmony_ciconst obj1 = {
6861cb0ef41Sopenharmony_ci  a: {
6871cb0ef41Sopenharmony_ci    b: 1,
6881cb0ef41Sopenharmony_ci  },
6891cb0ef41Sopenharmony_ci};
6901cb0ef41Sopenharmony_ciconst obj2 = {
6911cb0ef41Sopenharmony_ci  a: {
6921cb0ef41Sopenharmony_ci    b: 2,
6931cb0ef41Sopenharmony_ci  },
6941cb0ef41Sopenharmony_ci};
6951cb0ef41Sopenharmony_ciconst obj3 = {
6961cb0ef41Sopenharmony_ci  a: {
6971cb0ef41Sopenharmony_ci    b: 1,
6981cb0ef41Sopenharmony_ci  },
6991cb0ef41Sopenharmony_ci};
7001cb0ef41Sopenharmony_ciconst obj4 = Object.create(obj1);
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj1);
7031cb0ef41Sopenharmony_ci// OK
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci// Values of b are different:
7061cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj2);
7071cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj3);
7101cb0ef41Sopenharmony_ci// OK
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci// Prototypes are ignored:
7131cb0ef41Sopenharmony_ciassert.deepEqual(obj1, obj4);
7141cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } deepEqual {}
7151cb0ef41Sopenharmony_ci```
7161cb0ef41Sopenharmony_ci
7171cb0ef41Sopenharmony_ciIf the values are not equal, an [`AssertionError`][] is thrown with a `message`
7181cb0ef41Sopenharmony_ciproperty set equal to the value of the `message` parameter. If the `message`
7191cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the `message`
7201cb0ef41Sopenharmony_ciparameter is an instance of an [`Error`][] then it will be thrown instead of the
7211cb0ef41Sopenharmony_ci[`AssertionError`][].
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci## `assert.deepStrictEqual(actual, expected[, message])`
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ci<!-- YAML
7261cb0ef41Sopenharmony_ciadded: v1.2.0
7271cb0ef41Sopenharmony_cichanges:
7281cb0ef41Sopenharmony_ci  - version: v18.0.0
7291cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41020
7301cb0ef41Sopenharmony_ci    description: Regular expressions lastIndex property is now compared as well.
7311cb0ef41Sopenharmony_ci  - version: v9.0.0
7321cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15169
7331cb0ef41Sopenharmony_ci    description: Enumerable symbol properties are now compared.
7341cb0ef41Sopenharmony_ci  - version: v9.0.0
7351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15036
7361cb0ef41Sopenharmony_ci    description: The `NaN` is now compared using the
7371cb0ef41Sopenharmony_ci              [SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero)
7381cb0ef41Sopenharmony_ci              comparison.
7391cb0ef41Sopenharmony_ci  - version: v8.5.0
7401cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15001
7411cb0ef41Sopenharmony_ci    description: The `Error` names and messages are now properly compared.
7421cb0ef41Sopenharmony_ci  - version: v8.0.0
7431cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/12142
7441cb0ef41Sopenharmony_ci    description: The `Set` and `Map` content is also compared.
7451cb0ef41Sopenharmony_ci  - version:
7461cb0ef41Sopenharmony_ci    - v6.4.0
7471cb0ef41Sopenharmony_ci    - v4.7.1
7481cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8002
7491cb0ef41Sopenharmony_ci    description: Typed array slices are handled correctly now.
7501cb0ef41Sopenharmony_ci  - version: v6.1.0
7511cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6432
7521cb0ef41Sopenharmony_ci    description: Objects with circular references can be used as inputs now.
7531cb0ef41Sopenharmony_ci  - version:
7541cb0ef41Sopenharmony_ci    - v5.10.1
7551cb0ef41Sopenharmony_ci    - v4.4.3
7561cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5910
7571cb0ef41Sopenharmony_ci    description: Handle non-`Uint8Array` typed arrays correctly.
7581cb0ef41Sopenharmony_ci-->
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci* `actual` {any}
7611cb0ef41Sopenharmony_ci* `expected` {any}
7621cb0ef41Sopenharmony_ci* `message` {string|Error}
7631cb0ef41Sopenharmony_ci
7641cb0ef41Sopenharmony_ciTests for deep equality between the `actual` and `expected` parameters.
7651cb0ef41Sopenharmony_ci"Deep" equality means that the enumerable "own" properties of child objects
7661cb0ef41Sopenharmony_ciare recursively evaluated also by the following rules.
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci### Comparison details
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ci* Primitive values are compared using [`Object.is()`][].
7711cb0ef41Sopenharmony_ci* [Type tags][Object.prototype.toString()] of objects should be the same.
7721cb0ef41Sopenharmony_ci* [`[[Prototype]]`][prototype-spec] of objects are compared using
7731cb0ef41Sopenharmony_ci  the [`===` operator][].
7741cb0ef41Sopenharmony_ci* Only [enumerable "own" properties][] are considered.
7751cb0ef41Sopenharmony_ci* [`Error`][] names and messages are always compared, even if these are not
7761cb0ef41Sopenharmony_ci  enumerable properties.
7771cb0ef41Sopenharmony_ci* Enumerable own [`Symbol`][] properties are compared as well.
7781cb0ef41Sopenharmony_ci* [Object wrappers][] are compared both as objects and unwrapped values.
7791cb0ef41Sopenharmony_ci* `Object` properties are compared unordered.
7801cb0ef41Sopenharmony_ci* [`Map`][] keys and [`Set`][] items are compared unordered.
7811cb0ef41Sopenharmony_ci* Recursion stops when both sides differ or both sides encounter a circular
7821cb0ef41Sopenharmony_ci  reference.
7831cb0ef41Sopenharmony_ci* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
7841cb0ef41Sopenharmony_ci  below for further details.
7851cb0ef41Sopenharmony_ci* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
7861cb0ef41Sopenharmony_ci  are not enumerable properties.
7871cb0ef41Sopenharmony_ci
7881cb0ef41Sopenharmony_ci```mjs
7891cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci// This fails because 1 !== '1'.
7921cb0ef41Sopenharmony_ciassert.deepStrictEqual({ a: 1 }, { a: '1' });
7931cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
7941cb0ef41Sopenharmony_ci// + actual - expected
7951cb0ef41Sopenharmony_ci//
7961cb0ef41Sopenharmony_ci//   {
7971cb0ef41Sopenharmony_ci// +   a: 1
7981cb0ef41Sopenharmony_ci// -   a: '1'
7991cb0ef41Sopenharmony_ci//   }
8001cb0ef41Sopenharmony_ci
8011cb0ef41Sopenharmony_ci// The following objects don't have own properties
8021cb0ef41Sopenharmony_ciconst date = new Date();
8031cb0ef41Sopenharmony_ciconst object = {};
8041cb0ef41Sopenharmony_ciconst fakeDate = {};
8051cb0ef41Sopenharmony_ciObject.setPrototypeOf(fakeDate, Date.prototype);
8061cb0ef41Sopenharmony_ci
8071cb0ef41Sopenharmony_ci// Different [[Prototype]]:
8081cb0ef41Sopenharmony_ciassert.deepStrictEqual(object, fakeDate);
8091cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
8101cb0ef41Sopenharmony_ci// + actual - expected
8111cb0ef41Sopenharmony_ci//
8121cb0ef41Sopenharmony_ci// + {}
8131cb0ef41Sopenharmony_ci// - Date {}
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_ci// Different type tags:
8161cb0ef41Sopenharmony_ciassert.deepStrictEqual(date, fakeDate);
8171cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
8181cb0ef41Sopenharmony_ci// + actual - expected
8191cb0ef41Sopenharmony_ci//
8201cb0ef41Sopenharmony_ci// + 2018-04-26T00:49:08.604Z
8211cb0ef41Sopenharmony_ci// - Date {}
8221cb0ef41Sopenharmony_ci
8231cb0ef41Sopenharmony_ciassert.deepStrictEqual(NaN, NaN);
8241cb0ef41Sopenharmony_ci// OK because Object.is(NaN, NaN) is true.
8251cb0ef41Sopenharmony_ci
8261cb0ef41Sopenharmony_ci// Different unwrapped numbers:
8271cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Number(1), new Number(2));
8281cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
8291cb0ef41Sopenharmony_ci// + actual - expected
8301cb0ef41Sopenharmony_ci//
8311cb0ef41Sopenharmony_ci// + [Number: 1]
8321cb0ef41Sopenharmony_ci// - [Number: 2]
8331cb0ef41Sopenharmony_ci
8341cb0ef41Sopenharmony_ciassert.deepStrictEqual(new String('foo'), Object('foo'));
8351cb0ef41Sopenharmony_ci// OK because the object and the string are identical when unwrapped.
8361cb0ef41Sopenharmony_ci
8371cb0ef41Sopenharmony_ciassert.deepStrictEqual(-0, -0);
8381cb0ef41Sopenharmony_ci// OK
8391cb0ef41Sopenharmony_ci
8401cb0ef41Sopenharmony_ci// Different zeros:
8411cb0ef41Sopenharmony_ciassert.deepStrictEqual(0, -0);
8421cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
8431cb0ef41Sopenharmony_ci// + actual - expected
8441cb0ef41Sopenharmony_ci//
8451cb0ef41Sopenharmony_ci// + 0
8461cb0ef41Sopenharmony_ci// - -0
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ciconst symbol1 = Symbol();
8491cb0ef41Sopenharmony_ciconst symbol2 = Symbol();
8501cb0ef41Sopenharmony_ciassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
8511cb0ef41Sopenharmony_ci// OK, because it is the same symbol on both objects.
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ciassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
8541cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
8551cb0ef41Sopenharmony_ci//
8561cb0ef41Sopenharmony_ci// {
8571cb0ef41Sopenharmony_ci//   [Symbol()]: 1
8581cb0ef41Sopenharmony_ci// }
8591cb0ef41Sopenharmony_ci
8601cb0ef41Sopenharmony_ciconst weakMap1 = new WeakMap();
8611cb0ef41Sopenharmony_ciconst weakMap2 = new WeakMap([[{}, {}]]);
8621cb0ef41Sopenharmony_ciconst weakMap3 = new WeakMap();
8631cb0ef41Sopenharmony_ciweakMap3.unequal = true;
8641cb0ef41Sopenharmony_ci
8651cb0ef41Sopenharmony_ciassert.deepStrictEqual(weakMap1, weakMap2);
8661cb0ef41Sopenharmony_ci// OK, because it is impossible to compare the entries
8671cb0ef41Sopenharmony_ci
8681cb0ef41Sopenharmony_ci// Fails because weakMap3 has a property that weakMap1 does not contain:
8691cb0ef41Sopenharmony_ciassert.deepStrictEqual(weakMap1, weakMap3);
8701cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
8711cb0ef41Sopenharmony_ci// + actual - expected
8721cb0ef41Sopenharmony_ci//
8731cb0ef41Sopenharmony_ci//   WeakMap {
8741cb0ef41Sopenharmony_ci// +   [items unknown]
8751cb0ef41Sopenharmony_ci// -   [items unknown],
8761cb0ef41Sopenharmony_ci// -   unequal: true
8771cb0ef41Sopenharmony_ci//   }
8781cb0ef41Sopenharmony_ci```
8791cb0ef41Sopenharmony_ci
8801cb0ef41Sopenharmony_ci```cjs
8811cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
8821cb0ef41Sopenharmony_ci
8831cb0ef41Sopenharmony_ci// This fails because 1 !== '1'.
8841cb0ef41Sopenharmony_ciassert.deepStrictEqual({ a: 1 }, { a: '1' });
8851cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
8861cb0ef41Sopenharmony_ci// + actual - expected
8871cb0ef41Sopenharmony_ci//
8881cb0ef41Sopenharmony_ci//   {
8891cb0ef41Sopenharmony_ci// +   a: 1
8901cb0ef41Sopenharmony_ci// -   a: '1'
8911cb0ef41Sopenharmony_ci//   }
8921cb0ef41Sopenharmony_ci
8931cb0ef41Sopenharmony_ci// The following objects don't have own properties
8941cb0ef41Sopenharmony_ciconst date = new Date();
8951cb0ef41Sopenharmony_ciconst object = {};
8961cb0ef41Sopenharmony_ciconst fakeDate = {};
8971cb0ef41Sopenharmony_ciObject.setPrototypeOf(fakeDate, Date.prototype);
8981cb0ef41Sopenharmony_ci
8991cb0ef41Sopenharmony_ci// Different [[Prototype]]:
9001cb0ef41Sopenharmony_ciassert.deepStrictEqual(object, fakeDate);
9011cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
9021cb0ef41Sopenharmony_ci// + actual - expected
9031cb0ef41Sopenharmony_ci//
9041cb0ef41Sopenharmony_ci// + {}
9051cb0ef41Sopenharmony_ci// - Date {}
9061cb0ef41Sopenharmony_ci
9071cb0ef41Sopenharmony_ci// Different type tags:
9081cb0ef41Sopenharmony_ciassert.deepStrictEqual(date, fakeDate);
9091cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
9101cb0ef41Sopenharmony_ci// + actual - expected
9111cb0ef41Sopenharmony_ci//
9121cb0ef41Sopenharmony_ci// + 2018-04-26T00:49:08.604Z
9131cb0ef41Sopenharmony_ci// - Date {}
9141cb0ef41Sopenharmony_ci
9151cb0ef41Sopenharmony_ciassert.deepStrictEqual(NaN, NaN);
9161cb0ef41Sopenharmony_ci// OK because Object.is(NaN, NaN) is true.
9171cb0ef41Sopenharmony_ci
9181cb0ef41Sopenharmony_ci// Different unwrapped numbers:
9191cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Number(1), new Number(2));
9201cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
9211cb0ef41Sopenharmony_ci// + actual - expected
9221cb0ef41Sopenharmony_ci//
9231cb0ef41Sopenharmony_ci// + [Number: 1]
9241cb0ef41Sopenharmony_ci// - [Number: 2]
9251cb0ef41Sopenharmony_ci
9261cb0ef41Sopenharmony_ciassert.deepStrictEqual(new String('foo'), Object('foo'));
9271cb0ef41Sopenharmony_ci// OK because the object and the string are identical when unwrapped.
9281cb0ef41Sopenharmony_ci
9291cb0ef41Sopenharmony_ciassert.deepStrictEqual(-0, -0);
9301cb0ef41Sopenharmony_ci// OK
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_ci// Different zeros:
9331cb0ef41Sopenharmony_ciassert.deepStrictEqual(0, -0);
9341cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
9351cb0ef41Sopenharmony_ci// + actual - expected
9361cb0ef41Sopenharmony_ci//
9371cb0ef41Sopenharmony_ci// + 0
9381cb0ef41Sopenharmony_ci// - -0
9391cb0ef41Sopenharmony_ci
9401cb0ef41Sopenharmony_ciconst symbol1 = Symbol();
9411cb0ef41Sopenharmony_ciconst symbol2 = Symbol();
9421cb0ef41Sopenharmony_ciassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
9431cb0ef41Sopenharmony_ci// OK, because it is the same symbol on both objects.
9441cb0ef41Sopenharmony_ci
9451cb0ef41Sopenharmony_ciassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
9461cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
9471cb0ef41Sopenharmony_ci//
9481cb0ef41Sopenharmony_ci// {
9491cb0ef41Sopenharmony_ci//   [Symbol()]: 1
9501cb0ef41Sopenharmony_ci// }
9511cb0ef41Sopenharmony_ci
9521cb0ef41Sopenharmony_ciconst weakMap1 = new WeakMap();
9531cb0ef41Sopenharmony_ciconst weakMap2 = new WeakMap([[{}, {}]]);
9541cb0ef41Sopenharmony_ciconst weakMap3 = new WeakMap();
9551cb0ef41Sopenharmony_ciweakMap3.unequal = true;
9561cb0ef41Sopenharmony_ci
9571cb0ef41Sopenharmony_ciassert.deepStrictEqual(weakMap1, weakMap2);
9581cb0ef41Sopenharmony_ci// OK, because it is impossible to compare the entries
9591cb0ef41Sopenharmony_ci
9601cb0ef41Sopenharmony_ci// Fails because weakMap3 has a property that weakMap1 does not contain:
9611cb0ef41Sopenharmony_ciassert.deepStrictEqual(weakMap1, weakMap3);
9621cb0ef41Sopenharmony_ci// AssertionError: Expected inputs to be strictly deep-equal:
9631cb0ef41Sopenharmony_ci// + actual - expected
9641cb0ef41Sopenharmony_ci//
9651cb0ef41Sopenharmony_ci//   WeakMap {
9661cb0ef41Sopenharmony_ci// +   [items unknown]
9671cb0ef41Sopenharmony_ci// -   [items unknown],
9681cb0ef41Sopenharmony_ci// -   unequal: true
9691cb0ef41Sopenharmony_ci//   }
9701cb0ef41Sopenharmony_ci```
9711cb0ef41Sopenharmony_ci
9721cb0ef41Sopenharmony_ciIf the values are not equal, an [`AssertionError`][] is thrown with a `message`
9731cb0ef41Sopenharmony_ciproperty set equal to the value of the `message` parameter. If the `message`
9741cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the `message`
9751cb0ef41Sopenharmony_ciparameter is an instance of an [`Error`][] then it will be thrown instead of the
9761cb0ef41Sopenharmony_ci`AssertionError`.
9771cb0ef41Sopenharmony_ci
9781cb0ef41Sopenharmony_ci## `assert.doesNotMatch(string, regexp[, message])`
9791cb0ef41Sopenharmony_ci
9801cb0ef41Sopenharmony_ci<!-- YAML
9811cb0ef41Sopenharmony_ciadded:
9821cb0ef41Sopenharmony_ci  - v13.6.0
9831cb0ef41Sopenharmony_ci  - v12.16.0
9841cb0ef41Sopenharmony_cichanges:
9851cb0ef41Sopenharmony_ci  - version: v16.0.0
9861cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/38111
9871cb0ef41Sopenharmony_ci    description: This API is no longer experimental.
9881cb0ef41Sopenharmony_ci-->
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_ci* `string` {string}
9911cb0ef41Sopenharmony_ci* `regexp` {RegExp}
9921cb0ef41Sopenharmony_ci* `message` {string|Error}
9931cb0ef41Sopenharmony_ci
9941cb0ef41Sopenharmony_ciExpects the `string` input not to match the regular expression.
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ci```mjs
9971cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
9981cb0ef41Sopenharmony_ci
9991cb0ef41Sopenharmony_ciassert.doesNotMatch('I will fail', /fail/);
10001cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...
10011cb0ef41Sopenharmony_ci
10021cb0ef41Sopenharmony_ciassert.doesNotMatch(123, /pass/);
10031cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.
10041cb0ef41Sopenharmony_ci
10051cb0ef41Sopenharmony_ciassert.doesNotMatch('I will pass', /different/);
10061cb0ef41Sopenharmony_ci// OK
10071cb0ef41Sopenharmony_ci```
10081cb0ef41Sopenharmony_ci
10091cb0ef41Sopenharmony_ci```cjs
10101cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
10111cb0ef41Sopenharmony_ci
10121cb0ef41Sopenharmony_ciassert.doesNotMatch('I will fail', /fail/);
10131cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...
10141cb0ef41Sopenharmony_ci
10151cb0ef41Sopenharmony_ciassert.doesNotMatch(123, /pass/);
10161cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.
10171cb0ef41Sopenharmony_ci
10181cb0ef41Sopenharmony_ciassert.doesNotMatch('I will pass', /different/);
10191cb0ef41Sopenharmony_ci// OK
10201cb0ef41Sopenharmony_ci```
10211cb0ef41Sopenharmony_ci
10221cb0ef41Sopenharmony_ciIf the values do match, or if the `string` argument is of another type than
10231cb0ef41Sopenharmony_ci`string`, an [`AssertionError`][] is thrown with a `message` property set equal
10241cb0ef41Sopenharmony_cito the value of the `message` parameter. If the `message` parameter is
10251cb0ef41Sopenharmony_ciundefined, a default error message is assigned. If the `message` parameter is an
10261cb0ef41Sopenharmony_ciinstance of an [`Error`][] then it will be thrown instead of the
10271cb0ef41Sopenharmony_ci[`AssertionError`][].
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_ci## `assert.doesNotReject(asyncFn[, error][, message])`
10301cb0ef41Sopenharmony_ci
10311cb0ef41Sopenharmony_ci<!-- YAML
10321cb0ef41Sopenharmony_ciadded: v10.0.0
10331cb0ef41Sopenharmony_ci-->
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ci* `asyncFn` {Function|Promise}
10361cb0ef41Sopenharmony_ci* `error` {RegExp|Function}
10371cb0ef41Sopenharmony_ci* `message` {string}
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_ciAwaits the `asyncFn` promise or, if `asyncFn` is a function, immediately
10401cb0ef41Sopenharmony_cicalls the function and awaits the returned promise to complete. It will then
10411cb0ef41Sopenharmony_cicheck that the promise is not rejected.
10421cb0ef41Sopenharmony_ci
10431cb0ef41Sopenharmony_ciIf `asyncFn` is a function and it throws an error synchronously,
10441cb0ef41Sopenharmony_ci`assert.doesNotReject()` will return a rejected `Promise` with that error. If
10451cb0ef41Sopenharmony_cithe function does not return a promise, `assert.doesNotReject()` will return a
10461cb0ef41Sopenharmony_cirejected `Promise` with an [`ERR_INVALID_RETURN_VALUE`][] error. In both cases
10471cb0ef41Sopenharmony_cithe error handler is skipped.
10481cb0ef41Sopenharmony_ci
10491cb0ef41Sopenharmony_ciUsing `assert.doesNotReject()` is actually not useful because there is little
10501cb0ef41Sopenharmony_cibenefit in catching a rejection and then rejecting it again. Instead, consider
10511cb0ef41Sopenharmony_ciadding a comment next to the specific code path that should not reject and keep
10521cb0ef41Sopenharmony_cierror messages as expressive as possible.
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ciIf specified, `error` can be a [`Class`][], [`RegExp`][], or a validation
10551cb0ef41Sopenharmony_cifunction. See [`assert.throws()`][] for more details.
10561cb0ef41Sopenharmony_ci
10571cb0ef41Sopenharmony_ciBesides the async nature to await the completion behaves identically to
10581cb0ef41Sopenharmony_ci[`assert.doesNotThrow()`][].
10591cb0ef41Sopenharmony_ci
10601cb0ef41Sopenharmony_ci```mjs
10611cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_ciawait assert.doesNotReject(
10641cb0ef41Sopenharmony_ci  async () => {
10651cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
10661cb0ef41Sopenharmony_ci  },
10671cb0ef41Sopenharmony_ci  SyntaxError,
10681cb0ef41Sopenharmony_ci);
10691cb0ef41Sopenharmony_ci```
10701cb0ef41Sopenharmony_ci
10711cb0ef41Sopenharmony_ci```cjs
10721cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
10731cb0ef41Sopenharmony_ci
10741cb0ef41Sopenharmony_ci(async () => {
10751cb0ef41Sopenharmony_ci  await assert.doesNotReject(
10761cb0ef41Sopenharmony_ci    async () => {
10771cb0ef41Sopenharmony_ci      throw new TypeError('Wrong value');
10781cb0ef41Sopenharmony_ci    },
10791cb0ef41Sopenharmony_ci    SyntaxError,
10801cb0ef41Sopenharmony_ci  );
10811cb0ef41Sopenharmony_ci})();
10821cb0ef41Sopenharmony_ci```
10831cb0ef41Sopenharmony_ci
10841cb0ef41Sopenharmony_ci```mjs
10851cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
10861cb0ef41Sopenharmony_ci
10871cb0ef41Sopenharmony_ciassert.doesNotReject(Promise.reject(new TypeError('Wrong value')))
10881cb0ef41Sopenharmony_ci  .then(() => {
10891cb0ef41Sopenharmony_ci    // ...
10901cb0ef41Sopenharmony_ci  });
10911cb0ef41Sopenharmony_ci```
10921cb0ef41Sopenharmony_ci
10931cb0ef41Sopenharmony_ci```cjs
10941cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
10951cb0ef41Sopenharmony_ci
10961cb0ef41Sopenharmony_ciassert.doesNotReject(Promise.reject(new TypeError('Wrong value')))
10971cb0ef41Sopenharmony_ci  .then(() => {
10981cb0ef41Sopenharmony_ci    // ...
10991cb0ef41Sopenharmony_ci  });
11001cb0ef41Sopenharmony_ci```
11011cb0ef41Sopenharmony_ci
11021cb0ef41Sopenharmony_ci## `assert.doesNotThrow(fn[, error][, message])`
11031cb0ef41Sopenharmony_ci
11041cb0ef41Sopenharmony_ci<!-- YAML
11051cb0ef41Sopenharmony_ciadded: v0.1.21
11061cb0ef41Sopenharmony_cichanges:
11071cb0ef41Sopenharmony_ci  - version:
11081cb0ef41Sopenharmony_ci    - v5.11.0
11091cb0ef41Sopenharmony_ci    - v4.4.5
11101cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/2407
11111cb0ef41Sopenharmony_ci    description: The `message` parameter is respected now.
11121cb0ef41Sopenharmony_ci  - version: v4.2.0
11131cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/3276
11141cb0ef41Sopenharmony_ci    description: The `error` parameter can now be an arrow function.
11151cb0ef41Sopenharmony_ci-->
11161cb0ef41Sopenharmony_ci
11171cb0ef41Sopenharmony_ci* `fn` {Function}
11181cb0ef41Sopenharmony_ci* `error` {RegExp|Function}
11191cb0ef41Sopenharmony_ci* `message` {string}
11201cb0ef41Sopenharmony_ci
11211cb0ef41Sopenharmony_ciAsserts that the function `fn` does not throw an error.
11221cb0ef41Sopenharmony_ci
11231cb0ef41Sopenharmony_ciUsing `assert.doesNotThrow()` is actually not useful because there
11241cb0ef41Sopenharmony_ciis no benefit in catching an error and then rethrowing it. Instead, consider
11251cb0ef41Sopenharmony_ciadding a comment next to the specific code path that should not throw and keep
11261cb0ef41Sopenharmony_cierror messages as expressive as possible.
11271cb0ef41Sopenharmony_ci
11281cb0ef41Sopenharmony_ciWhen `assert.doesNotThrow()` is called, it will immediately call the `fn`
11291cb0ef41Sopenharmony_cifunction.
11301cb0ef41Sopenharmony_ci
11311cb0ef41Sopenharmony_ciIf an error is thrown and it is the same type as that specified by the `error`
11321cb0ef41Sopenharmony_ciparameter, then an [`AssertionError`][] is thrown. If the error is of a
11331cb0ef41Sopenharmony_cidifferent type, or if the `error` parameter is undefined, the error is
11341cb0ef41Sopenharmony_cipropagated back to the caller.
11351cb0ef41Sopenharmony_ci
11361cb0ef41Sopenharmony_ciIf specified, `error` can be a [`Class`][], [`RegExp`][], or a validation
11371cb0ef41Sopenharmony_cifunction. See [`assert.throws()`][] for more details.
11381cb0ef41Sopenharmony_ci
11391cb0ef41Sopenharmony_ciThe following, for instance, will throw the [`TypeError`][] because there is no
11401cb0ef41Sopenharmony_cimatching error type in the assertion:
11411cb0ef41Sopenharmony_ci
11421cb0ef41Sopenharmony_ci```mjs
11431cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
11441cb0ef41Sopenharmony_ci
11451cb0ef41Sopenharmony_ciassert.doesNotThrow(
11461cb0ef41Sopenharmony_ci  () => {
11471cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
11481cb0ef41Sopenharmony_ci  },
11491cb0ef41Sopenharmony_ci  SyntaxError,
11501cb0ef41Sopenharmony_ci);
11511cb0ef41Sopenharmony_ci```
11521cb0ef41Sopenharmony_ci
11531cb0ef41Sopenharmony_ci```cjs
11541cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
11551cb0ef41Sopenharmony_ci
11561cb0ef41Sopenharmony_ciassert.doesNotThrow(
11571cb0ef41Sopenharmony_ci  () => {
11581cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
11591cb0ef41Sopenharmony_ci  },
11601cb0ef41Sopenharmony_ci  SyntaxError,
11611cb0ef41Sopenharmony_ci);
11621cb0ef41Sopenharmony_ci```
11631cb0ef41Sopenharmony_ci
11641cb0ef41Sopenharmony_ciHowever, the following will result in an [`AssertionError`][] with the message
11651cb0ef41Sopenharmony_ci'Got unwanted exception...':
11661cb0ef41Sopenharmony_ci
11671cb0ef41Sopenharmony_ci```mjs
11681cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
11691cb0ef41Sopenharmony_ci
11701cb0ef41Sopenharmony_ciassert.doesNotThrow(
11711cb0ef41Sopenharmony_ci  () => {
11721cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
11731cb0ef41Sopenharmony_ci  },
11741cb0ef41Sopenharmony_ci  TypeError,
11751cb0ef41Sopenharmony_ci);
11761cb0ef41Sopenharmony_ci```
11771cb0ef41Sopenharmony_ci
11781cb0ef41Sopenharmony_ci```cjs
11791cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
11801cb0ef41Sopenharmony_ci
11811cb0ef41Sopenharmony_ciassert.doesNotThrow(
11821cb0ef41Sopenharmony_ci  () => {
11831cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
11841cb0ef41Sopenharmony_ci  },
11851cb0ef41Sopenharmony_ci  TypeError,
11861cb0ef41Sopenharmony_ci);
11871cb0ef41Sopenharmony_ci```
11881cb0ef41Sopenharmony_ci
11891cb0ef41Sopenharmony_ciIf an [`AssertionError`][] is thrown and a value is provided for the `message`
11901cb0ef41Sopenharmony_ciparameter, the value of `message` will be appended to the [`AssertionError`][]
11911cb0ef41Sopenharmony_cimessage:
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ci```mjs
11941cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
11951cb0ef41Sopenharmony_ci
11961cb0ef41Sopenharmony_ciassert.doesNotThrow(
11971cb0ef41Sopenharmony_ci  () => {
11981cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
11991cb0ef41Sopenharmony_ci  },
12001cb0ef41Sopenharmony_ci  /Wrong value/,
12011cb0ef41Sopenharmony_ci  'Whoops',
12021cb0ef41Sopenharmony_ci);
12031cb0ef41Sopenharmony_ci// Throws: AssertionError: Got unwanted exception: Whoops
12041cb0ef41Sopenharmony_ci```
12051cb0ef41Sopenharmony_ci
12061cb0ef41Sopenharmony_ci```cjs
12071cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
12081cb0ef41Sopenharmony_ci
12091cb0ef41Sopenharmony_ciassert.doesNotThrow(
12101cb0ef41Sopenharmony_ci  () => {
12111cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
12121cb0ef41Sopenharmony_ci  },
12131cb0ef41Sopenharmony_ci  /Wrong value/,
12141cb0ef41Sopenharmony_ci  'Whoops',
12151cb0ef41Sopenharmony_ci);
12161cb0ef41Sopenharmony_ci// Throws: AssertionError: Got unwanted exception: Whoops
12171cb0ef41Sopenharmony_ci```
12181cb0ef41Sopenharmony_ci
12191cb0ef41Sopenharmony_ci## `assert.equal(actual, expected[, message])`
12201cb0ef41Sopenharmony_ci
12211cb0ef41Sopenharmony_ci<!-- YAML
12221cb0ef41Sopenharmony_ciadded: v0.1.21
12231cb0ef41Sopenharmony_cichanges:
12241cb0ef41Sopenharmony_ci  - version:
12251cb0ef41Sopenharmony_ci      - v16.0.0
12261cb0ef41Sopenharmony_ci      - v14.18.0
12271cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/38113
12281cb0ef41Sopenharmony_ci    description: In Legacy assertion mode, changed status from Deprecated to
12291cb0ef41Sopenharmony_ci                 Legacy.
12301cb0ef41Sopenharmony_ci  - version: v14.0.0
12311cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30766
12321cb0ef41Sopenharmony_ci    description: NaN is now treated as being identical if both sides are
12331cb0ef41Sopenharmony_ci                 NaN.
12341cb0ef41Sopenharmony_ci-->
12351cb0ef41Sopenharmony_ci
12361cb0ef41Sopenharmony_ci* `actual` {any}
12371cb0ef41Sopenharmony_ci* `expected` {any}
12381cb0ef41Sopenharmony_ci* `message` {string|Error}
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_ci**Strict assertion mode**
12411cb0ef41Sopenharmony_ci
12421cb0ef41Sopenharmony_ciAn alias of [`assert.strictEqual()`][].
12431cb0ef41Sopenharmony_ci
12441cb0ef41Sopenharmony_ci**Legacy assertion mode**
12451cb0ef41Sopenharmony_ci
12461cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`assert.strictEqual()`][] instead.
12471cb0ef41Sopenharmony_ci
12481cb0ef41Sopenharmony_ciTests shallow, coercive equality between the `actual` and `expected` parameters
12491cb0ef41Sopenharmony_ciusing the [`==` operator][]. `NaN` is specially handled
12501cb0ef41Sopenharmony_ciand treated as being identical if both sides are `NaN`.
12511cb0ef41Sopenharmony_ci
12521cb0ef41Sopenharmony_ci```mjs
12531cb0ef41Sopenharmony_ciimport assert from 'node:assert';
12541cb0ef41Sopenharmony_ci
12551cb0ef41Sopenharmony_ciassert.equal(1, 1);
12561cb0ef41Sopenharmony_ci// OK, 1 == 1
12571cb0ef41Sopenharmony_ciassert.equal(1, '1');
12581cb0ef41Sopenharmony_ci// OK, 1 == '1'
12591cb0ef41Sopenharmony_ciassert.equal(NaN, NaN);
12601cb0ef41Sopenharmony_ci// OK
12611cb0ef41Sopenharmony_ci
12621cb0ef41Sopenharmony_ciassert.equal(1, 2);
12631cb0ef41Sopenharmony_ci// AssertionError: 1 == 2
12641cb0ef41Sopenharmony_ciassert.equal({ a: { b: 1 } }, { a: { b: 1 } });
12651cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
12661cb0ef41Sopenharmony_ci```
12671cb0ef41Sopenharmony_ci
12681cb0ef41Sopenharmony_ci```cjs
12691cb0ef41Sopenharmony_ciconst assert = require('node:assert');
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_ciassert.equal(1, 1);
12721cb0ef41Sopenharmony_ci// OK, 1 == 1
12731cb0ef41Sopenharmony_ciassert.equal(1, '1');
12741cb0ef41Sopenharmony_ci// OK, 1 == '1'
12751cb0ef41Sopenharmony_ciassert.equal(NaN, NaN);
12761cb0ef41Sopenharmony_ci// OK
12771cb0ef41Sopenharmony_ci
12781cb0ef41Sopenharmony_ciassert.equal(1, 2);
12791cb0ef41Sopenharmony_ci// AssertionError: 1 == 2
12801cb0ef41Sopenharmony_ciassert.equal({ a: { b: 1 } }, { a: { b: 1 } });
12811cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
12821cb0ef41Sopenharmony_ci```
12831cb0ef41Sopenharmony_ci
12841cb0ef41Sopenharmony_ciIf the values are not equal, an [`AssertionError`][] is thrown with a `message`
12851cb0ef41Sopenharmony_ciproperty set equal to the value of the `message` parameter. If the `message`
12861cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the `message`
12871cb0ef41Sopenharmony_ciparameter is an instance of an [`Error`][] then it will be thrown instead of the
12881cb0ef41Sopenharmony_ci`AssertionError`.
12891cb0ef41Sopenharmony_ci
12901cb0ef41Sopenharmony_ci## `assert.fail([message])`
12911cb0ef41Sopenharmony_ci
12921cb0ef41Sopenharmony_ci<!-- YAML
12931cb0ef41Sopenharmony_ciadded: v0.1.21
12941cb0ef41Sopenharmony_ci-->
12951cb0ef41Sopenharmony_ci
12961cb0ef41Sopenharmony_ci* `message` {string|Error} **Default:** `'Failed'`
12971cb0ef41Sopenharmony_ci
12981cb0ef41Sopenharmony_ciThrows an [`AssertionError`][] with the provided error message or a default
12991cb0ef41Sopenharmony_cierror message. If the `message` parameter is an instance of an [`Error`][] then
13001cb0ef41Sopenharmony_ciit will be thrown instead of the [`AssertionError`][].
13011cb0ef41Sopenharmony_ci
13021cb0ef41Sopenharmony_ci```mjs
13031cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
13041cb0ef41Sopenharmony_ci
13051cb0ef41Sopenharmony_ciassert.fail();
13061cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Failed
13071cb0ef41Sopenharmony_ci
13081cb0ef41Sopenharmony_ciassert.fail('boom');
13091cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: boom
13101cb0ef41Sopenharmony_ci
13111cb0ef41Sopenharmony_ciassert.fail(new TypeError('need array'));
13121cb0ef41Sopenharmony_ci// TypeError: need array
13131cb0ef41Sopenharmony_ci```
13141cb0ef41Sopenharmony_ci
13151cb0ef41Sopenharmony_ci```cjs
13161cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
13171cb0ef41Sopenharmony_ci
13181cb0ef41Sopenharmony_ciassert.fail();
13191cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Failed
13201cb0ef41Sopenharmony_ci
13211cb0ef41Sopenharmony_ciassert.fail('boom');
13221cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: boom
13231cb0ef41Sopenharmony_ci
13241cb0ef41Sopenharmony_ciassert.fail(new TypeError('need array'));
13251cb0ef41Sopenharmony_ci// TypeError: need array
13261cb0ef41Sopenharmony_ci```
13271cb0ef41Sopenharmony_ci
13281cb0ef41Sopenharmony_ciUsing `assert.fail()` with more than two arguments is possible but deprecated.
13291cb0ef41Sopenharmony_ciSee below for further details.
13301cb0ef41Sopenharmony_ci
13311cb0ef41Sopenharmony_ci## `assert.fail(actual, expected[, message[, operator[, stackStartFn]]])`
13321cb0ef41Sopenharmony_ci
13331cb0ef41Sopenharmony_ci<!-- YAML
13341cb0ef41Sopenharmony_ciadded: v0.1.21
13351cb0ef41Sopenharmony_cichanges:
13361cb0ef41Sopenharmony_ci  - version: v10.0.0
13371cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18418
13381cb0ef41Sopenharmony_ci    description: Calling `assert.fail()` with more than one argument is
13391cb0ef41Sopenharmony_ci                 deprecated and emits a warning.
13401cb0ef41Sopenharmony_ci-->
13411cb0ef41Sopenharmony_ci
13421cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `assert.fail([message])` or other assert
13431cb0ef41Sopenharmony_ci> functions instead.
13441cb0ef41Sopenharmony_ci
13451cb0ef41Sopenharmony_ci* `actual` {any}
13461cb0ef41Sopenharmony_ci* `expected` {any}
13471cb0ef41Sopenharmony_ci* `message` {string|Error}
13481cb0ef41Sopenharmony_ci* `operator` {string} **Default:** `'!='`
13491cb0ef41Sopenharmony_ci* `stackStartFn` {Function} **Default:** `assert.fail`
13501cb0ef41Sopenharmony_ci
13511cb0ef41Sopenharmony_ciIf `message` is falsy, the error message is set as the values of `actual` and
13521cb0ef41Sopenharmony_ci`expected` separated by the provided `operator`. If just the two `actual` and
13531cb0ef41Sopenharmony_ci`expected` arguments are provided, `operator` will default to `'!='`. If
13541cb0ef41Sopenharmony_ci`message` is provided as third argument it will be used as the error message and
13551cb0ef41Sopenharmony_cithe other arguments will be stored as properties on the thrown object. If
13561cb0ef41Sopenharmony_ci`stackStartFn` is provided, all stack frames above that function will be
13571cb0ef41Sopenharmony_ciremoved from stacktrace (see [`Error.captureStackTrace`][]). If no arguments are
13581cb0ef41Sopenharmony_cigiven, the default message `Failed` will be used.
13591cb0ef41Sopenharmony_ci
13601cb0ef41Sopenharmony_ci```mjs
13611cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
13621cb0ef41Sopenharmony_ci
13631cb0ef41Sopenharmony_ciassert.fail('a', 'b');
13641cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: 'a' != 'b'
13651cb0ef41Sopenharmony_ci
13661cb0ef41Sopenharmony_ciassert.fail(1, 2, undefined, '>');
13671cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: 1 > 2
13681cb0ef41Sopenharmony_ci
13691cb0ef41Sopenharmony_ciassert.fail(1, 2, 'fail');
13701cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: fail
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ciassert.fail(1, 2, 'whoops', '>');
13731cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: whoops
13741cb0ef41Sopenharmony_ci
13751cb0ef41Sopenharmony_ciassert.fail(1, 2, new TypeError('need array'));
13761cb0ef41Sopenharmony_ci// TypeError: need array
13771cb0ef41Sopenharmony_ci```
13781cb0ef41Sopenharmony_ci
13791cb0ef41Sopenharmony_ci```cjs
13801cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
13811cb0ef41Sopenharmony_ci
13821cb0ef41Sopenharmony_ciassert.fail('a', 'b');
13831cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: 'a' != 'b'
13841cb0ef41Sopenharmony_ci
13851cb0ef41Sopenharmony_ciassert.fail(1, 2, undefined, '>');
13861cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: 1 > 2
13871cb0ef41Sopenharmony_ci
13881cb0ef41Sopenharmony_ciassert.fail(1, 2, 'fail');
13891cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: fail
13901cb0ef41Sopenharmony_ci
13911cb0ef41Sopenharmony_ciassert.fail(1, 2, 'whoops', '>');
13921cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: whoops
13931cb0ef41Sopenharmony_ci
13941cb0ef41Sopenharmony_ciassert.fail(1, 2, new TypeError('need array'));
13951cb0ef41Sopenharmony_ci// TypeError: need array
13961cb0ef41Sopenharmony_ci```
13971cb0ef41Sopenharmony_ci
13981cb0ef41Sopenharmony_ciIn the last three cases `actual`, `expected`, and `operator` have no
13991cb0ef41Sopenharmony_ciinfluence on the error message.
14001cb0ef41Sopenharmony_ci
14011cb0ef41Sopenharmony_ciExample use of `stackStartFn` for truncating the exception's stacktrace:
14021cb0ef41Sopenharmony_ci
14031cb0ef41Sopenharmony_ci```mjs
14041cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
14051cb0ef41Sopenharmony_ci
14061cb0ef41Sopenharmony_cifunction suppressFrame() {
14071cb0ef41Sopenharmony_ci  assert.fail('a', 'b', undefined, '!==', suppressFrame);
14081cb0ef41Sopenharmony_ci}
14091cb0ef41Sopenharmony_cisuppressFrame();
14101cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
14111cb0ef41Sopenharmony_ci//     at repl:1:1
14121cb0ef41Sopenharmony_ci//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)
14131cb0ef41Sopenharmony_ci//     ...
14141cb0ef41Sopenharmony_ci```
14151cb0ef41Sopenharmony_ci
14161cb0ef41Sopenharmony_ci```cjs
14171cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
14181cb0ef41Sopenharmony_ci
14191cb0ef41Sopenharmony_cifunction suppressFrame() {
14201cb0ef41Sopenharmony_ci  assert.fail('a', 'b', undefined, '!==', suppressFrame);
14211cb0ef41Sopenharmony_ci}
14221cb0ef41Sopenharmony_cisuppressFrame();
14231cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
14241cb0ef41Sopenharmony_ci//     at repl:1:1
14251cb0ef41Sopenharmony_ci//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)
14261cb0ef41Sopenharmony_ci//     ...
14271cb0ef41Sopenharmony_ci```
14281cb0ef41Sopenharmony_ci
14291cb0ef41Sopenharmony_ci## `assert.ifError(value)`
14301cb0ef41Sopenharmony_ci
14311cb0ef41Sopenharmony_ci<!-- YAML
14321cb0ef41Sopenharmony_ciadded: v0.1.97
14331cb0ef41Sopenharmony_cichanges:
14341cb0ef41Sopenharmony_ci  - version: v10.0.0
14351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18247
14361cb0ef41Sopenharmony_ci    description: Instead of throwing the original error it is now wrapped into
14371cb0ef41Sopenharmony_ci                 an [`AssertionError`][] that contains the full stack trace.
14381cb0ef41Sopenharmony_ci  - version: v10.0.0
14391cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18247
14401cb0ef41Sopenharmony_ci    description: Value may now only be `undefined` or `null`. Before all falsy
14411cb0ef41Sopenharmony_ci                 values were handled the same as `null` and did not throw.
14421cb0ef41Sopenharmony_ci-->
14431cb0ef41Sopenharmony_ci
14441cb0ef41Sopenharmony_ci* `value` {any}
14451cb0ef41Sopenharmony_ci
14461cb0ef41Sopenharmony_ciThrows `value` if `value` is not `undefined` or `null`. This is useful when
14471cb0ef41Sopenharmony_citesting the `error` argument in callbacks. The stack trace contains all frames
14481cb0ef41Sopenharmony_cifrom the error passed to `ifError()` including the potential new frames for
14491cb0ef41Sopenharmony_ci`ifError()` itself.
14501cb0ef41Sopenharmony_ci
14511cb0ef41Sopenharmony_ci```mjs
14521cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
14531cb0ef41Sopenharmony_ci
14541cb0ef41Sopenharmony_ciassert.ifError(null);
14551cb0ef41Sopenharmony_ci// OK
14561cb0ef41Sopenharmony_ciassert.ifError(0);
14571cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
14581cb0ef41Sopenharmony_ciassert.ifError('error');
14591cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
14601cb0ef41Sopenharmony_ciassert.ifError(new Error());
14611cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
14621cb0ef41Sopenharmony_ci
14631cb0ef41Sopenharmony_ci// Create some random error frames.
14641cb0ef41Sopenharmony_cilet err;
14651cb0ef41Sopenharmony_ci(function errorFrame() {
14661cb0ef41Sopenharmony_ci  err = new Error('test error');
14671cb0ef41Sopenharmony_ci})();
14681cb0ef41Sopenharmony_ci
14691cb0ef41Sopenharmony_ci(function ifErrorFrame() {
14701cb0ef41Sopenharmony_ci  assert.ifError(err);
14711cb0ef41Sopenharmony_ci})();
14721cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
14731cb0ef41Sopenharmony_ci//     at ifErrorFrame
14741cb0ef41Sopenharmony_ci//     at errorFrame
14751cb0ef41Sopenharmony_ci```
14761cb0ef41Sopenharmony_ci
14771cb0ef41Sopenharmony_ci```cjs
14781cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
14791cb0ef41Sopenharmony_ci
14801cb0ef41Sopenharmony_ciassert.ifError(null);
14811cb0ef41Sopenharmony_ci// OK
14821cb0ef41Sopenharmony_ciassert.ifError(0);
14831cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
14841cb0ef41Sopenharmony_ciassert.ifError('error');
14851cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
14861cb0ef41Sopenharmony_ciassert.ifError(new Error());
14871cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
14881cb0ef41Sopenharmony_ci
14891cb0ef41Sopenharmony_ci// Create some random error frames.
14901cb0ef41Sopenharmony_cilet err;
14911cb0ef41Sopenharmony_ci(function errorFrame() {
14921cb0ef41Sopenharmony_ci  err = new Error('test error');
14931cb0ef41Sopenharmony_ci})();
14941cb0ef41Sopenharmony_ci
14951cb0ef41Sopenharmony_ci(function ifErrorFrame() {
14961cb0ef41Sopenharmony_ci  assert.ifError(err);
14971cb0ef41Sopenharmony_ci})();
14981cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
14991cb0ef41Sopenharmony_ci//     at ifErrorFrame
15001cb0ef41Sopenharmony_ci//     at errorFrame
15011cb0ef41Sopenharmony_ci```
15021cb0ef41Sopenharmony_ci
15031cb0ef41Sopenharmony_ci## `assert.match(string, regexp[, message])`
15041cb0ef41Sopenharmony_ci
15051cb0ef41Sopenharmony_ci<!-- YAML
15061cb0ef41Sopenharmony_ciadded:
15071cb0ef41Sopenharmony_ci  - v13.6.0
15081cb0ef41Sopenharmony_ci  - v12.16.0
15091cb0ef41Sopenharmony_cichanges:
15101cb0ef41Sopenharmony_ci  - version: v16.0.0
15111cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/38111
15121cb0ef41Sopenharmony_ci    description: This API is no longer experimental.
15131cb0ef41Sopenharmony_ci-->
15141cb0ef41Sopenharmony_ci
15151cb0ef41Sopenharmony_ci* `string` {string}
15161cb0ef41Sopenharmony_ci* `regexp` {RegExp}
15171cb0ef41Sopenharmony_ci* `message` {string|Error}
15181cb0ef41Sopenharmony_ci
15191cb0ef41Sopenharmony_ciExpects the `string` input to match the regular expression.
15201cb0ef41Sopenharmony_ci
15211cb0ef41Sopenharmony_ci```mjs
15221cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
15231cb0ef41Sopenharmony_ci
15241cb0ef41Sopenharmony_ciassert.match('I will fail', /pass/);
15251cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The input did not match the regular ...
15261cb0ef41Sopenharmony_ci
15271cb0ef41Sopenharmony_ciassert.match(123, /pass/);
15281cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.
15291cb0ef41Sopenharmony_ci
15301cb0ef41Sopenharmony_ciassert.match('I will pass', /pass/);
15311cb0ef41Sopenharmony_ci// OK
15321cb0ef41Sopenharmony_ci```
15331cb0ef41Sopenharmony_ci
15341cb0ef41Sopenharmony_ci```cjs
15351cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
15361cb0ef41Sopenharmony_ci
15371cb0ef41Sopenharmony_ciassert.match('I will fail', /pass/);
15381cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The input did not match the regular ...
15391cb0ef41Sopenharmony_ci
15401cb0ef41Sopenharmony_ciassert.match(123, /pass/);
15411cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.
15421cb0ef41Sopenharmony_ci
15431cb0ef41Sopenharmony_ciassert.match('I will pass', /pass/);
15441cb0ef41Sopenharmony_ci// OK
15451cb0ef41Sopenharmony_ci```
15461cb0ef41Sopenharmony_ci
15471cb0ef41Sopenharmony_ciIf the values do not match, or if the `string` argument is of another type than
15481cb0ef41Sopenharmony_ci`string`, an [`AssertionError`][] is thrown with a `message` property set equal
15491cb0ef41Sopenharmony_cito the value of the `message` parameter. If the `message` parameter is
15501cb0ef41Sopenharmony_ciundefined, a default error message is assigned. If the `message` parameter is an
15511cb0ef41Sopenharmony_ciinstance of an [`Error`][] then it will be thrown instead of the
15521cb0ef41Sopenharmony_ci[`AssertionError`][].
15531cb0ef41Sopenharmony_ci
15541cb0ef41Sopenharmony_ci## `assert.notDeepEqual(actual, expected[, message])`
15551cb0ef41Sopenharmony_ci
15561cb0ef41Sopenharmony_ci<!-- YAML
15571cb0ef41Sopenharmony_ciadded: v0.1.21
15581cb0ef41Sopenharmony_cichanges:
15591cb0ef41Sopenharmony_ci  - version:
15601cb0ef41Sopenharmony_ci      - v16.0.0
15611cb0ef41Sopenharmony_ci      - v14.18.0
15621cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/38113
15631cb0ef41Sopenharmony_ci    description: In Legacy assertion mode, changed status from Deprecated to
15641cb0ef41Sopenharmony_ci                 Legacy.
15651cb0ef41Sopenharmony_ci  - version: v14.0.0
15661cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30766
15671cb0ef41Sopenharmony_ci    description: NaN is now treated as being identical if both sides are
15681cb0ef41Sopenharmony_ci                 NaN.
15691cb0ef41Sopenharmony_ci  - version: v9.0.0
15701cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15001
15711cb0ef41Sopenharmony_ci    description: The `Error` names and messages are now properly compared.
15721cb0ef41Sopenharmony_ci  - version: v8.0.0
15731cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/12142
15741cb0ef41Sopenharmony_ci    description: The `Set` and `Map` content is also compared.
15751cb0ef41Sopenharmony_ci  - version:
15761cb0ef41Sopenharmony_ci      - v6.4.0
15771cb0ef41Sopenharmony_ci      - v4.7.1
15781cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8002
15791cb0ef41Sopenharmony_ci    description: Typed array slices are handled correctly now.
15801cb0ef41Sopenharmony_ci  - version:
15811cb0ef41Sopenharmony_ci      - v6.1.0
15821cb0ef41Sopenharmony_ci      - v4.5.0
15831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6432
15841cb0ef41Sopenharmony_ci    description: Objects with circular references can be used as inputs now.
15851cb0ef41Sopenharmony_ci  - version:
15861cb0ef41Sopenharmony_ci      - v5.10.1
15871cb0ef41Sopenharmony_ci      - v4.4.3
15881cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5910
15891cb0ef41Sopenharmony_ci    description: Handle non-`Uint8Array` typed arrays correctly.
15901cb0ef41Sopenharmony_ci-->
15911cb0ef41Sopenharmony_ci
15921cb0ef41Sopenharmony_ci* `actual` {any}
15931cb0ef41Sopenharmony_ci* `expected` {any}
15941cb0ef41Sopenharmony_ci* `message` {string|Error}
15951cb0ef41Sopenharmony_ci
15961cb0ef41Sopenharmony_ci**Strict assertion mode**
15971cb0ef41Sopenharmony_ci
15981cb0ef41Sopenharmony_ciAn alias of [`assert.notDeepStrictEqual()`][].
15991cb0ef41Sopenharmony_ci
16001cb0ef41Sopenharmony_ci**Legacy assertion mode**
16011cb0ef41Sopenharmony_ci
16021cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`assert.notDeepStrictEqual()`][] instead.
16031cb0ef41Sopenharmony_ci
16041cb0ef41Sopenharmony_ciTests for any deep inequality. Opposite of [`assert.deepEqual()`][].
16051cb0ef41Sopenharmony_ci
16061cb0ef41Sopenharmony_ci```mjs
16071cb0ef41Sopenharmony_ciimport assert from 'node:assert';
16081cb0ef41Sopenharmony_ci
16091cb0ef41Sopenharmony_ciconst obj1 = {
16101cb0ef41Sopenharmony_ci  a: {
16111cb0ef41Sopenharmony_ci    b: 1,
16121cb0ef41Sopenharmony_ci  },
16131cb0ef41Sopenharmony_ci};
16141cb0ef41Sopenharmony_ciconst obj2 = {
16151cb0ef41Sopenharmony_ci  a: {
16161cb0ef41Sopenharmony_ci    b: 2,
16171cb0ef41Sopenharmony_ci  },
16181cb0ef41Sopenharmony_ci};
16191cb0ef41Sopenharmony_ciconst obj3 = {
16201cb0ef41Sopenharmony_ci  a: {
16211cb0ef41Sopenharmony_ci    b: 1,
16221cb0ef41Sopenharmony_ci  },
16231cb0ef41Sopenharmony_ci};
16241cb0ef41Sopenharmony_ciconst obj4 = Object.create(obj1);
16251cb0ef41Sopenharmony_ci
16261cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj1);
16271cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
16281cb0ef41Sopenharmony_ci
16291cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj2);
16301cb0ef41Sopenharmony_ci// OK
16311cb0ef41Sopenharmony_ci
16321cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj3);
16331cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
16341cb0ef41Sopenharmony_ci
16351cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj4);
16361cb0ef41Sopenharmony_ci// OK
16371cb0ef41Sopenharmony_ci```
16381cb0ef41Sopenharmony_ci
16391cb0ef41Sopenharmony_ci```cjs
16401cb0ef41Sopenharmony_ciconst assert = require('node:assert');
16411cb0ef41Sopenharmony_ci
16421cb0ef41Sopenharmony_ciconst obj1 = {
16431cb0ef41Sopenharmony_ci  a: {
16441cb0ef41Sopenharmony_ci    b: 1,
16451cb0ef41Sopenharmony_ci  },
16461cb0ef41Sopenharmony_ci};
16471cb0ef41Sopenharmony_ciconst obj2 = {
16481cb0ef41Sopenharmony_ci  a: {
16491cb0ef41Sopenharmony_ci    b: 2,
16501cb0ef41Sopenharmony_ci  },
16511cb0ef41Sopenharmony_ci};
16521cb0ef41Sopenharmony_ciconst obj3 = {
16531cb0ef41Sopenharmony_ci  a: {
16541cb0ef41Sopenharmony_ci    b: 1,
16551cb0ef41Sopenharmony_ci  },
16561cb0ef41Sopenharmony_ci};
16571cb0ef41Sopenharmony_ciconst obj4 = Object.create(obj1);
16581cb0ef41Sopenharmony_ci
16591cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj1);
16601cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
16611cb0ef41Sopenharmony_ci
16621cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj2);
16631cb0ef41Sopenharmony_ci// OK
16641cb0ef41Sopenharmony_ci
16651cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj3);
16661cb0ef41Sopenharmony_ci// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
16671cb0ef41Sopenharmony_ci
16681cb0ef41Sopenharmony_ciassert.notDeepEqual(obj1, obj4);
16691cb0ef41Sopenharmony_ci// OK
16701cb0ef41Sopenharmony_ci```
16711cb0ef41Sopenharmony_ci
16721cb0ef41Sopenharmony_ciIf the values are deeply equal, an [`AssertionError`][] is thrown with a
16731cb0ef41Sopenharmony_ci`message` property set equal to the value of the `message` parameter. If the
16741cb0ef41Sopenharmony_ci`message` parameter is undefined, a default error message is assigned. If the
16751cb0ef41Sopenharmony_ci`message` parameter is an instance of an [`Error`][] then it will be thrown
16761cb0ef41Sopenharmony_ciinstead of the `AssertionError`.
16771cb0ef41Sopenharmony_ci
16781cb0ef41Sopenharmony_ci## `assert.notDeepStrictEqual(actual, expected[, message])`
16791cb0ef41Sopenharmony_ci
16801cb0ef41Sopenharmony_ci<!-- YAML
16811cb0ef41Sopenharmony_ciadded: v1.2.0
16821cb0ef41Sopenharmony_cichanges:
16831cb0ef41Sopenharmony_ci  - version: v9.0.0
16841cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15398
16851cb0ef41Sopenharmony_ci    description: The `-0` and `+0` are not considered equal anymore.
16861cb0ef41Sopenharmony_ci  - version: v9.0.0
16871cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15036
16881cb0ef41Sopenharmony_ci    description: The `NaN` is now compared using the
16891cb0ef41Sopenharmony_ci              [SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero)
16901cb0ef41Sopenharmony_ci              comparison.
16911cb0ef41Sopenharmony_ci  - version: v9.0.0
16921cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15001
16931cb0ef41Sopenharmony_ci    description: The `Error` names and messages are now properly compared.
16941cb0ef41Sopenharmony_ci  - version: v8.0.0
16951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/12142
16961cb0ef41Sopenharmony_ci    description: The `Set` and `Map` content is also compared.
16971cb0ef41Sopenharmony_ci  - version:
16981cb0ef41Sopenharmony_ci    - v6.4.0
16991cb0ef41Sopenharmony_ci    - v4.7.1
17001cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8002
17011cb0ef41Sopenharmony_ci    description: Typed array slices are handled correctly now.
17021cb0ef41Sopenharmony_ci  - version: v6.1.0
17031cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6432
17041cb0ef41Sopenharmony_ci    description: Objects with circular references can be used as inputs now.
17051cb0ef41Sopenharmony_ci  - version:
17061cb0ef41Sopenharmony_ci    - v5.10.1
17071cb0ef41Sopenharmony_ci    - v4.4.3
17081cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5910
17091cb0ef41Sopenharmony_ci    description: Handle non-`Uint8Array` typed arrays correctly.
17101cb0ef41Sopenharmony_ci-->
17111cb0ef41Sopenharmony_ci
17121cb0ef41Sopenharmony_ci* `actual` {any}
17131cb0ef41Sopenharmony_ci* `expected` {any}
17141cb0ef41Sopenharmony_ci* `message` {string|Error}
17151cb0ef41Sopenharmony_ci
17161cb0ef41Sopenharmony_ciTests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
17171cb0ef41Sopenharmony_ci
17181cb0ef41Sopenharmony_ci```mjs
17191cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
17201cb0ef41Sopenharmony_ci
17211cb0ef41Sopenharmony_ciassert.notDeepStrictEqual({ a: 1 }, { a: '1' });
17221cb0ef41Sopenharmony_ci// OK
17231cb0ef41Sopenharmony_ci```
17241cb0ef41Sopenharmony_ci
17251cb0ef41Sopenharmony_ci```cjs
17261cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
17271cb0ef41Sopenharmony_ci
17281cb0ef41Sopenharmony_ciassert.notDeepStrictEqual({ a: 1 }, { a: '1' });
17291cb0ef41Sopenharmony_ci// OK
17301cb0ef41Sopenharmony_ci```
17311cb0ef41Sopenharmony_ci
17321cb0ef41Sopenharmony_ciIf the values are deeply and strictly equal, an [`AssertionError`][] is thrown
17331cb0ef41Sopenharmony_ciwith a `message` property set equal to the value of the `message` parameter. If
17341cb0ef41Sopenharmony_cithe `message` parameter is undefined, a default error message is assigned. If
17351cb0ef41Sopenharmony_cithe `message` parameter is an instance of an [`Error`][] then it will be thrown
17361cb0ef41Sopenharmony_ciinstead of the [`AssertionError`][].
17371cb0ef41Sopenharmony_ci
17381cb0ef41Sopenharmony_ci## `assert.notEqual(actual, expected[, message])`
17391cb0ef41Sopenharmony_ci
17401cb0ef41Sopenharmony_ci<!-- YAML
17411cb0ef41Sopenharmony_ciadded: v0.1.21
17421cb0ef41Sopenharmony_cichanges:
17431cb0ef41Sopenharmony_ci  - version:
17441cb0ef41Sopenharmony_ci      - v16.0.0
17451cb0ef41Sopenharmony_ci      - v14.18.0
17461cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/38113
17471cb0ef41Sopenharmony_ci    description: In Legacy assertion mode, changed status from Deprecated to
17481cb0ef41Sopenharmony_ci                 Legacy.
17491cb0ef41Sopenharmony_ci  - version: v14.0.0
17501cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30766
17511cb0ef41Sopenharmony_ci    description: NaN is now treated as being identical if both sides are
17521cb0ef41Sopenharmony_ci                 NaN.
17531cb0ef41Sopenharmony_ci-->
17541cb0ef41Sopenharmony_ci
17551cb0ef41Sopenharmony_ci* `actual` {any}
17561cb0ef41Sopenharmony_ci* `expected` {any}
17571cb0ef41Sopenharmony_ci* `message` {string|Error}
17581cb0ef41Sopenharmony_ci
17591cb0ef41Sopenharmony_ci**Strict assertion mode**
17601cb0ef41Sopenharmony_ci
17611cb0ef41Sopenharmony_ciAn alias of [`assert.notStrictEqual()`][].
17621cb0ef41Sopenharmony_ci
17631cb0ef41Sopenharmony_ci**Legacy assertion mode**
17641cb0ef41Sopenharmony_ci
17651cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`assert.notStrictEqual()`][] instead.
17661cb0ef41Sopenharmony_ci
17671cb0ef41Sopenharmony_ciTests shallow, coercive inequality with the [`!=` operator][]. `NaN` is
17681cb0ef41Sopenharmony_cispecially handled and treated as being identical if both sides are `NaN`.
17691cb0ef41Sopenharmony_ci
17701cb0ef41Sopenharmony_ci```mjs
17711cb0ef41Sopenharmony_ciimport assert from 'node:assert';
17721cb0ef41Sopenharmony_ci
17731cb0ef41Sopenharmony_ciassert.notEqual(1, 2);
17741cb0ef41Sopenharmony_ci// OK
17751cb0ef41Sopenharmony_ci
17761cb0ef41Sopenharmony_ciassert.notEqual(1, 1);
17771cb0ef41Sopenharmony_ci// AssertionError: 1 != 1
17781cb0ef41Sopenharmony_ci
17791cb0ef41Sopenharmony_ciassert.notEqual(1, '1');
17801cb0ef41Sopenharmony_ci// AssertionError: 1 != '1'
17811cb0ef41Sopenharmony_ci```
17821cb0ef41Sopenharmony_ci
17831cb0ef41Sopenharmony_ci```cjs
17841cb0ef41Sopenharmony_ciconst assert = require('node:assert');
17851cb0ef41Sopenharmony_ci
17861cb0ef41Sopenharmony_ciassert.notEqual(1, 2);
17871cb0ef41Sopenharmony_ci// OK
17881cb0ef41Sopenharmony_ci
17891cb0ef41Sopenharmony_ciassert.notEqual(1, 1);
17901cb0ef41Sopenharmony_ci// AssertionError: 1 != 1
17911cb0ef41Sopenharmony_ci
17921cb0ef41Sopenharmony_ciassert.notEqual(1, '1');
17931cb0ef41Sopenharmony_ci// AssertionError: 1 != '1'
17941cb0ef41Sopenharmony_ci```
17951cb0ef41Sopenharmony_ci
17961cb0ef41Sopenharmony_ciIf the values are equal, an [`AssertionError`][] is thrown with a `message`
17971cb0ef41Sopenharmony_ciproperty set equal to the value of the `message` parameter. If the `message`
17981cb0ef41Sopenharmony_ciparameter is undefined, a default error message is assigned. If the `message`
17991cb0ef41Sopenharmony_ciparameter is an instance of an [`Error`][] then it will be thrown instead of the
18001cb0ef41Sopenharmony_ci`AssertionError`.
18011cb0ef41Sopenharmony_ci
18021cb0ef41Sopenharmony_ci## `assert.notStrictEqual(actual, expected[, message])`
18031cb0ef41Sopenharmony_ci
18041cb0ef41Sopenharmony_ci<!-- YAML
18051cb0ef41Sopenharmony_ciadded: v0.1.21
18061cb0ef41Sopenharmony_cichanges:
18071cb0ef41Sopenharmony_ci  - version: v10.0.0
18081cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17003
18091cb0ef41Sopenharmony_ci    description: Used comparison changed from Strict Equality to `Object.is()`.
18101cb0ef41Sopenharmony_ci-->
18111cb0ef41Sopenharmony_ci
18121cb0ef41Sopenharmony_ci* `actual` {any}
18131cb0ef41Sopenharmony_ci* `expected` {any}
18141cb0ef41Sopenharmony_ci* `message` {string|Error}
18151cb0ef41Sopenharmony_ci
18161cb0ef41Sopenharmony_ciTests strict inequality between the `actual` and `expected` parameters as
18171cb0ef41Sopenharmony_cidetermined by [`Object.is()`][].
18181cb0ef41Sopenharmony_ci
18191cb0ef41Sopenharmony_ci```mjs
18201cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
18211cb0ef41Sopenharmony_ci
18221cb0ef41Sopenharmony_ciassert.notStrictEqual(1, 2);
18231cb0ef41Sopenharmony_ci// OK
18241cb0ef41Sopenharmony_ci
18251cb0ef41Sopenharmony_ciassert.notStrictEqual(1, 1);
18261cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:
18271cb0ef41Sopenharmony_ci//
18281cb0ef41Sopenharmony_ci// 1
18291cb0ef41Sopenharmony_ci
18301cb0ef41Sopenharmony_ciassert.notStrictEqual(1, '1');
18311cb0ef41Sopenharmony_ci// OK
18321cb0ef41Sopenharmony_ci```
18331cb0ef41Sopenharmony_ci
18341cb0ef41Sopenharmony_ci```cjs
18351cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
18361cb0ef41Sopenharmony_ci
18371cb0ef41Sopenharmony_ciassert.notStrictEqual(1, 2);
18381cb0ef41Sopenharmony_ci// OK
18391cb0ef41Sopenharmony_ci
18401cb0ef41Sopenharmony_ciassert.notStrictEqual(1, 1);
18411cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:
18421cb0ef41Sopenharmony_ci//
18431cb0ef41Sopenharmony_ci// 1
18441cb0ef41Sopenharmony_ci
18451cb0ef41Sopenharmony_ciassert.notStrictEqual(1, '1');
18461cb0ef41Sopenharmony_ci// OK
18471cb0ef41Sopenharmony_ci```
18481cb0ef41Sopenharmony_ci
18491cb0ef41Sopenharmony_ciIf the values are strictly equal, an [`AssertionError`][] is thrown with a
18501cb0ef41Sopenharmony_ci`message` property set equal to the value of the `message` parameter. If the
18511cb0ef41Sopenharmony_ci`message` parameter is undefined, a default error message is assigned. If the
18521cb0ef41Sopenharmony_ci`message` parameter is an instance of an [`Error`][] then it will be thrown
18531cb0ef41Sopenharmony_ciinstead of the `AssertionError`.
18541cb0ef41Sopenharmony_ci
18551cb0ef41Sopenharmony_ci## `assert.ok(value[, message])`
18561cb0ef41Sopenharmony_ci
18571cb0ef41Sopenharmony_ci<!-- YAML
18581cb0ef41Sopenharmony_ciadded: v0.1.21
18591cb0ef41Sopenharmony_cichanges:
18601cb0ef41Sopenharmony_ci  - version: v10.0.0
18611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18319
18621cb0ef41Sopenharmony_ci    description: The `assert.ok()` (no arguments) will now use a predefined
18631cb0ef41Sopenharmony_ci                 error message.
18641cb0ef41Sopenharmony_ci-->
18651cb0ef41Sopenharmony_ci
18661cb0ef41Sopenharmony_ci* `value` {any}
18671cb0ef41Sopenharmony_ci* `message` {string|Error}
18681cb0ef41Sopenharmony_ci
18691cb0ef41Sopenharmony_ciTests if `value` is truthy. It is equivalent to
18701cb0ef41Sopenharmony_ci`assert.equal(!!value, true, message)`.
18711cb0ef41Sopenharmony_ci
18721cb0ef41Sopenharmony_ciIf `value` is not truthy, an [`AssertionError`][] is thrown with a `message`
18731cb0ef41Sopenharmony_ciproperty set equal to the value of the `message` parameter. If the `message`
18741cb0ef41Sopenharmony_ciparameter is `undefined`, a default error message is assigned. If the `message`
18751cb0ef41Sopenharmony_ciparameter is an instance of an [`Error`][] then it will be thrown instead of the
18761cb0ef41Sopenharmony_ci`AssertionError`.
18771cb0ef41Sopenharmony_ciIf no arguments are passed in at all `message` will be set to the string:
18781cb0ef41Sopenharmony_ci``'No value argument passed to `assert.ok()`'``.
18791cb0ef41Sopenharmony_ci
18801cb0ef41Sopenharmony_ciBe aware that in the `repl` the error message will be different to the one
18811cb0ef41Sopenharmony_cithrown in a file! See below for further details.
18821cb0ef41Sopenharmony_ci
18831cb0ef41Sopenharmony_ci```mjs
18841cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
18851cb0ef41Sopenharmony_ci
18861cb0ef41Sopenharmony_ciassert.ok(true);
18871cb0ef41Sopenharmony_ci// OK
18881cb0ef41Sopenharmony_ciassert.ok(1);
18891cb0ef41Sopenharmony_ci// OK
18901cb0ef41Sopenharmony_ci
18911cb0ef41Sopenharmony_ciassert.ok();
18921cb0ef41Sopenharmony_ci// AssertionError: No value argument passed to `assert.ok()`
18931cb0ef41Sopenharmony_ci
18941cb0ef41Sopenharmony_ciassert.ok(false, 'it\'s false');
18951cb0ef41Sopenharmony_ci// AssertionError: it's false
18961cb0ef41Sopenharmony_ci
18971cb0ef41Sopenharmony_ci// In the repl:
18981cb0ef41Sopenharmony_ciassert.ok(typeof 123 === 'string');
18991cb0ef41Sopenharmony_ci// AssertionError: false == true
19001cb0ef41Sopenharmony_ci
19011cb0ef41Sopenharmony_ci// In a file (e.g. test.js):
19021cb0ef41Sopenharmony_ciassert.ok(typeof 123 === 'string');
19031cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19041cb0ef41Sopenharmony_ci//
19051cb0ef41Sopenharmony_ci//   assert.ok(typeof 123 === 'string')
19061cb0ef41Sopenharmony_ci
19071cb0ef41Sopenharmony_ciassert.ok(false);
19081cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19091cb0ef41Sopenharmony_ci//
19101cb0ef41Sopenharmony_ci//   assert.ok(false)
19111cb0ef41Sopenharmony_ci
19121cb0ef41Sopenharmony_ciassert.ok(0);
19131cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19141cb0ef41Sopenharmony_ci//
19151cb0ef41Sopenharmony_ci//   assert.ok(0)
19161cb0ef41Sopenharmony_ci```
19171cb0ef41Sopenharmony_ci
19181cb0ef41Sopenharmony_ci```cjs
19191cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
19201cb0ef41Sopenharmony_ci
19211cb0ef41Sopenharmony_ciassert.ok(true);
19221cb0ef41Sopenharmony_ci// OK
19231cb0ef41Sopenharmony_ciassert.ok(1);
19241cb0ef41Sopenharmony_ci// OK
19251cb0ef41Sopenharmony_ci
19261cb0ef41Sopenharmony_ciassert.ok();
19271cb0ef41Sopenharmony_ci// AssertionError: No value argument passed to `assert.ok()`
19281cb0ef41Sopenharmony_ci
19291cb0ef41Sopenharmony_ciassert.ok(false, 'it\'s false');
19301cb0ef41Sopenharmony_ci// AssertionError: it's false
19311cb0ef41Sopenharmony_ci
19321cb0ef41Sopenharmony_ci// In the repl:
19331cb0ef41Sopenharmony_ciassert.ok(typeof 123 === 'string');
19341cb0ef41Sopenharmony_ci// AssertionError: false == true
19351cb0ef41Sopenharmony_ci
19361cb0ef41Sopenharmony_ci// In a file (e.g. test.js):
19371cb0ef41Sopenharmony_ciassert.ok(typeof 123 === 'string');
19381cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19391cb0ef41Sopenharmony_ci//
19401cb0ef41Sopenharmony_ci//   assert.ok(typeof 123 === 'string')
19411cb0ef41Sopenharmony_ci
19421cb0ef41Sopenharmony_ciassert.ok(false);
19431cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19441cb0ef41Sopenharmony_ci//
19451cb0ef41Sopenharmony_ci//   assert.ok(false)
19461cb0ef41Sopenharmony_ci
19471cb0ef41Sopenharmony_ciassert.ok(0);
19481cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19491cb0ef41Sopenharmony_ci//
19501cb0ef41Sopenharmony_ci//   assert.ok(0)
19511cb0ef41Sopenharmony_ci```
19521cb0ef41Sopenharmony_ci
19531cb0ef41Sopenharmony_ci```mjs
19541cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
19551cb0ef41Sopenharmony_ci
19561cb0ef41Sopenharmony_ci// Using `assert()` works the same:
19571cb0ef41Sopenharmony_ciassert(0);
19581cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19591cb0ef41Sopenharmony_ci//
19601cb0ef41Sopenharmony_ci//   assert(0)
19611cb0ef41Sopenharmony_ci```
19621cb0ef41Sopenharmony_ci
19631cb0ef41Sopenharmony_ci```cjs
19641cb0ef41Sopenharmony_ciconst assert = require('node:assert');
19651cb0ef41Sopenharmony_ci
19661cb0ef41Sopenharmony_ci// Using `assert()` works the same:
19671cb0ef41Sopenharmony_ciassert(0);
19681cb0ef41Sopenharmony_ci// AssertionError: The expression evaluated to a falsy value:
19691cb0ef41Sopenharmony_ci//
19701cb0ef41Sopenharmony_ci//   assert(0)
19711cb0ef41Sopenharmony_ci```
19721cb0ef41Sopenharmony_ci
19731cb0ef41Sopenharmony_ci## `assert.rejects(asyncFn[, error][, message])`
19741cb0ef41Sopenharmony_ci
19751cb0ef41Sopenharmony_ci<!-- YAML
19761cb0ef41Sopenharmony_ciadded: v10.0.0
19771cb0ef41Sopenharmony_ci-->
19781cb0ef41Sopenharmony_ci
19791cb0ef41Sopenharmony_ci* `asyncFn` {Function|Promise}
19801cb0ef41Sopenharmony_ci* `error` {RegExp|Function|Object|Error}
19811cb0ef41Sopenharmony_ci* `message` {string}
19821cb0ef41Sopenharmony_ci
19831cb0ef41Sopenharmony_ciAwaits the `asyncFn` promise or, if `asyncFn` is a function, immediately
19841cb0ef41Sopenharmony_cicalls the function and awaits the returned promise to complete. It will then
19851cb0ef41Sopenharmony_cicheck that the promise is rejected.
19861cb0ef41Sopenharmony_ci
19871cb0ef41Sopenharmony_ciIf `asyncFn` is a function and it throws an error synchronously,
19881cb0ef41Sopenharmony_ci`assert.rejects()` will return a rejected `Promise` with that error. If the
19891cb0ef41Sopenharmony_cifunction does not return a promise, `assert.rejects()` will return a rejected
19901cb0ef41Sopenharmony_ci`Promise` with an [`ERR_INVALID_RETURN_VALUE`][] error. In both cases the error
19911cb0ef41Sopenharmony_cihandler is skipped.
19921cb0ef41Sopenharmony_ci
19931cb0ef41Sopenharmony_ciBesides the async nature to await the completion behaves identically to
19941cb0ef41Sopenharmony_ci[`assert.throws()`][].
19951cb0ef41Sopenharmony_ci
19961cb0ef41Sopenharmony_ciIf specified, `error` can be a [`Class`][], [`RegExp`][], a validation function,
19971cb0ef41Sopenharmony_cian object where each property will be tested for, or an instance of error where
19981cb0ef41Sopenharmony_cieach property will be tested for including the non-enumerable `message` and
19991cb0ef41Sopenharmony_ci`name` properties.
20001cb0ef41Sopenharmony_ci
20011cb0ef41Sopenharmony_ciIf specified, `message` will be the message provided by the [`AssertionError`][]
20021cb0ef41Sopenharmony_ciif the `asyncFn` fails to reject.
20031cb0ef41Sopenharmony_ci
20041cb0ef41Sopenharmony_ci```mjs
20051cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
20061cb0ef41Sopenharmony_ci
20071cb0ef41Sopenharmony_ciawait assert.rejects(
20081cb0ef41Sopenharmony_ci  async () => {
20091cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
20101cb0ef41Sopenharmony_ci  },
20111cb0ef41Sopenharmony_ci  {
20121cb0ef41Sopenharmony_ci    name: 'TypeError',
20131cb0ef41Sopenharmony_ci    message: 'Wrong value',
20141cb0ef41Sopenharmony_ci  },
20151cb0ef41Sopenharmony_ci);
20161cb0ef41Sopenharmony_ci```
20171cb0ef41Sopenharmony_ci
20181cb0ef41Sopenharmony_ci```cjs
20191cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
20201cb0ef41Sopenharmony_ci
20211cb0ef41Sopenharmony_ci(async () => {
20221cb0ef41Sopenharmony_ci  await assert.rejects(
20231cb0ef41Sopenharmony_ci    async () => {
20241cb0ef41Sopenharmony_ci      throw new TypeError('Wrong value');
20251cb0ef41Sopenharmony_ci    },
20261cb0ef41Sopenharmony_ci    {
20271cb0ef41Sopenharmony_ci      name: 'TypeError',
20281cb0ef41Sopenharmony_ci      message: 'Wrong value',
20291cb0ef41Sopenharmony_ci    },
20301cb0ef41Sopenharmony_ci  );
20311cb0ef41Sopenharmony_ci})();
20321cb0ef41Sopenharmony_ci```
20331cb0ef41Sopenharmony_ci
20341cb0ef41Sopenharmony_ci```mjs
20351cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
20361cb0ef41Sopenharmony_ci
20371cb0ef41Sopenharmony_ciawait assert.rejects(
20381cb0ef41Sopenharmony_ci  async () => {
20391cb0ef41Sopenharmony_ci    throw new TypeError('Wrong value');
20401cb0ef41Sopenharmony_ci  },
20411cb0ef41Sopenharmony_ci  (err) => {
20421cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'TypeError');
20431cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'Wrong value');
20441cb0ef41Sopenharmony_ci    return true;
20451cb0ef41Sopenharmony_ci  },
20461cb0ef41Sopenharmony_ci);
20471cb0ef41Sopenharmony_ci```
20481cb0ef41Sopenharmony_ci
20491cb0ef41Sopenharmony_ci```cjs
20501cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
20511cb0ef41Sopenharmony_ci
20521cb0ef41Sopenharmony_ci(async () => {
20531cb0ef41Sopenharmony_ci  await assert.rejects(
20541cb0ef41Sopenharmony_ci    async () => {
20551cb0ef41Sopenharmony_ci      throw new TypeError('Wrong value');
20561cb0ef41Sopenharmony_ci    },
20571cb0ef41Sopenharmony_ci    (err) => {
20581cb0ef41Sopenharmony_ci      assert.strictEqual(err.name, 'TypeError');
20591cb0ef41Sopenharmony_ci      assert.strictEqual(err.message, 'Wrong value');
20601cb0ef41Sopenharmony_ci      return true;
20611cb0ef41Sopenharmony_ci    },
20621cb0ef41Sopenharmony_ci  );
20631cb0ef41Sopenharmony_ci})();
20641cb0ef41Sopenharmony_ci```
20651cb0ef41Sopenharmony_ci
20661cb0ef41Sopenharmony_ci```mjs
20671cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
20681cb0ef41Sopenharmony_ci
20691cb0ef41Sopenharmony_ciassert.rejects(
20701cb0ef41Sopenharmony_ci  Promise.reject(new Error('Wrong value')),
20711cb0ef41Sopenharmony_ci  Error,
20721cb0ef41Sopenharmony_ci).then(() => {
20731cb0ef41Sopenharmony_ci  // ...
20741cb0ef41Sopenharmony_ci});
20751cb0ef41Sopenharmony_ci```
20761cb0ef41Sopenharmony_ci
20771cb0ef41Sopenharmony_ci```cjs
20781cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
20791cb0ef41Sopenharmony_ci
20801cb0ef41Sopenharmony_ciassert.rejects(
20811cb0ef41Sopenharmony_ci  Promise.reject(new Error('Wrong value')),
20821cb0ef41Sopenharmony_ci  Error,
20831cb0ef41Sopenharmony_ci).then(() => {
20841cb0ef41Sopenharmony_ci  // ...
20851cb0ef41Sopenharmony_ci});
20861cb0ef41Sopenharmony_ci```
20871cb0ef41Sopenharmony_ci
20881cb0ef41Sopenharmony_ci`error` cannot be a string. If a string is provided as the second
20891cb0ef41Sopenharmony_ciargument, then `error` is assumed to be omitted and the string will be used for
20901cb0ef41Sopenharmony_ci`message` instead. This can lead to easy-to-miss mistakes. Please read the
20911cb0ef41Sopenharmony_ciexample in [`assert.throws()`][] carefully if using a string as the second
20921cb0ef41Sopenharmony_ciargument gets considered.
20931cb0ef41Sopenharmony_ci
20941cb0ef41Sopenharmony_ci## `assert.strictEqual(actual, expected[, message])`
20951cb0ef41Sopenharmony_ci
20961cb0ef41Sopenharmony_ci<!-- YAML
20971cb0ef41Sopenharmony_ciadded: v0.1.21
20981cb0ef41Sopenharmony_cichanges:
20991cb0ef41Sopenharmony_ci  - version: v10.0.0
21001cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17003
21011cb0ef41Sopenharmony_ci    description: Used comparison changed from Strict Equality to `Object.is()`.
21021cb0ef41Sopenharmony_ci-->
21031cb0ef41Sopenharmony_ci
21041cb0ef41Sopenharmony_ci* `actual` {any}
21051cb0ef41Sopenharmony_ci* `expected` {any}
21061cb0ef41Sopenharmony_ci* `message` {string|Error}
21071cb0ef41Sopenharmony_ci
21081cb0ef41Sopenharmony_ciTests strict equality between the `actual` and `expected` parameters as
21091cb0ef41Sopenharmony_cidetermined by [`Object.is()`][].
21101cb0ef41Sopenharmony_ci
21111cb0ef41Sopenharmony_ci```mjs
21121cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
21131cb0ef41Sopenharmony_ci
21141cb0ef41Sopenharmony_ciassert.strictEqual(1, 2);
21151cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
21161cb0ef41Sopenharmony_ci//
21171cb0ef41Sopenharmony_ci// 1 !== 2
21181cb0ef41Sopenharmony_ci
21191cb0ef41Sopenharmony_ciassert.strictEqual(1, 1);
21201cb0ef41Sopenharmony_ci// OK
21211cb0ef41Sopenharmony_ci
21221cb0ef41Sopenharmony_ciassert.strictEqual('Hello foobar', 'Hello World!');
21231cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
21241cb0ef41Sopenharmony_ci// + actual - expected
21251cb0ef41Sopenharmony_ci//
21261cb0ef41Sopenharmony_ci// + 'Hello foobar'
21271cb0ef41Sopenharmony_ci// - 'Hello World!'
21281cb0ef41Sopenharmony_ci//          ^
21291cb0ef41Sopenharmony_ci
21301cb0ef41Sopenharmony_ciconst apples = 1;
21311cb0ef41Sopenharmony_ciconst oranges = 2;
21321cb0ef41Sopenharmony_ciassert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
21331cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
21341cb0ef41Sopenharmony_ci
21351cb0ef41Sopenharmony_ciassert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
21361cb0ef41Sopenharmony_ci// TypeError: Inputs are not identical
21371cb0ef41Sopenharmony_ci```
21381cb0ef41Sopenharmony_ci
21391cb0ef41Sopenharmony_ci```cjs
21401cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
21411cb0ef41Sopenharmony_ci
21421cb0ef41Sopenharmony_ciassert.strictEqual(1, 2);
21431cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
21441cb0ef41Sopenharmony_ci//
21451cb0ef41Sopenharmony_ci// 1 !== 2
21461cb0ef41Sopenharmony_ci
21471cb0ef41Sopenharmony_ciassert.strictEqual(1, 1);
21481cb0ef41Sopenharmony_ci// OK
21491cb0ef41Sopenharmony_ci
21501cb0ef41Sopenharmony_ciassert.strictEqual('Hello foobar', 'Hello World!');
21511cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
21521cb0ef41Sopenharmony_ci// + actual - expected
21531cb0ef41Sopenharmony_ci//
21541cb0ef41Sopenharmony_ci// + 'Hello foobar'
21551cb0ef41Sopenharmony_ci// - 'Hello World!'
21561cb0ef41Sopenharmony_ci//          ^
21571cb0ef41Sopenharmony_ci
21581cb0ef41Sopenharmony_ciconst apples = 1;
21591cb0ef41Sopenharmony_ciconst oranges = 2;
21601cb0ef41Sopenharmony_ciassert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
21611cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
21621cb0ef41Sopenharmony_ci
21631cb0ef41Sopenharmony_ciassert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
21641cb0ef41Sopenharmony_ci// TypeError: Inputs are not identical
21651cb0ef41Sopenharmony_ci```
21661cb0ef41Sopenharmony_ci
21671cb0ef41Sopenharmony_ciIf the values are not strictly equal, an [`AssertionError`][] is thrown with a
21681cb0ef41Sopenharmony_ci`message` property set equal to the value of the `message` parameter. If the
21691cb0ef41Sopenharmony_ci`message` parameter is undefined, a default error message is assigned. If the
21701cb0ef41Sopenharmony_ci`message` parameter is an instance of an [`Error`][] then it will be thrown
21711cb0ef41Sopenharmony_ciinstead of the [`AssertionError`][].
21721cb0ef41Sopenharmony_ci
21731cb0ef41Sopenharmony_ci## `assert.throws(fn[, error][, message])`
21741cb0ef41Sopenharmony_ci
21751cb0ef41Sopenharmony_ci<!-- YAML
21761cb0ef41Sopenharmony_ciadded: v0.1.21
21771cb0ef41Sopenharmony_cichanges:
21781cb0ef41Sopenharmony_ci  - version: v10.2.0
21791cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/20485
21801cb0ef41Sopenharmony_ci    description: The `error` parameter can be an object containing regular
21811cb0ef41Sopenharmony_ci                 expressions now.
21821cb0ef41Sopenharmony_ci  - version: v9.9.0
21831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17584
21841cb0ef41Sopenharmony_ci    description: The `error` parameter can now be an object as well.
21851cb0ef41Sopenharmony_ci  - version: v4.2.0
21861cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/3276
21871cb0ef41Sopenharmony_ci    description: The `error` parameter can now be an arrow function.
21881cb0ef41Sopenharmony_ci-->
21891cb0ef41Sopenharmony_ci
21901cb0ef41Sopenharmony_ci* `fn` {Function}
21911cb0ef41Sopenharmony_ci* `error` {RegExp|Function|Object|Error}
21921cb0ef41Sopenharmony_ci* `message` {string}
21931cb0ef41Sopenharmony_ci
21941cb0ef41Sopenharmony_ciExpects the function `fn` to throw an error.
21951cb0ef41Sopenharmony_ci
21961cb0ef41Sopenharmony_ciIf specified, `error` can be a [`Class`][], [`RegExp`][], a validation function,
21971cb0ef41Sopenharmony_cia validation object where each property will be tested for strict deep equality,
21981cb0ef41Sopenharmony_cior an instance of error where each property will be tested for strict deep
21991cb0ef41Sopenharmony_ciequality including the non-enumerable `message` and `name` properties. When
22001cb0ef41Sopenharmony_ciusing an object, it is also possible to use a regular expression, when
22011cb0ef41Sopenharmony_civalidating against a string property. See below for examples.
22021cb0ef41Sopenharmony_ci
22031cb0ef41Sopenharmony_ciIf specified, `message` will be appended to the message provided by the
22041cb0ef41Sopenharmony_ci`AssertionError` if the `fn` call fails to throw or in case the error validation
22051cb0ef41Sopenharmony_cifails.
22061cb0ef41Sopenharmony_ci
22071cb0ef41Sopenharmony_ciCustom validation object/error instance:
22081cb0ef41Sopenharmony_ci
22091cb0ef41Sopenharmony_ci```mjs
22101cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
22111cb0ef41Sopenharmony_ci
22121cb0ef41Sopenharmony_ciconst err = new TypeError('Wrong value');
22131cb0ef41Sopenharmony_cierr.code = 404;
22141cb0ef41Sopenharmony_cierr.foo = 'bar';
22151cb0ef41Sopenharmony_cierr.info = {
22161cb0ef41Sopenharmony_ci  nested: true,
22171cb0ef41Sopenharmony_ci  baz: 'text',
22181cb0ef41Sopenharmony_ci};
22191cb0ef41Sopenharmony_cierr.reg = /abc/i;
22201cb0ef41Sopenharmony_ci
22211cb0ef41Sopenharmony_ciassert.throws(
22221cb0ef41Sopenharmony_ci  () => {
22231cb0ef41Sopenharmony_ci    throw err;
22241cb0ef41Sopenharmony_ci  },
22251cb0ef41Sopenharmony_ci  {
22261cb0ef41Sopenharmony_ci    name: 'TypeError',
22271cb0ef41Sopenharmony_ci    message: 'Wrong value',
22281cb0ef41Sopenharmony_ci    info: {
22291cb0ef41Sopenharmony_ci      nested: true,
22301cb0ef41Sopenharmony_ci      baz: 'text',
22311cb0ef41Sopenharmony_ci    },
22321cb0ef41Sopenharmony_ci    // Only properties on the validation object will be tested for.
22331cb0ef41Sopenharmony_ci    // Using nested objects requires all properties to be present. Otherwise
22341cb0ef41Sopenharmony_ci    // the validation is going to fail.
22351cb0ef41Sopenharmony_ci  },
22361cb0ef41Sopenharmony_ci);
22371cb0ef41Sopenharmony_ci
22381cb0ef41Sopenharmony_ci// Using regular expressions to validate error properties:
22391cb0ef41Sopenharmony_ciassert.throws(
22401cb0ef41Sopenharmony_ci  () => {
22411cb0ef41Sopenharmony_ci    throw err;
22421cb0ef41Sopenharmony_ci  },
22431cb0ef41Sopenharmony_ci  {
22441cb0ef41Sopenharmony_ci    // The `name` and `message` properties are strings and using regular
22451cb0ef41Sopenharmony_ci    // expressions on those will match against the string. If they fail, an
22461cb0ef41Sopenharmony_ci    // error is thrown.
22471cb0ef41Sopenharmony_ci    name: /^TypeError$/,
22481cb0ef41Sopenharmony_ci    message: /Wrong/,
22491cb0ef41Sopenharmony_ci    foo: 'bar',
22501cb0ef41Sopenharmony_ci    info: {
22511cb0ef41Sopenharmony_ci      nested: true,
22521cb0ef41Sopenharmony_ci      // It is not possible to use regular expressions for nested properties!
22531cb0ef41Sopenharmony_ci      baz: 'text',
22541cb0ef41Sopenharmony_ci    },
22551cb0ef41Sopenharmony_ci    // The `reg` property contains a regular expression and only if the
22561cb0ef41Sopenharmony_ci    // validation object contains an identical regular expression, it is going
22571cb0ef41Sopenharmony_ci    // to pass.
22581cb0ef41Sopenharmony_ci    reg: /abc/i,
22591cb0ef41Sopenharmony_ci  },
22601cb0ef41Sopenharmony_ci);
22611cb0ef41Sopenharmony_ci
22621cb0ef41Sopenharmony_ci// Fails due to the different `message` and `name` properties:
22631cb0ef41Sopenharmony_ciassert.throws(
22641cb0ef41Sopenharmony_ci  () => {
22651cb0ef41Sopenharmony_ci    const otherErr = new Error('Not found');
22661cb0ef41Sopenharmony_ci    // Copy all enumerable properties from `err` to `otherErr`.
22671cb0ef41Sopenharmony_ci    for (const [key, value] of Object.entries(err)) {
22681cb0ef41Sopenharmony_ci      otherErr[key] = value;
22691cb0ef41Sopenharmony_ci    }
22701cb0ef41Sopenharmony_ci    throw otherErr;
22711cb0ef41Sopenharmony_ci  },
22721cb0ef41Sopenharmony_ci  // The error's `message` and `name` properties will also be checked when using
22731cb0ef41Sopenharmony_ci  // an error as validation object.
22741cb0ef41Sopenharmony_ci  err,
22751cb0ef41Sopenharmony_ci);
22761cb0ef41Sopenharmony_ci```
22771cb0ef41Sopenharmony_ci
22781cb0ef41Sopenharmony_ci```cjs
22791cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
22801cb0ef41Sopenharmony_ci
22811cb0ef41Sopenharmony_ciconst err = new TypeError('Wrong value');
22821cb0ef41Sopenharmony_cierr.code = 404;
22831cb0ef41Sopenharmony_cierr.foo = 'bar';
22841cb0ef41Sopenharmony_cierr.info = {
22851cb0ef41Sopenharmony_ci  nested: true,
22861cb0ef41Sopenharmony_ci  baz: 'text',
22871cb0ef41Sopenharmony_ci};
22881cb0ef41Sopenharmony_cierr.reg = /abc/i;
22891cb0ef41Sopenharmony_ci
22901cb0ef41Sopenharmony_ciassert.throws(
22911cb0ef41Sopenharmony_ci  () => {
22921cb0ef41Sopenharmony_ci    throw err;
22931cb0ef41Sopenharmony_ci  },
22941cb0ef41Sopenharmony_ci  {
22951cb0ef41Sopenharmony_ci    name: 'TypeError',
22961cb0ef41Sopenharmony_ci    message: 'Wrong value',
22971cb0ef41Sopenharmony_ci    info: {
22981cb0ef41Sopenharmony_ci      nested: true,
22991cb0ef41Sopenharmony_ci      baz: 'text',
23001cb0ef41Sopenharmony_ci    },
23011cb0ef41Sopenharmony_ci    // Only properties on the validation object will be tested for.
23021cb0ef41Sopenharmony_ci    // Using nested objects requires all properties to be present. Otherwise
23031cb0ef41Sopenharmony_ci    // the validation is going to fail.
23041cb0ef41Sopenharmony_ci  },
23051cb0ef41Sopenharmony_ci);
23061cb0ef41Sopenharmony_ci
23071cb0ef41Sopenharmony_ci// Using regular expressions to validate error properties:
23081cb0ef41Sopenharmony_ciassert.throws(
23091cb0ef41Sopenharmony_ci  () => {
23101cb0ef41Sopenharmony_ci    throw err;
23111cb0ef41Sopenharmony_ci  },
23121cb0ef41Sopenharmony_ci  {
23131cb0ef41Sopenharmony_ci    // The `name` and `message` properties are strings and using regular
23141cb0ef41Sopenharmony_ci    // expressions on those will match against the string. If they fail, an
23151cb0ef41Sopenharmony_ci    // error is thrown.
23161cb0ef41Sopenharmony_ci    name: /^TypeError$/,
23171cb0ef41Sopenharmony_ci    message: /Wrong/,
23181cb0ef41Sopenharmony_ci    foo: 'bar',
23191cb0ef41Sopenharmony_ci    info: {
23201cb0ef41Sopenharmony_ci      nested: true,
23211cb0ef41Sopenharmony_ci      // It is not possible to use regular expressions for nested properties!
23221cb0ef41Sopenharmony_ci      baz: 'text',
23231cb0ef41Sopenharmony_ci    },
23241cb0ef41Sopenharmony_ci    // The `reg` property contains a regular expression and only if the
23251cb0ef41Sopenharmony_ci    // validation object contains an identical regular expression, it is going
23261cb0ef41Sopenharmony_ci    // to pass.
23271cb0ef41Sopenharmony_ci    reg: /abc/i,
23281cb0ef41Sopenharmony_ci  },
23291cb0ef41Sopenharmony_ci);
23301cb0ef41Sopenharmony_ci
23311cb0ef41Sopenharmony_ci// Fails due to the different `message` and `name` properties:
23321cb0ef41Sopenharmony_ciassert.throws(
23331cb0ef41Sopenharmony_ci  () => {
23341cb0ef41Sopenharmony_ci    const otherErr = new Error('Not found');
23351cb0ef41Sopenharmony_ci    // Copy all enumerable properties from `err` to `otherErr`.
23361cb0ef41Sopenharmony_ci    for (const [key, value] of Object.entries(err)) {
23371cb0ef41Sopenharmony_ci      otherErr[key] = value;
23381cb0ef41Sopenharmony_ci    }
23391cb0ef41Sopenharmony_ci    throw otherErr;
23401cb0ef41Sopenharmony_ci  },
23411cb0ef41Sopenharmony_ci  // The error's `message` and `name` properties will also be checked when using
23421cb0ef41Sopenharmony_ci  // an error as validation object.
23431cb0ef41Sopenharmony_ci  err,
23441cb0ef41Sopenharmony_ci);
23451cb0ef41Sopenharmony_ci```
23461cb0ef41Sopenharmony_ci
23471cb0ef41Sopenharmony_ciValidate instanceof using constructor:
23481cb0ef41Sopenharmony_ci
23491cb0ef41Sopenharmony_ci```mjs
23501cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
23511cb0ef41Sopenharmony_ci
23521cb0ef41Sopenharmony_ciassert.throws(
23531cb0ef41Sopenharmony_ci  () => {
23541cb0ef41Sopenharmony_ci    throw new Error('Wrong value');
23551cb0ef41Sopenharmony_ci  },
23561cb0ef41Sopenharmony_ci  Error,
23571cb0ef41Sopenharmony_ci);
23581cb0ef41Sopenharmony_ci```
23591cb0ef41Sopenharmony_ci
23601cb0ef41Sopenharmony_ci```cjs
23611cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
23621cb0ef41Sopenharmony_ci
23631cb0ef41Sopenharmony_ciassert.throws(
23641cb0ef41Sopenharmony_ci  () => {
23651cb0ef41Sopenharmony_ci    throw new Error('Wrong value');
23661cb0ef41Sopenharmony_ci  },
23671cb0ef41Sopenharmony_ci  Error,
23681cb0ef41Sopenharmony_ci);
23691cb0ef41Sopenharmony_ci```
23701cb0ef41Sopenharmony_ci
23711cb0ef41Sopenharmony_ciValidate error message using [`RegExp`][]:
23721cb0ef41Sopenharmony_ci
23731cb0ef41Sopenharmony_ciUsing a regular expression runs `.toString` on the error object, and will
23741cb0ef41Sopenharmony_citherefore also include the error name.
23751cb0ef41Sopenharmony_ci
23761cb0ef41Sopenharmony_ci```mjs
23771cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
23781cb0ef41Sopenharmony_ci
23791cb0ef41Sopenharmony_ciassert.throws(
23801cb0ef41Sopenharmony_ci  () => {
23811cb0ef41Sopenharmony_ci    throw new Error('Wrong value');
23821cb0ef41Sopenharmony_ci  },
23831cb0ef41Sopenharmony_ci  /^Error: Wrong value$/,
23841cb0ef41Sopenharmony_ci);
23851cb0ef41Sopenharmony_ci```
23861cb0ef41Sopenharmony_ci
23871cb0ef41Sopenharmony_ci```cjs
23881cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
23891cb0ef41Sopenharmony_ci
23901cb0ef41Sopenharmony_ciassert.throws(
23911cb0ef41Sopenharmony_ci  () => {
23921cb0ef41Sopenharmony_ci    throw new Error('Wrong value');
23931cb0ef41Sopenharmony_ci  },
23941cb0ef41Sopenharmony_ci  /^Error: Wrong value$/,
23951cb0ef41Sopenharmony_ci);
23961cb0ef41Sopenharmony_ci```
23971cb0ef41Sopenharmony_ci
23981cb0ef41Sopenharmony_ciCustom error validation:
23991cb0ef41Sopenharmony_ci
24001cb0ef41Sopenharmony_ciThe function must return `true` to indicate all internal validations passed.
24011cb0ef41Sopenharmony_ciIt will otherwise fail with an [`AssertionError`][].
24021cb0ef41Sopenharmony_ci
24031cb0ef41Sopenharmony_ci```mjs
24041cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
24051cb0ef41Sopenharmony_ci
24061cb0ef41Sopenharmony_ciassert.throws(
24071cb0ef41Sopenharmony_ci  () => {
24081cb0ef41Sopenharmony_ci    throw new Error('Wrong value');
24091cb0ef41Sopenharmony_ci  },
24101cb0ef41Sopenharmony_ci  (err) => {
24111cb0ef41Sopenharmony_ci    assert(err instanceof Error);
24121cb0ef41Sopenharmony_ci    assert(/value/.test(err));
24131cb0ef41Sopenharmony_ci    // Avoid returning anything from validation functions besides `true`.
24141cb0ef41Sopenharmony_ci    // Otherwise, it's not clear what part of the validation failed. Instead,
24151cb0ef41Sopenharmony_ci    // throw an error about the specific validation that failed (as done in this
24161cb0ef41Sopenharmony_ci    // example) and add as much helpful debugging information to that error as
24171cb0ef41Sopenharmony_ci    // possible.
24181cb0ef41Sopenharmony_ci    return true;
24191cb0ef41Sopenharmony_ci  },
24201cb0ef41Sopenharmony_ci  'unexpected error',
24211cb0ef41Sopenharmony_ci);
24221cb0ef41Sopenharmony_ci```
24231cb0ef41Sopenharmony_ci
24241cb0ef41Sopenharmony_ci```cjs
24251cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
24261cb0ef41Sopenharmony_ci
24271cb0ef41Sopenharmony_ciassert.throws(
24281cb0ef41Sopenharmony_ci  () => {
24291cb0ef41Sopenharmony_ci    throw new Error('Wrong value');
24301cb0ef41Sopenharmony_ci  },
24311cb0ef41Sopenharmony_ci  (err) => {
24321cb0ef41Sopenharmony_ci    assert(err instanceof Error);
24331cb0ef41Sopenharmony_ci    assert(/value/.test(err));
24341cb0ef41Sopenharmony_ci    // Avoid returning anything from validation functions besides `true`.
24351cb0ef41Sopenharmony_ci    // Otherwise, it's not clear what part of the validation failed. Instead,
24361cb0ef41Sopenharmony_ci    // throw an error about the specific validation that failed (as done in this
24371cb0ef41Sopenharmony_ci    // example) and add as much helpful debugging information to that error as
24381cb0ef41Sopenharmony_ci    // possible.
24391cb0ef41Sopenharmony_ci    return true;
24401cb0ef41Sopenharmony_ci  },
24411cb0ef41Sopenharmony_ci  'unexpected error',
24421cb0ef41Sopenharmony_ci);
24431cb0ef41Sopenharmony_ci```
24441cb0ef41Sopenharmony_ci
24451cb0ef41Sopenharmony_ci`error` cannot be a string. If a string is provided as the second
24461cb0ef41Sopenharmony_ciargument, then `error` is assumed to be omitted and the string will be used for
24471cb0ef41Sopenharmony_ci`message` instead. This can lead to easy-to-miss mistakes. Using the same
24481cb0ef41Sopenharmony_cimessage as the thrown error message is going to result in an
24491cb0ef41Sopenharmony_ci`ERR_AMBIGUOUS_ARGUMENT` error. Please read the example below carefully if using
24501cb0ef41Sopenharmony_cia string as the second argument gets considered:
24511cb0ef41Sopenharmony_ci
24521cb0ef41Sopenharmony_ci```mjs
24531cb0ef41Sopenharmony_ciimport assert from 'node:assert/strict';
24541cb0ef41Sopenharmony_ci
24551cb0ef41Sopenharmony_cifunction throwingFirst() {
24561cb0ef41Sopenharmony_ci  throw new Error('First');
24571cb0ef41Sopenharmony_ci}
24581cb0ef41Sopenharmony_ci
24591cb0ef41Sopenharmony_cifunction throwingSecond() {
24601cb0ef41Sopenharmony_ci  throw new Error('Second');
24611cb0ef41Sopenharmony_ci}
24621cb0ef41Sopenharmony_ci
24631cb0ef41Sopenharmony_cifunction notThrowing() {}
24641cb0ef41Sopenharmony_ci
24651cb0ef41Sopenharmony_ci// The second argument is a string and the input function threw an Error.
24661cb0ef41Sopenharmony_ci// The first case will not throw as it does not match for the error message
24671cb0ef41Sopenharmony_ci// thrown by the input function!
24681cb0ef41Sopenharmony_ciassert.throws(throwingFirst, 'Second');
24691cb0ef41Sopenharmony_ci// In the next example the message has no benefit over the message from the
24701cb0ef41Sopenharmony_ci// error and since it is not clear if the user intended to actually match
24711cb0ef41Sopenharmony_ci// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.
24721cb0ef41Sopenharmony_ciassert.throws(throwingSecond, 'Second');
24731cb0ef41Sopenharmony_ci// TypeError [ERR_AMBIGUOUS_ARGUMENT]
24741cb0ef41Sopenharmony_ci
24751cb0ef41Sopenharmony_ci// The string is only used (as message) in case the function does not throw:
24761cb0ef41Sopenharmony_ciassert.throws(notThrowing, 'Second');
24771cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Missing expected exception: Second
24781cb0ef41Sopenharmony_ci
24791cb0ef41Sopenharmony_ci// If it was intended to match for the error message do this instead:
24801cb0ef41Sopenharmony_ci// It does not throw because the error messages match.
24811cb0ef41Sopenharmony_ciassert.throws(throwingSecond, /Second$/);
24821cb0ef41Sopenharmony_ci
24831cb0ef41Sopenharmony_ci// If the error message does not match, an AssertionError is thrown.
24841cb0ef41Sopenharmony_ciassert.throws(throwingFirst, /Second$/);
24851cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]
24861cb0ef41Sopenharmony_ci```
24871cb0ef41Sopenharmony_ci
24881cb0ef41Sopenharmony_ci```cjs
24891cb0ef41Sopenharmony_ciconst assert = require('node:assert/strict');
24901cb0ef41Sopenharmony_ci
24911cb0ef41Sopenharmony_cifunction throwingFirst() {
24921cb0ef41Sopenharmony_ci  throw new Error('First');
24931cb0ef41Sopenharmony_ci}
24941cb0ef41Sopenharmony_ci
24951cb0ef41Sopenharmony_cifunction throwingSecond() {
24961cb0ef41Sopenharmony_ci  throw new Error('Second');
24971cb0ef41Sopenharmony_ci}
24981cb0ef41Sopenharmony_ci
24991cb0ef41Sopenharmony_cifunction notThrowing() {}
25001cb0ef41Sopenharmony_ci
25011cb0ef41Sopenharmony_ci// The second argument is a string and the input function threw an Error.
25021cb0ef41Sopenharmony_ci// The first case will not throw as it does not match for the error message
25031cb0ef41Sopenharmony_ci// thrown by the input function!
25041cb0ef41Sopenharmony_ciassert.throws(throwingFirst, 'Second');
25051cb0ef41Sopenharmony_ci// In the next example the message has no benefit over the message from the
25061cb0ef41Sopenharmony_ci// error and since it is not clear if the user intended to actually match
25071cb0ef41Sopenharmony_ci// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.
25081cb0ef41Sopenharmony_ciassert.throws(throwingSecond, 'Second');
25091cb0ef41Sopenharmony_ci// TypeError [ERR_AMBIGUOUS_ARGUMENT]
25101cb0ef41Sopenharmony_ci
25111cb0ef41Sopenharmony_ci// The string is only used (as message) in case the function does not throw:
25121cb0ef41Sopenharmony_ciassert.throws(notThrowing, 'Second');
25131cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]: Missing expected exception: Second
25141cb0ef41Sopenharmony_ci
25151cb0ef41Sopenharmony_ci// If it was intended to match for the error message do this instead:
25161cb0ef41Sopenharmony_ci// It does not throw because the error messages match.
25171cb0ef41Sopenharmony_ciassert.throws(throwingSecond, /Second$/);
25181cb0ef41Sopenharmony_ci
25191cb0ef41Sopenharmony_ci// If the error message does not match, an AssertionError is thrown.
25201cb0ef41Sopenharmony_ciassert.throws(throwingFirst, /Second$/);
25211cb0ef41Sopenharmony_ci// AssertionError [ERR_ASSERTION]
25221cb0ef41Sopenharmony_ci```
25231cb0ef41Sopenharmony_ci
25241cb0ef41Sopenharmony_ciDue to the confusing error-prone notation, avoid a string as the second
25251cb0ef41Sopenharmony_ciargument.
25261cb0ef41Sopenharmony_ci
25271cb0ef41Sopenharmony_ci[Object wrappers]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript
25281cb0ef41Sopenharmony_ci[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring
25291cb0ef41Sopenharmony_ci[`!=` operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality
25301cb0ef41Sopenharmony_ci[`===` operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality
25311cb0ef41Sopenharmony_ci[`==` operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality
25321cb0ef41Sopenharmony_ci[`AssertionError`]: #class-assertassertionerror
25331cb0ef41Sopenharmony_ci[`CallTracker`]: #class-assertcalltracker
25341cb0ef41Sopenharmony_ci[`Class`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
25351cb0ef41Sopenharmony_ci[`ERR_INVALID_RETURN_VALUE`]: errors.md#err_invalid_return_value
25361cb0ef41Sopenharmony_ci[`Error.captureStackTrace`]: errors.md#errorcapturestacktracetargetobject-constructoropt
25371cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error
25381cb0ef41Sopenharmony_ci[`Map`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
25391cb0ef41Sopenharmony_ci[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
25401cb0ef41Sopenharmony_ci[`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
25411cb0ef41Sopenharmony_ci[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
25421cb0ef41Sopenharmony_ci[`Symbol`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
25431cb0ef41Sopenharmony_ci[`TypeError`]: errors.md#class-typeerror
25441cb0ef41Sopenharmony_ci[`WeakMap`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
25451cb0ef41Sopenharmony_ci[`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
25461cb0ef41Sopenharmony_ci[`assert.deepEqual()`]: #assertdeepequalactual-expected-message
25471cb0ef41Sopenharmony_ci[`assert.deepStrictEqual()`]: #assertdeepstrictequalactual-expected-message
25481cb0ef41Sopenharmony_ci[`assert.doesNotThrow()`]: #assertdoesnotthrowfn-error-message
25491cb0ef41Sopenharmony_ci[`assert.equal()`]: #assertequalactual-expected-message
25501cb0ef41Sopenharmony_ci[`assert.notDeepEqual()`]: #assertnotdeepequalactual-expected-message
25511cb0ef41Sopenharmony_ci[`assert.notDeepStrictEqual()`]: #assertnotdeepstrictequalactual-expected-message
25521cb0ef41Sopenharmony_ci[`assert.notEqual()`]: #assertnotequalactual-expected-message
25531cb0ef41Sopenharmony_ci[`assert.notStrictEqual()`]: #assertnotstrictequalactual-expected-message
25541cb0ef41Sopenharmony_ci[`assert.ok()`]: #assertokvalue-message
25551cb0ef41Sopenharmony_ci[`assert.strictEqual()`]: #assertstrictequalactual-expected-message
25561cb0ef41Sopenharmony_ci[`assert.throws()`]: #assertthrowsfn-error-message
25571cb0ef41Sopenharmony_ci[`getColorDepth()`]: tty.md#writestreamgetcolordepthenv
25581cb0ef41Sopenharmony_ci[`process.on('exit')`]: process.md#event-exit
25591cb0ef41Sopenharmony_ci[`tracker.calls()`]: #trackercallsfn-exact
25601cb0ef41Sopenharmony_ci[`tracker.verify()`]: #trackerverify
25611cb0ef41Sopenharmony_ci[enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
25621cb0ef41Sopenharmony_ci[prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
2563