The node:assert module provides a set of assertion functions for verifying\ninvariants.
In strict assertion mode, non-strict methods behave like their corresponding\nstrict methods. For example, assert.deepEqual() will behave like\nassert.deepStrictEqual().
In strict assertion mode, error messages for objects display a diff. In legacy\nassertion mode, error messages for objects display the objects, often truncated.
\nTo use strict assertion mode:
\nimport { strict as assert } from 'node:assert';\n\nconst assert = require('node:assert').strict;\n\nimport assert from 'node:assert/strict';\n\nconst assert = require('node:assert/strict');\n\nExample error diff:
\nimport { strict as assert } from 'node:assert';\n\nassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected ... Lines skipped\n//\n// [\n// [\n// ...\n// 2,\n// + 3\n// - '3'\n// ],\n// ...\n// 5\n// ]\n\nconst assert = require('node:assert/strict');\n\nassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected ... Lines skipped\n//\n// [\n// [\n// ...\n// 2,\n// + 3\n// - '3'\n// ],\n// ...\n// 5\n// ]\n\nTo deactivate the colors, use the NO_COLOR or NODE_DISABLE_COLORS\nenvironment variables. This will also deactivate the colors in the REPL. For\nmore on color support in terminal environments, read the tty\ngetColorDepth() documentation.
Legacy assertion mode uses the == operator in:
To use legacy assertion mode:
\nimport assert from 'node:assert';\n\nconst assert = require('node:assert');\n\nLegacy assertion mode may have surprising results, especially when using\nassert.deepEqual():
// WARNING: This does not throw an AssertionError in legacy assertion mode!\nassert.deepEqual(/a/gi, new Date());\n",
"displayName": "Legacy assertion mode"
}
],
"classes": [
{
"textRaw": "Class: `assert.AssertionError`",
"name": "assert.AssertionError",
"type": "class",
"desc": "<errors.Error>Indicates the failure of an assertion. All errors thrown by the node:assert\nmodule will be instances of the AssertionError class.
A subclass of <Error> that indicates the failure of an assertion.
All instances contain the built-in Error properties (message and name)\nand:
actual <any> Set to the actual argument for methods such as\nassert.strictEqual().expected <any> Set to the expected value for methods such as\nassert.strictEqual().generatedMessage <boolean> Indicates if the message was auto-generated\n(true) or not.code <string> Value is always ERR_ASSERTION to show that the error is an\nassertion error.operator <string> Set to the passed in operator value.import assert from 'node:assert';\n\n// Generate an AssertionError to compare the error message later:\nconst { message } = new assert.AssertionError({\n actual: 1,\n expected: 2,\n operator: 'strictEqual',\n});\n\n// Verify error output:\ntry {\n assert.strictEqual(1, 2);\n} catch (err) {\n assert(err instanceof assert.AssertionError);\n assert.strictEqual(err.message, message);\n assert.strictEqual(err.name, 'AssertionError');\n assert.strictEqual(err.actual, 1);\n assert.strictEqual(err.expected, 2);\n assert.strictEqual(err.code, 'ERR_ASSERTION');\n assert.strictEqual(err.operator, 'strictEqual');\n assert.strictEqual(err.generatedMessage, true);\n}\n\nconst assert = require('node:assert');\n\n// Generate an AssertionError to compare the error message later:\nconst { message } = new assert.AssertionError({\n actual: 1,\n expected: 2,\n operator: 'strictEqual',\n});\n\n// Verify error output:\ntry {\n assert.strictEqual(1, 2);\n} catch (err) {\n assert(err instanceof assert.AssertionError);\n assert.strictEqual(err.message, message);\n assert.strictEqual(err.name, 'AssertionError');\n assert.strictEqual(err.actual, 1);\n assert.strictEqual(err.expected, 2);\n assert.strictEqual(err.code, 'ERR_ASSERTION');\n assert.strictEqual(err.operator, 'strictEqual');\n assert.strictEqual(err.generatedMessage, true);\n}\n"
}
]
},
{
"textRaw": "Class: `assert.Assert`",
"name": "assert.Assert",
"type": "class",
"meta": {
"added": [
"v24.6.0",
"v22.19.0"
],
"changes": []
},
"desc": "The Assert class allows creating independent assertion instances with custom options.
Creates a new assertion instance. The diff option controls the verbosity of diffs in assertion error messages.
const { Assert } = require('node:assert');\nconst assertInstance = new Assert({ diff: 'full' });\nassertInstance.deepStrictEqual({ a: 1 }, { a: 2 });\n// Shows a full diff in the error message.\n\nImportant: When destructuring assertion methods from an Assert instance,\nthe methods lose their connection to the instance's configuration options (such\nas diff, strict, and skipPrototype settings).\nThe destructured methods will fall back to default behavior instead.
const myAssert = new Assert({ diff: 'full' });\n\n// This works as expected - uses 'full' diff\nmyAssert.strictEqual({ a: 1 }, { b: { c: 1 } });\n\n// This loses the 'full' diff setting - falls back to default 'simple' diff\nconst { strictEqual } = myAssert;\nstrictEqual({ a: 1 }, { b: { c: 1 } });\n\nThe skipPrototype option affects all deep equality methods:
class Foo {\n constructor(a) {\n this.a = a;\n }\n}\n\nclass Bar {\n constructor(a) {\n this.a = a;\n }\n}\n\nconst foo = new Foo(1);\nconst bar = new Bar(1);\n\n// Default behavior - fails due to different constructors\nconst assert1 = new Assert();\nassert1.deepStrictEqual(foo, bar); // AssertionError\n\n// Skip prototype comparison - passes if properties are equal\nconst assert2 = new Assert({ skipPrototype: true });\nassert2.deepStrictEqual(foo, bar); // OK\n\nWhen destructured, methods lose access to the instance's this context and revert to default assertion behavior\n(diff: 'simple', non-strict mode).\nTo maintain custom options when using destructured methods, avoid\ndestructuring and call methods directly on the instance.
An alias of assert.ok().
Strict assertion mode
\nAn alias of assert.deepStrictEqual().
Legacy assertion mode
\nTests for deep equality between the actual and expected parameters. Consider\nusing assert.deepStrictEqual() instead. assert.deepEqual() can have\nsurprising results.
Deep equality means that the enumerable \"own\" properties of child objects\nare also recursively evaluated by the following rules.
", "modules": [ { "textRaw": "Comparison details", "name": "comparison_details", "type": "module", "desc": "== operator,\nwith the exception of <NaN>. It is treated as being identical in case\nboth sides are <NaN>.<Error> names, messages, causes, and errors are always compared,\neven if these are not enumerable properties.Object properties are compared unordered.<Map> keys and <Set> items are compared unordered.[[Prototype]] of\nobjects.<Symbol> properties are not compared.<WeakMap>, <WeakSet> and <Promise> instances are not compared\nstructurally. They are only equal if they reference the same object. Any\ncomparison between different WeakMap, WeakSet, or Promise instances\nwill result in inequality, even if they contain the same content.<RegExp> lastIndex, flags, and source are always compared, even if these\nare not enumerable properties.The following example does not throw an AssertionError because the\nprimitives are compared using the == operator.
import assert from 'node:assert';\n// WARNING: This does not throw an AssertionError!\n\nassert.deepEqual('+00000000', false);\n\nconst assert = require('node:assert');\n// WARNING: This does not throw an AssertionError!\n\nassert.deepEqual('+00000000', false);\n\n\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare evaluated also:
\nimport assert from 'node:assert';\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = { __proto__: obj1 };\n\nassert.deepEqual(obj1, obj1);\n// OK\n\n// Values of b are different:\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n\nassert.deepEqual(obj1, obj3);\n// OK\n\n// Prototypes are ignored:\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n\nconst assert = require('node:assert');\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = { __proto__: obj1 };\n\nassert.deepEqual(obj1, obj1);\n// OK\n\n// Values of b are different:\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n\nassert.deepEqual(obj1, obj3);\n// OK\n\n// Prototypes are ignored:\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n\nIf the values are not equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned. If the message parameter is an instance of <Error> then it will be thrown instead of the AssertionError.
Tests for deep equality between the actual and expected parameters.\n\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare recursively evaluated also by the following rules.
Object.is().[[Prototype]] of objects are compared using\nthe === operator.<Error> names, messages, causes, and errors are always compared,\neven if these are not enumerable properties. errors is also compared.<Symbol> properties are compared as well.Object properties are compared unordered.<Map> keys and <Set> items are compared unordered.<WeakMap>, <WeakSet> and <Promise> instances are not compared\nstructurally. They are only equal if they reference the same object. Any\ncomparison between different WeakMap, WeakSet, or Promise instances\nwill result in inequality, even if they contain the same content.<RegExp> lastIndex, flags, and source are always compared, even if these\nare not enumerable properties.import assert from 'node:assert/strict';\n\n// This fails because 1 !== '1'.\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// {\n// + a: 1\n// - a: '1'\n// }\n\n// The following objects don't have own properties\nconst date = new Date();\nconst object = {};\nconst fakeDate = {};\nObject.setPrototypeOf(fakeDate, Date.prototype);\n\n// Different [[Prototype]]:\nassert.deepStrictEqual(object, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + {}\n// - Date {}\n\n// Different type tags:\nassert.deepStrictEqual(date, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 2018-04-26T00:49:08.604Z\n// - Date {}\n\nassert.deepStrictEqual(NaN, NaN);\n// OK because Object.is(NaN, NaN) is true.\n\n// Different unwrapped numbers:\nassert.deepStrictEqual(new Number(1), new Number(2));\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + [Number: 1]\n// - [Number: 2]\n\nassert.deepStrictEqual(new String('foo'), Object('foo'));\n// OK because the object and the string are identical when unwrapped.\n\nassert.deepStrictEqual(-0, -0);\n// OK\n\n// Different zeros:\nassert.deepStrictEqual(0, -0);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 0\n// - -0\n\nconst symbol1 = Symbol();\nconst symbol2 = Symbol();\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });\n// OK, because it is the same symbol on both objects.\n\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });\n// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:\n//\n// {\n// Symbol(): 1\n// }\n\nconst weakMap1 = new WeakMap();\nconst weakMap2 = new WeakMap();\nconst obj = {};\n\nweakMap1.set(obj, 'value');\nweakMap2.set(obj, 'value');\n\n// Comparing different instances fails, even with same contents\nassert.deepStrictEqual(weakMap1, weakMap2);\n// AssertionError: Values have same structure but are not reference-equal:\n//\n// WeakMap {\n// <items unknown>\n// }\n\n// Comparing the same instance to itself succeeds\nassert.deepStrictEqual(weakMap1, weakMap1);\n// OK\n\nconst weakSet1 = new WeakSet();\nconst weakSet2 = new WeakSet();\nweakSet1.add(obj);\nweakSet2.add(obj);\n\n// Comparing different instances fails, even with same contents\nassert.deepStrictEqual(weakSet1, weakSet2);\n// AssertionError: Values have same structure but are not reference-equal:\n// + actual - expected\n//\n// WeakSet {\n// <items unknown>\n// }\n\n// Comparing the same instance to itself succeeds\nassert.deepStrictEqual(weakSet1, weakSet1);\n// OK\n\nconst assert = require('node:assert/strict');\n\n// This fails because 1 !== '1'.\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// {\n// + a: 1\n// - a: '1'\n// }\n\n// The following objects don't have own properties\nconst date = new Date();\nconst object = {};\nconst fakeDate = {};\nObject.setPrototypeOf(fakeDate, Date.prototype);\n\n// Different [[Prototype]]:\nassert.deepStrictEqual(object, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + {}\n// - Date {}\n\n// Different type tags:\nassert.deepStrictEqual(date, fakeDate);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 2018-04-26T00:49:08.604Z\n// - Date {}\n\nassert.deepStrictEqual(NaN, NaN);\n// OK because Object.is(NaN, NaN) is true.\n\n// Different unwrapped numbers:\nassert.deepStrictEqual(new Number(1), new Number(2));\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + [Number: 1]\n// - [Number: 2]\n\nassert.deepStrictEqual(new String('foo'), Object('foo'));\n// OK because the object and the string are identical when unwrapped.\n\nassert.deepStrictEqual(-0, -0);\n// OK\n\n// Different zeros:\nassert.deepStrictEqual(0, -0);\n// AssertionError: Expected inputs to be strictly deep-equal:\n// + actual - expected\n//\n// + 0\n// - -0\n\nconst symbol1 = Symbol();\nconst symbol2 = Symbol();\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });\n// OK, because it is the same symbol on both objects.\n\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });\n// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:\n//\n// {\n// Symbol(): 1\n// }\n\nconst weakMap1 = new WeakMap();\nconst weakMap2 = new WeakMap();\nconst obj = {};\n\nweakMap1.set(obj, 'value');\nweakMap2.set(obj, 'value');\n\n// Comparing different instances fails, even with same contents\nassert.deepStrictEqual(weakMap1, weakMap2);\n// AssertionError: Values have same structure but are not reference-equal:\n//\n// WeakMap {\n// <items unknown>\n// }\n\n// Comparing the same instance to itself succeeds\nassert.deepStrictEqual(weakMap1, weakMap1);\n// OK\n\nconst weakSet1 = new WeakSet();\nconst weakSet2 = new WeakSet();\nweakSet1.add(obj);\nweakSet2.add(obj);\n\n// Comparing different instances fails, even with same contents\nassert.deepStrictEqual(weakSet1, weakSet2);\n// AssertionError: Values have same structure but are not reference-equal:\n// + actual - expected\n//\n// WeakSet {\n// <items unknown>\n// }\n\n// Comparing the same instance to itself succeeds\nassert.deepStrictEqual(weakSet1, weakSet1);\n// OK\n\nIf the values are not equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned. If the message parameter is an instance of <Error> then it will be thrown instead of the AssertionError.
Expects the string input not to match the regular expression.
import assert from 'node:assert/strict';\n\nassert.doesNotMatch('I will fail', /fail/);\n// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...\n\nassert.doesNotMatch(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.doesNotMatch('I will pass', /different/);\n// OK\n\nconst assert = require('node:assert/strict');\n\nassert.doesNotMatch('I will fail', /fail/);\n// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...\n\nassert.doesNotMatch(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.doesNotMatch('I will pass', /different/);\n// OK\n\nIf the values do match, or if the string argument is of another type than\nstring, an AssertionError is thrown with a message property set equal\nto the value of the message parameter. If the message parameter is\nundefined, a default error message is assigned. If the message parameter is an\ninstance of <Error> then it will be thrown instead of the AssertionError.
Awaits the asyncFn promise or, if asyncFn is a function, immediately\ncalls the function and awaits the returned promise to complete. It will then\ncheck that the promise is not rejected.
If asyncFn is a function and it throws an error synchronously,\nassert.doesNotReject() will return a rejected Promise with that error. If\nthe function does not return a promise, assert.doesNotReject() will return a\nrejected Promise with an ERR_INVALID_RETURN_VALUE error. In both cases\nthe error handler is skipped.
Using assert.doesNotReject() is actually not useful because there is little\nbenefit in catching a rejection and then rejecting it again. Instead, consider\nadding a comment next to the specific code path that should not reject and keep\nerror messages as expressive as possible.
If specified, error can be a Class, <RegExp> or a validation\nfunction. See assert.throws() for more details.
Besides the async nature to await the completion behaves identically to\nassert.doesNotThrow().
import assert from 'node:assert/strict';\n\nawait assert.doesNotReject(\n async () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n);\n\nconst assert = require('node:assert/strict');\n\n(async () => {\n await assert.doesNotReject(\n async () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n );\n})();\n\nimport assert from 'node:assert/strict';\n\nassert.doesNotReject(Promise.reject(new TypeError('Wrong value')))\n .then(() => {\n // ...\n });\n\nconst assert = require('node:assert/strict');\n\nassert.doesNotReject(Promise.reject(new TypeError('Wrong value')))\n .then(() => {\n // ...\n });\n"
},
{
"textRaw": "`assert.doesNotThrow(fn[, error][, message])`",
"name": "doesNotThrow",
"type": "method",
"meta": {
"added": [
"v0.1.21"
],
"changes": [
{
"version": [
"v5.11.0",
"v4.4.5"
],
"pr-url": "https://github.com/nodejs/node/pull/2407",
"description": "The `message` parameter is respected now."
},
{
"version": "v4.2.0",
"pr-url": "https://github.com/nodejs/node/pull/3276",
"description": "The `error` parameter can now be an arrow function."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`fn` {Function}",
"name": "fn",
"type": "Function"
},
{
"textRaw": "`error` {RegExp|Function}",
"name": "error",
"type": "RegExp|Function",
"optional": true
},
{
"textRaw": "`message` {string}",
"name": "message",
"type": "string",
"optional": true
}
]
}
],
"desc": "Asserts that the function fn does not throw an error.
Using assert.doesNotThrow() is actually not useful because there\nis no benefit in catching an error and then rethrowing it. Instead, consider\nadding a comment next to the specific code path that should not throw and keep\nerror messages as expressive as possible.
When assert.doesNotThrow() is called, it will immediately call the fn\nfunction.
If an error is thrown and it is the same type as that specified by the error\nparameter, then an AssertionError is thrown. If the error is of a\ndifferent type, or if the error parameter is undefined, the error is\npropagated back to the caller.
If specified, error can be a Class, <RegExp>, or a validation\nfunction. See assert.throws() for more details.
The following, for instance, will throw the <TypeError> because there is no\nmatching error type in the assertion:
import assert from 'node:assert/strict';\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n);\n\nconst assert = require('node:assert/strict');\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError,\n);\n\nHowever, the following will result in an AssertionError with the message\n'Got unwanted exception...':
import assert from 'node:assert/strict';\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n TypeError,\n);\n\nconst assert = require('node:assert/strict');\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n TypeError,\n);\n\nIf an AssertionError is thrown and a value is provided for the message\nparameter, the value of message will be appended to the AssertionError\nmessage:
import assert from 'node:assert/strict';\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n /Wrong value/,\n 'Whoops',\n);\n// Throws: AssertionError: Got unwanted exception: Whoops\n\nconst assert = require('node:assert/strict');\n\nassert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n /Wrong value/,\n 'Whoops',\n);\n// Throws: AssertionError: Got unwanted exception: Whoops\n"
},
{
"textRaw": "`assert.equal(actual, expected[, message])`",
"name": "equal",
"type": "method",
"meta": {
"added": [
"v0.1.21"
],
"changes": [
{
"version": [
"v16.0.0",
"v14.18.0"
],
"pr-url": "https://github.com/nodejs/node/pull/38113",
"description": "In Legacy assertion mode, changed status from Deprecated to Legacy."
},
{
"version": "v14.0.0",
"pr-url": "https://github.com/nodejs/node/pull/30766",
"description": "NaN is now treated as being identical if both sides are NaN."
}
]
},
"stability": 3,
"stabilityText": "Legacy: Use `assert.strictEqual()` instead.",
"signatures": [
{
"params": [
{
"textRaw": "`actual` {any}",
"name": "actual",
"type": "any"
},
{
"textRaw": "`expected` {any}",
"name": "expected",
"type": "any"
},
{
"textRaw": "`message` {string|Error}",
"name": "message",
"type": "string|Error",
"optional": true
}
]
}
],
"desc": "Strict assertion mode
\nAn alias of assert.strictEqual().
Legacy assertion mode
\nTests shallow, coercive equality between the actual and expected parameters\nusing the == operator. NaN is specially handled\nand treated as being identical if both sides are NaN.
import assert from 'node:assert';\n\nassert.equal(1, 1);\n// OK, 1 == 1\nassert.equal(1, '1');\n// OK, 1 == '1'\nassert.equal(NaN, NaN);\n// OK\n\nassert.equal(1, 2);\n// AssertionError: 1 == 2\nassert.equal({ a: { b: 1 } }, { a: { b: 1 } });\n// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }\n\nconst assert = require('node:assert');\n\nassert.equal(1, 1);\n// OK, 1 == 1\nassert.equal(1, '1');\n// OK, 1 == '1'\nassert.equal(NaN, NaN);\n// OK\n\nassert.equal(1, 2);\n// AssertionError: 1 == 2\nassert.equal({ a: { b: 1 } }, { a: { b: 1 } });\n// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }\n\nIf the values are not equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned. If the message parameter is an instance of <Error> then it will be thrown instead of the AssertionError.
Throws an AssertionError with the provided error message or a default\nerror message. If the message parameter is an instance of <Error> then\nit will be thrown instead of the AssertionError.
import assert from 'node:assert/strict';\n\nassert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail(new TypeError('need array'));\n// TypeError: need array\n\nconst assert = require('node:assert/strict');\n\nassert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail(new TypeError('need array'));\n// TypeError: need array\n"
},
{
"textRaw": "`assert.ifError(value)`",
"name": "ifError",
"type": "method",
"meta": {
"added": [
"v0.1.97"
],
"changes": [
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/18247",
"description": "Instead of throwing the original error it is now wrapped into an [`AssertionError`][] that contains the full stack trace."
},
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/18247",
"description": "Value may now only be `undefined` or `null`. Before all falsy values were handled the same as `null` and did not throw."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Throws value if value is not undefined or null. This is useful when\ntesting the error argument in callbacks. The stack trace contains all frames\nfrom the error passed to ifError() including the potential new frames for\nifError() itself.
import assert from 'node:assert/strict';\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0\nassert.ifError('error');\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'\nassert.ifError(new Error());\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error\n\n// Create some random error frames.\nlet err;\n(function errorFrame() {\n err = new Error('test error');\n})();\n\n(function ifErrorFrame() {\n assert.ifError(err);\n})();\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error\n// at ifErrorFrame\n// at errorFrame\n\nconst assert = require('node:assert/strict');\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0\nassert.ifError('error');\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'\nassert.ifError(new Error());\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error\n\n// Create some random error frames.\nlet err;\n(function errorFrame() {\n err = new Error('test error');\n})();\n\n(function ifErrorFrame() {\n assert.ifError(err);\n})();\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error\n// at ifErrorFrame\n// at errorFrame\n"
},
{
"textRaw": "`assert.match(string, regexp[, message])`",
"name": "match",
"type": "method",
"meta": {
"added": [
"v13.6.0",
"v12.16.0"
],
"changes": [
{
"version": "v16.0.0",
"pr-url": "https://github.com/nodejs/node/pull/38111",
"description": "This API is no longer experimental."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`string` {string}",
"name": "string",
"type": "string"
},
{
"textRaw": "`regexp` {RegExp}",
"name": "regexp",
"type": "RegExp"
},
{
"textRaw": "`message` {string|Error}",
"name": "message",
"type": "string|Error",
"optional": true
}
]
}
],
"desc": "Expects the string input to match the regular expression.
import assert from 'node:assert/strict';\n\nassert.match('I will fail', /pass/);\n// AssertionError [ERR_ASSERTION]: The input did not match the regular ...\n\nassert.match(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.match('I will pass', /pass/);\n// OK\n\nconst assert = require('node:assert/strict');\n\nassert.match('I will fail', /pass/);\n// AssertionError [ERR_ASSERTION]: The input did not match the regular ...\n\nassert.match(123, /pass/);\n// AssertionError [ERR_ASSERTION]: The \"string\" argument must be of type string.\n\nassert.match('I will pass', /pass/);\n// OK\n\nIf the values do not match, or if the string argument is of another type than\nstring, an AssertionError is thrown with a message property set equal\nto the value of the message parameter. If the message parameter is\nundefined, a default error message is assigned. If the message parameter is an\ninstance of <Error> then it will be thrown instead of the AssertionError.
Strict assertion mode
\nAn alias of assert.notDeepStrictEqual().
Legacy assertion mode
\nTests for any deep inequality. Opposite of assert.deepEqual().
import assert from 'node:assert';\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = { __proto__: obj1 };\n\nassert.notDeepEqual(obj1, obj1);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj2);\n// OK\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK\n\nconst assert = require('node:assert');\n\nconst obj1 = {\n a: {\n b: 1,\n },\n};\nconst obj2 = {\n a: {\n b: 2,\n },\n};\nconst obj3 = {\n a: {\n b: 1,\n },\n};\nconst obj4 = { __proto__: obj1 };\n\nassert.notDeepEqual(obj1, obj1);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj2);\n// OK\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK\n\nIf the values are deeply equal, an AssertionError is thrown with a\nmessage property set equal to the value of the message parameter. If the\nmessage parameter is undefined, a default error message is assigned. If the\nmessage parameter is an instance of <Error> then it will be thrown\ninstead of the AssertionError.
Tests for deep strict inequality. Opposite of assert.deepStrictEqual().
import assert from 'node:assert/strict';\n\nassert.notDeepStrictEqual({ a: 1 }, { a: '1' });\n// OK\n\nconst assert = require('node:assert/strict');\n\nassert.notDeepStrictEqual({ a: 1 }, { a: '1' });\n// OK\n\nIf the values are deeply and strictly equal, an AssertionError is thrown\nwith a message property set equal to the value of the message parameter. If\nthe message parameter is undefined, a default error message is assigned. If\nthe message parameter is an instance of <Error> then it will be thrown\ninstead of the AssertionError.
Strict assertion mode
\nAn alias of assert.notStrictEqual().
Legacy assertion mode
\nTests shallow, coercive inequality with the != operator. NaN is\nspecially handled and treated as being identical if both sides are NaN.
import assert from 'node:assert';\n\nassert.notEqual(1, 2);\n// OK\n\nassert.notEqual(1, 1);\n// AssertionError: 1 != 1\n\nassert.notEqual(1, '1');\n// AssertionError: 1 != '1'\n\nconst assert = require('node:assert');\n\nassert.notEqual(1, 2);\n// OK\n\nassert.notEqual(1, 1);\n// AssertionError: 1 != 1\n\nassert.notEqual(1, '1');\n// AssertionError: 1 != '1'\n\nIf the values are equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned. If the message parameter is an instance of <Error> then it will be thrown instead of the AssertionError.
Tests strict inequality between the actual and expected parameters as\ndetermined by Object.is().
import assert from 'node:assert/strict';\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError [ERR_ASSERTION]: Expected \"actual\" to be strictly unequal to:\n//\n// 1\n\nassert.notStrictEqual(1, '1');\n// OK\n\nconst assert = require('node:assert/strict');\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError [ERR_ASSERTION]: Expected \"actual\" to be strictly unequal to:\n//\n// 1\n\nassert.notStrictEqual(1, '1');\n// OK\n\nIf the values are strictly equal, an AssertionError is thrown with a\nmessage property set equal to the value of the message parameter. If the\nmessage parameter is undefined, a default error message is assigned. If the\nmessage parameter is an instance of <Error> then it will be thrown\ninstead of the AssertionError.
Tests if value is truthy. It is equivalent to\nassert.equal(!!value, true, message).
If value is not truthy, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned. If the message parameter is an instance of <Error> then it will be thrown instead of the AssertionError.\nIf no arguments are passed in at all message will be set to the string:\n'No value argument passed to `assert.ok()`'.
Be aware that in the repl the error message will be different to the one\nthrown in a file! See below for further details.
import assert from 'node:assert/strict';\n\nassert.ok(true);\n// OK\nassert.ok(1);\n// OK\n\nassert.ok();\n// AssertionError: No value argument passed to `assert.ok()`\n\nassert.ok(false, 'it\\'s false');\n// AssertionError: it's false\n\n// In the repl:\nassert.ok(typeof 123 === 'string');\n// AssertionError: false == true\n\n// In a file (e.g. test.js):\nassert.ok(typeof 123 === 'string');\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(typeof 123 === 'string')\n\nassert.ok(false);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(false)\n\nassert.ok(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(0)\n\nconst assert = require('node:assert/strict');\n\nassert.ok(true);\n// OK\nassert.ok(1);\n// OK\n\nassert.ok();\n// AssertionError: No value argument passed to `assert.ok()`\n\nassert.ok(false, 'it\\'s false');\n// AssertionError: it's false\n\n// In the repl:\nassert.ok(typeof 123 === 'string');\n// AssertionError: false == true\n\n// In a file (e.g. test.js):\nassert.ok(typeof 123 === 'string');\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(typeof 123 === 'string')\n\nassert.ok(false);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(false)\n\nassert.ok(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert.ok(0)\n\nimport assert from 'node:assert/strict';\n\n// Using `assert()` works the same:\nassert(2 + 2 > 5);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert(2 + 2 > 5)\n\nconst assert = require('node:assert');\n\n// Using `assert()` works the same:\nassert(2 + 2 > 5);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert(2 + 2 > 5)\n"
},
{
"textRaw": "`assert.rejects(asyncFn[, error][, message])`",
"name": "rejects",
"type": "method",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`asyncFn` {Function|Promise}",
"name": "asyncFn",
"type": "Function|Promise"
},
{
"textRaw": "`error` {RegExp|Function|Object|Error}",
"name": "error",
"type": "RegExp|Function|Object|Error",
"optional": true
},
{
"textRaw": "`message` {string}",
"name": "message",
"type": "string",
"optional": true
}
],
"return": {
"textRaw": "Returns: {Promise}",
"name": "return",
"type": "Promise"
}
}
],
"desc": "Awaits the asyncFn promise or, if asyncFn is a function, immediately\ncalls the function and awaits the returned promise to complete. It will then\ncheck that the promise is rejected.
If asyncFn is a function and it throws an error synchronously,\nassert.rejects() will return a rejected Promise with that error. If the\nfunction does not return a promise, assert.rejects() will return a rejected\nPromise with an ERR_INVALID_RETURN_VALUE error. In both cases the error\nhandler is skipped.
Besides the async nature to await the completion behaves identically to\nassert.throws().
If specified, error can be a Class, <RegExp>, a validation function,\nan object where each property will be tested for, or an instance of error where\neach property will be tested for including the non-enumerable message and\nname properties.
If specified, message will be the message provided by the AssertionError\nif the asyncFn fails to reject.
import assert from 'node:assert/strict';\n\nawait assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n },\n);\n\nconst assert = require('node:assert/strict');\n\n(async () => {\n await assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n },\n );\n})();\n\nimport assert from 'node:assert/strict';\n\nawait assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n (err) => {\n assert.strictEqual(err.name, 'TypeError');\n assert.strictEqual(err.message, 'Wrong value');\n return true;\n },\n);\n\nconst assert = require('node:assert/strict');\n\n(async () => {\n await assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n (err) => {\n assert.strictEqual(err.name, 'TypeError');\n assert.strictEqual(err.message, 'Wrong value');\n return true;\n },\n );\n})();\n\nimport assert from 'node:assert/strict';\n\nassert.rejects(\n Promise.reject(new Error('Wrong value')),\n Error,\n).then(() => {\n // ...\n});\n\nconst assert = require('node:assert/strict');\n\nassert.rejects(\n Promise.reject(new Error('Wrong value')),\n Error,\n).then(() => {\n // ...\n});\n\nerror cannot be a string. If a string is provided as the second\nargument, then error is assumed to be omitted and the string will be used for\nmessage instead. This can lead to easy-to-miss mistakes. Please read the\nexample in assert.throws() carefully if using a string as the second\nargument gets considered.
Tests strict equality between the actual and expected parameters as\ndetermined by Object.is().
import assert from 'node:assert/strict';\n\nassert.strictEqual(1, 2);\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n//\n// 1 !== 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual('Hello foobar', 'Hello World!');\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n// + actual - expected\n//\n// + 'Hello foobar'\n// - 'Hello World!'\n// ^\n\nconst apples = 1;\nconst oranges = 2;\nassert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);\n// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2\n\nassert.strictEqual(1, '1', new TypeError('Inputs are not identical'));\n// TypeError: Inputs are not identical\n\nconst assert = require('node:assert/strict');\n\nassert.strictEqual(1, 2);\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n//\n// 1 !== 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual('Hello foobar', 'Hello World!');\n// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:\n// + actual - expected\n//\n// + 'Hello foobar'\n// - 'Hello World!'\n// ^\n\nconst apples = 1;\nconst oranges = 2;\nassert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);\n// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2\n\nassert.strictEqual(1, '1', new TypeError('Inputs are not identical'));\n// TypeError: Inputs are not identical\n\nIf the values are not strictly equal, an AssertionError is thrown with a\nmessage property set equal to the value of the message parameter. If the\nmessage parameter is undefined, a default error message is assigned. If the\nmessage parameter is an instance of <Error> then it will be thrown\ninstead of the AssertionError.
Expects the function fn to throw an error.
If specified, error can be a Class, <RegExp>, a validation function,\na validation object where each property will be tested for strict deep equality,\nor an instance of error where each property will be tested for strict deep\nequality including the non-enumerable message and name properties. When\nusing an object, it is also possible to use a regular expression, when\nvalidating against a string property. See below for examples.
If specified, message will be appended to the message provided by the\nAssertionError if the fn call fails to throw or in case the error validation\nfails.
Custom validation object/error instance:
\nimport assert from 'node:assert/strict';\n\nconst err = new TypeError('Wrong value');\nerr.code = 404;\nerr.foo = 'bar';\nerr.info = {\n nested: true,\n baz: 'text',\n};\nerr.reg = /abc/i;\n\nassert.throws(\n () => {\n throw err;\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n info: {\n nested: true,\n baz: 'text',\n },\n // Only properties on the validation object will be tested for.\n // Using nested objects requires all properties to be present. Otherwise\n // the validation is going to fail.\n },\n);\n\n// Using regular expressions to validate error properties:\nassert.throws(\n () => {\n throw err;\n },\n {\n // The `name` and `message` properties are strings and using regular\n // expressions on those will match against the string. If they fail, an\n // error is thrown.\n name: /^TypeError$/,\n message: /Wrong/,\n foo: 'bar',\n info: {\n nested: true,\n // It is not possible to use regular expressions for nested properties!\n baz: 'text',\n },\n // The `reg` property contains a regular expression and only if the\n // validation object contains an identical regular expression, it is going\n // to pass.\n reg: /abc/i,\n },\n);\n\n// Fails due to the different `message` and `name` properties:\nassert.throws(\n () => {\n const otherErr = new Error('Not found');\n // Copy all enumerable properties from `err` to `otherErr`.\n for (const [key, value] of Object.entries(err)) {\n otherErr[key] = value;\n }\n throw otherErr;\n },\n // The error's `message` and `name` properties will also be checked when using\n // an error as validation object.\n err,\n);\n\nconst assert = require('node:assert/strict');\n\nconst err = new TypeError('Wrong value');\nerr.code = 404;\nerr.foo = 'bar';\nerr.info = {\n nested: true,\n baz: 'text',\n};\nerr.reg = /abc/i;\n\nassert.throws(\n () => {\n throw err;\n },\n {\n name: 'TypeError',\n message: 'Wrong value',\n info: {\n nested: true,\n baz: 'text',\n },\n // Only properties on the validation object will be tested for.\n // Using nested objects requires all properties to be present. Otherwise\n // the validation is going to fail.\n },\n);\n\n// Using regular expressions to validate error properties:\nassert.throws(\n () => {\n throw err;\n },\n {\n // The `name` and `message` properties are strings and using regular\n // expressions on those will match against the string. If they fail, an\n // error is thrown.\n name: /^TypeError$/,\n message: /Wrong/,\n foo: 'bar',\n info: {\n nested: true,\n // It is not possible to use regular expressions for nested properties!\n baz: 'text',\n },\n // The `reg` property contains a regular expression and only if the\n // validation object contains an identical regular expression, it is going\n // to pass.\n reg: /abc/i,\n },\n);\n\n// Fails due to the different `message` and `name` properties:\nassert.throws(\n () => {\n const otherErr = new Error('Not found');\n // Copy all enumerable properties from `err` to `otherErr`.\n for (const [key, value] of Object.entries(err)) {\n otherErr[key] = value;\n }\n throw otherErr;\n },\n // The error's `message` and `name` properties will also be checked when using\n // an error as validation object.\n err,\n);\n\nValidate instanceof using constructor:
\nimport assert from 'node:assert/strict';\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n Error,\n);\n\nconst assert = require('node:assert/strict');\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n Error,\n);\n\nValidate error message using <RegExp>:
Using a regular expression runs .toString on the error object, and will\ntherefore also include the error name.
import assert from 'node:assert/strict';\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n /^Error: Wrong value$/,\n);\n\nconst assert = require('node:assert/strict');\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n /^Error: Wrong value$/,\n);\n\nCustom error validation:
\nThe function must return true to indicate all internal validations passed.\nIt will otherwise fail with an AssertionError.
import assert from 'node:assert/strict';\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n (err) => {\n assert(err instanceof Error);\n assert(/value/.test(err));\n // Avoid returning anything from validation functions besides `true`.\n // Otherwise, it's not clear what part of the validation failed. Instead,\n // throw an error about the specific validation that failed (as done in this\n // example) and add as much helpful debugging information to that error as\n // possible.\n return true;\n },\n 'unexpected error',\n);\n\nconst assert = require('node:assert/strict');\n\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n (err) => {\n assert(err instanceof Error);\n assert(/value/.test(err));\n // Avoid returning anything from validation functions besides `true`.\n // Otherwise, it's not clear what part of the validation failed. Instead,\n // throw an error about the specific validation that failed (as done in this\n // example) and add as much helpful debugging information to that error as\n // possible.\n return true;\n },\n 'unexpected error',\n);\n\nerror cannot be a string. If a string is provided as the second\nargument, then error is assumed to be omitted and the string will be used for\nmessage instead. This can lead to easy-to-miss mistakes. Using the same\nmessage as the thrown error message is going to result in an\nERR_AMBIGUOUS_ARGUMENT error. Please read the example below carefully if using\na string as the second argument gets considered:
import assert from 'node:assert/strict';\n\nfunction throwingFirst() {\n throw new Error('First');\n}\n\nfunction throwingSecond() {\n throw new Error('Second');\n}\n\nfunction notThrowing() {}\n\n// The second argument is a string and the input function threw an Error.\n// The first case will not throw as it does not match for the error message\n// thrown by the input function!\nassert.throws(throwingFirst, 'Second');\n// In the next example the message has no benefit over the message from the\n// error and since it is not clear if the user intended to actually match\n// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.\nassert.throws(throwingSecond, 'Second');\n// TypeError [ERR_AMBIGUOUS_ARGUMENT]\n\n// The string is only used (as message) in case the function does not throw:\nassert.throws(notThrowing, 'Second');\n// AssertionError [ERR_ASSERTION]: Missing expected exception: Second\n\n// If it was intended to match for the error message do this instead:\n// It does not throw because the error messages match.\nassert.throws(throwingSecond, /Second$/);\n\n// If the error message does not match, an AssertionError is thrown.\nassert.throws(throwingFirst, /Second$/);\n// AssertionError [ERR_ASSERTION]\n\nconst assert = require('node:assert/strict');\n\nfunction throwingFirst() {\n throw new Error('First');\n}\n\nfunction throwingSecond() {\n throw new Error('Second');\n}\n\nfunction notThrowing() {}\n\n// The second argument is a string and the input function threw an Error.\n// The first case will not throw as it does not match for the error message\n// thrown by the input function!\nassert.throws(throwingFirst, 'Second');\n// In the next example the message has no benefit over the message from the\n// error and since it is not clear if the user intended to actually match\n// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.\nassert.throws(throwingSecond, 'Second');\n// TypeError [ERR_AMBIGUOUS_ARGUMENT]\n\n// The string is only used (as message) in case the function does not throw:\nassert.throws(notThrowing, 'Second');\n// AssertionError [ERR_ASSERTION]: Missing expected exception: Second\n\n// If it was intended to match for the error message do this instead:\n// It does not throw because the error messages match.\nassert.throws(throwingSecond, /Second$/);\n\n// If the error message does not match, an AssertionError is thrown.\nassert.throws(throwingFirst, /Second$/);\n// AssertionError [ERR_ASSERTION]\n\nDue to the confusing error-prone notation, avoid a string as the second\nargument.
" }, { "textRaw": "`assert.partialDeepStrictEqual(actual, expected[, message])`", "name": "partialDeepStrictEqual", "type": "method", "meta": { "added": [ "v23.4.0", "v22.13.0" ], "changes": [ { "version": "v25.0.0", "pr-url": "https://github.com/nodejs/node/pull/59448", "description": "Promises are not considered equal anymore if they are not of the same instance." }, { "version": "v25.0.0", "pr-url": "https://github.com/nodejs/node/pull/57627", "description": "Invalid dates are now considered equal." }, { "version": [ "v24.0.0", "v22.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/57370", "description": "partialDeepStrictEqual is now Stable. Previously, it had been Experimental." } ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any}", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any}", "name": "expected", "type": "any" }, { "textRaw": "`message` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "Tests for partial deep equality between the actual and expected parameters.\n\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare recursively evaluated also by the following rules. \"Partial\" equality means\nthat only properties that exist on the expected parameter are going to be\ncompared.
This method always passes the same test cases as assert.deepStrictEqual(),\nbehaving as a super set of it.
Object.is().[[Prototype]] of objects are not compared.<Error> names, messages, causes, and errors are always compared,\neven if these are not enumerable properties. errors is also compared.<Symbol> properties are compared as well.Object properties are compared unordered.<Map> keys and <Set> items are compared unordered.<WeakMap>, <WeakSet> and <Promise> instances are not compared\nstructurally. They are only equal if they reference the same object. Any\ncomparison between different WeakMap, WeakSet, or Promise instances\nwill result in inequality, even if they contain the same content.<RegExp> lastIndex, flags, and source are always compared, even if these\nare not enumerable properties.import assert from 'node:assert';\n\nassert.partialDeepStrictEqual(\n { a: { b: { c: 1 } } },\n { a: { b: { c: 1 } } },\n);\n// OK\n\nassert.partialDeepStrictEqual(\n { a: 1, b: 2, c: 3 },\n { b: 2 },\n);\n// OK\n\nassert.partialDeepStrictEqual(\n [1, 2, 3, 4, 5, 6, 7, 8, 9],\n [4, 5, 8],\n);\n// OK\n\nassert.partialDeepStrictEqual(\n new Set([{ a: 1 }, { b: 1 }]),\n new Set([{ a: 1 }]),\n);\n// OK\n\nassert.partialDeepStrictEqual(\n new Map([['key1', 'value1'], ['key2', 'value2']]),\n new Map([['key2', 'value2']]),\n);\n// OK\n\nassert.partialDeepStrictEqual(123n, 123n);\n// OK\n\nassert.partialDeepStrictEqual(\n [1, 2, 3, 4, 5, 6, 7, 8, 9],\n [5, 4, 8],\n);\n// AssertionError\n\nassert.partialDeepStrictEqual(\n { a: 1 },\n { a: 1, b: 2 },\n);\n// AssertionError\n\nassert.partialDeepStrictEqual(\n { a: { b: 2 } },\n { a: { b: '2' } },\n);\n// AssertionError\n\nconst assert = require('node:assert');\n\nassert.partialDeepStrictEqual(\n { a: { b: { c: 1 } } },\n { a: { b: { c: 1 } } },\n);\n// OK\n\nassert.partialDeepStrictEqual(\n { a: 1, b: 2, c: 3 },\n { b: 2 },\n);\n// OK\n\nassert.partialDeepStrictEqual(\n [1, 2, 3, 4, 5, 6, 7, 8, 9],\n [4, 5, 8],\n);\n// OK\n\nassert.partialDeepStrictEqual(\n new Set([{ a: 1 }, { b: 1 }]),\n new Set([{ a: 1 }]),\n);\n// OK\n\nassert.partialDeepStrictEqual(\n new Map([['key1', 'value1'], ['key2', 'value2']]),\n new Map([['key2', 'value2']]),\n);\n// OK\n\nassert.partialDeepStrictEqual(123n, 123n);\n// OK\n\nassert.partialDeepStrictEqual(\n [1, 2, 3, 4, 5, 6, 7, 8, 9],\n [5, 4, 8],\n);\n// AssertionError\n\nassert.partialDeepStrictEqual(\n { a: 1 },\n { a: 1, b: 2 },\n);\n// AssertionError\n\nassert.partialDeepStrictEqual(\n { a: { b: 2 } },\n { a: { b: '2' } },\n);\n// AssertionError\n",
"displayName": "Comparison details"
}
]
}
],
"displayName": "Assert"
}
]
}