{ "source": "doc/api/assert.md", "modules": [ { "textRaw": "Assert", "name": "assert", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "

The assert module provides a simple set of assertion tests that can be used to\ntest invariants.

\n

A strict and a legacy mode exist, while it is recommended to only use\nstrict mode.

\n

For more information about the used equality comparisons see\nMDN's guide on equality comparisons and sameness.

\n", "modules": [ { "textRaw": "Strict mode", "name": "strict_mode", "meta": { "added": [ "v9.9.0" ], "changes": [ { "version": "v9.9.0", "pr-url": "https://github.com/nodejs/node/pull/17615", "description": "Added error diffs to the strict mode" }, { "version": "v9.9.0", "pr-url": "https://github.com/nodejs/node/pull/17002", "description": "Added strict mode to the assert module." } ] }, "desc": "

When using the strict mode, any assert function will use the equality used\nin the strict function mode. So assert.deepEqual() will, for example,\nwork the same as assert.deepStrictEqual().

\n

On top of that, error messages which involve objects produce an error diff\ninstead of displaying both objects. That is not the case for the legacy mode.

\n

It can be accessed using:

\n
const assert = require('assert').strict;\n
\n

Example error diff (the expected, actual, and Lines skipped will be on a\nsingle row):

\n
const assert = require('assert').strict;\n\nassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);\n
\n
AssertionError [ERR_ASSERTION]: Input A expected to deepStrictEqual input B:\n+ expected\n- actual\n... Lines skipped\n\n  [\n    [\n...\n      2,\n-     3\n+     '3'\n    ],\n...\n    5\n  ]\n
\n

To deactivate the colors, use the NODE_DISABLE_COLORS environment variable.\nPlease note that this will also deactivate the colors in the REPL.

\n", "type": "module", "displayName": "Strict mode" }, { "textRaw": "Legacy mode", "name": "legacy_mode", "stability": 0, "stabilityText": "Deprecated: Use strict mode instead.", "desc": "

When accessing assert directly instead of using the strict property, the\nAbstract Equality Comparison will be used for any function without "strict"\nin its name, such as assert.deepEqual().

\n

It can be accessed using:

\n
const assert = require('assert');\n
\n

It is recommended to use the strict mode instead as the\nAbstract Equality Comparison can often have surprising results. This is\nespecially true for assert.deepEqual(), where the comparison rules are\nlax:

\n
// WARNING: This does not throw an AssertionError!\nassert.deepEqual(/a/gi, new Date());\n
\n", "type": "module", "displayName": "Legacy mode" }, { "textRaw": "Caveats", "name": "caveats", "desc": "

For the following cases, consider using ES2015 Object.is(),\nwhich uses the SameValueZero comparison.

\n
const a = 0;\nconst b = -a;\nassert.notStrictEqual(a, b);\n// AssertionError: 0 !== -0\n// Strict Equality Comparison doesn't distinguish between -0 and +0...\nassert(!Object.is(a, b));\n// but Object.is() does!\n\nconst str1 = 'foo';\nconst str2 = 'foo';\nassert.strictEqual(str1 / 1, str2 / 1);\n// AssertionError: NaN === NaN\n// Strict Equality Comparison can't be used to check NaN...\nassert(Object.is(str1 / 1, str2 / 1));\n// but Object.is() can!\n
\n

For more information, see\nMDN's guide on equality comparisons and sameness.

\n", "type": "module", "displayName": "Caveats" } ], "methods": [ { "textRaw": "assert(value[, message])", "type": "method", "name": "assert", "meta": { "added": [ "v0.5.9" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} ", "name": "value", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "value" }, { "name": "message", "optional": true } ] } ], "desc": "

An alias of assert.ok().

\n" }, { "textRaw": "assert.deepEqual(actual, expected[, message])", "type": "method", "name": "deepEqual", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15001", "description": "Error names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "Set and Map content is also compared" }, { "version": "v6.4.0, v4.7.1", "pr-url": "https://github.com/nodejs/node/pull/8002", "description": "Typed array slices are handled correctly now." }, { "version": "v6.1.0, v4.5.0", "pr-url": "https://github.com/nodejs/node/pull/6432", "description": "Objects with circular references can be used as inputs now." }, { "version": "v5.10.1, v4.4.3", "pr-url": "https://github.com/nodejs/node/pull/5910", "description": "Handle non-`Uint8Array` typed arrays correctly." } ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Strict mode

\n

An alias of assert.deepStrictEqual().

\n

Legacy mode

\n
\n

Stability: 0 - Deprecated: Use assert.deepStrictEqual() instead.

\n
\n

Tests for deep equality between the actual and expected parameters.\nPrimitive values are compared with the Abstract Equality Comparison\n( == ).

\n

Only enumerable "own" properties are considered. The\nassert.deepEqual() implementation does not test the\n[[Prototype]] of objects or enumerable own Symbol\nproperties. For such checks, consider using assert.deepStrictEqual()\ninstead. assert.deepEqual() can have potentially surprising results. The\nfollowing example does not throw an AssertionError because the properties on\nthe RegExp object are not enumerable:

\n
// WARNING: This does not throw an AssertionError!\nassert.deepEqual(/a/gi, new Date());\n
\n

An exception is made for Map and Set. Maps and Sets have their\ncontained items compared too, as expected.

\n

"Deep" equality means that the enumerable "own" properties of child objects\nare evaluated also:

\n
const assert = require('assert');\n\nconst obj1 = {\n  a: {\n    b: 1\n  }\n};\nconst obj2 = {\n  a: {\n    b: 2\n  }\n};\nconst obj3 = {\n  a: {\n    b: 1\n  }\n};\nconst obj4 = Object.create(obj1);\n\nassert.deepEqual(obj1, obj1);\n// OK, object is equal to itself\n\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n// values of b are different\n\nassert.deepEqual(obj1, obj3);\n// OK, objects are equal\n\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n// Prototypes are ignored\n
\n

If 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\nparameter is an instance of an [Error][] then it will be thrown instead of the\nAssertionError.

\n" }, { "textRaw": "assert.deepStrictEqual(actual, expected[, message])", "type": "method", "name": "deepStrictEqual", "meta": { "added": [ "v1.2.0" ], "changes": [ { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15169", "description": "Enumerable symbol properties are now compared." }, { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15036", "description": "NaN is now compared using the [SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero) comparison." }, { "version": "v8.5.0", "pr-url": "https://github.com/nodejs/node/pull/15001", "description": "Error names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "Set and Map content is also compared" }, { "version": "v6.4.0, v4.7.1", "pr-url": "https://github.com/nodejs/node/pull/8002", "description": "Typed array slices are handled correctly now." }, { "version": "v6.1.0", "pr-url": "https://github.com/nodejs/node/pull/6432", "description": "Objects with circular references can be used as inputs now." }, { "version": "v5.10.1, v4.4.3", "pr-url": "https://github.com/nodejs/node/pull/5910", "description": "Handle non-`Uint8Array` typed arrays correctly." } ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

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.

\n", "modules": [ { "textRaw": "Comparison details", "name": "comparison_details", "desc": "\n
const assert = require('assert').strict;\n\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: { a: 1 } deepStrictEqual { a: '1' }\n// because 1 !== '1' using SameValue comparison\n\n// The following objects don't have own properties\nconst date = new Date();\nconst object = {};\nconst fakeDate = {};\nObject.setPrototypeOf(fakeDate, Date.prototype);\n\nassert.deepStrictEqual(object, fakeDate);\n// AssertionError: {} deepStrictEqual Date {}\n// Different [[Prototype]]\n\nassert.deepStrictEqual(date, fakeDate);\n// AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {}\n// Different type tags\n\nassert.deepStrictEqual(NaN, NaN);\n// OK, because of the SameValue comparison\n\nassert.deepStrictEqual(new Number(1), new Number(2));\n// Fails because the wrapped number is unwrapped and compared as well.\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\nassert.deepStrictEqual(0, -0);\n// AssertionError: 0 deepStrictEqual -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.\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });\n// Fails because symbol1 !== symbol2!\n\nconst weakMap1 = new WeakMap();\nconst weakMap2 = new WeakMap([[{}, {}]]);\nconst weakMap3 = new WeakMap();\nweakMap3.unequal = true;\n\nassert.deepStrictEqual(weakMap1, weakMap2);\n// OK, because it is impossible to compare the entries\nassert.deepStrictEqual(weakMap1, weakMap3);\n// Fails because weakMap3 has a property that weakMap1 does not contain!\n
\n

If 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\nparameter is an instance of an [Error][] then it will be thrown instead of the\nAssertionError.

\n", "type": "module", "displayName": "Comparison details" } ] }, { "textRaw": "assert.doesNotThrow(block[, error][, message])", "type": "method", "name": "doesNotThrow", "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": "`block` {Function} ", "name": "block", "type": "Function" }, { "textRaw": "`error` {RegExp|Function} ", "name": "error", "type": "RegExp|Function", "optional": true }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "block" }, { "name": "error", "optional": true }, { "name": "message", "optional": true } ] } ], "desc": "

Asserts that the function block does not throw an error. See\nassert.throws() for more details.

\n

Please note: Using assert.doesNotThrow() is actually not useful because there\nis no benefit by 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.

\n

When assert.doesNotThrow() is called, it will immediately call the block\nfunction.

\n

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 different\ntype, or if the error parameter is undefined, the error is propagated back\nto the caller.

\n

The following, for instance, will throw the TypeError because there is no\nmatching error type in the assertion:

\n\n
assert.doesNotThrow(\n  () => {\n    throw new TypeError('Wrong value');\n  },\n  SyntaxError\n);\n
\n

However, the following will result in an AssertionError with the message\n'Got unwanted exception (TypeError)..':

\n\n
assert.doesNotThrow(\n  () => {\n    throw new TypeError('Wrong value');\n  },\n  TypeError\n);\n
\n

If an AssertionError is thrown and a value is provided for the message\nparameter, the value of message will be appended to the AssertionError\nmessage:

\n\n
assert.doesNotThrow(\n  () => {\n    throw new TypeError('Wrong value');\n  },\n  TypeError,\n  'Whoops'\n);\n// Throws: AssertionError: Got unwanted exception (TypeError). Whoops\n
\n" }, { "textRaw": "assert.equal(actual, expected[, message])", "type": "method", "name": "equal", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Strict mode

\n

An alias of assert.strictEqual().

\n

Legacy mode

\n
\n

Stability: 0 - Deprecated: Use assert.strictEqual() instead.

\n
\n

Tests shallow, coercive equality between the actual and expected parameters\nusing the Abstract Equality Comparison ( == ).

\n
const assert = require('assert');\n\nassert.equal(1, 1);\n// OK, 1 == 1\nassert.equal(1, '1');\n// OK, 1 == '1'\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
\n

If 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\nparameter is an instance of an [Error][] then it will be thrown instead of the\nAssertionError.

\n" }, { "textRaw": "assert.fail([message])", "type": "method", "name": "fail", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} **Default:** `'Failed'` ", "name": "message", "type": "any", "desc": "**Default:** `'Failed'`", "optional": true }, { "textRaw": "`operator` {string} **Default:** '!=' ", "name": "operator", "type": "string", "desc": "**Default:** '!='", "optional": true }, { "textRaw": "`stackStartFunction` {Function} **Default:** `assert.fail` ", "name": "stackStartFunction", "type": "Function", "desc": "**Default:** `assert.fail`", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true }, { "name": "operator", "optional": true }, { "name": "stackStartFunction", "optional": true } ] }, { "params": [ { "name": "message", "optional": true } ] } ], "desc": "

Throws an AssertionError. If message is falsy, the error message is set as\nthe values of actual and expected separated by the provided operator. If\nthe message parameter is an instance of an [Error][] then it will be thrown\ninstead of the AssertionError. If just the two actual and expected\narguments are provided, operator will default to '!='. If message is\nprovided only it will be used as the error message, the other arguments will be\nstored as properties on the thrown object. If stackStartFunction is provided,\nall stack frames above that function will be removed from stacktrace (see\nError.captureStackTrace). If no arguments are given, the default message\nFailed will be used.

\n
const assert = require('assert').strict;\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError [ERR_ASSERTION]: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError [ERR_ASSERTION]: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError [ERR_ASSERTION]: whoops\n\nassert.fail(1, 2, new TypeError('need array'));\n// TypeError: need array\n
\n

Note: In the last two cases actual, expected, and operator have no\ninfluence on the error message.

\n
assert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail('a', 'b');\n// AssertionError [ERR_ASSERTION]: 'a' != 'b'\n
\n

Example use of stackStartFunction for truncating the exception's stacktrace:

\n
function suppressFrame() {\n  assert.fail('a', 'b', undefined, '!==', suppressFrame);\n}\nsuppressFrame();\n// AssertionError [ERR_ASSERTION]: 'a' !== 'b'\n//     at repl:1:1\n//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)\n//     ...\n
\n" }, { "textRaw": "assert.fail(actual, expected[, message[, operator[, stackStartFunction]]])", "type": "method", "name": "fail", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} **Default:** `'Failed'` ", "name": "message", "type": "any", "desc": "**Default:** `'Failed'`", "optional": true }, { "textRaw": "`operator` {string} **Default:** '!=' ", "name": "operator", "type": "string", "desc": "**Default:** '!='", "optional": true }, { "textRaw": "`stackStartFunction` {Function} **Default:** `assert.fail` ", "name": "stackStartFunction", "type": "Function", "desc": "**Default:** `assert.fail`", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true }, { "name": "operator", "optional": true }, { "name": "stackStartFunction", "optional": true } ] } ], "desc": "

Throws an AssertionError. If message is falsy, the error message is set as\nthe values of actual and expected separated by the provided operator. If\nthe message parameter is an instance of an [Error][] then it will be thrown\ninstead of the AssertionError. If just the two actual and expected\narguments are provided, operator will default to '!='. If message is\nprovided only it will be used as the error message, the other arguments will be\nstored as properties on the thrown object. If stackStartFunction is provided,\nall stack frames above that function will be removed from stacktrace (see\nError.captureStackTrace). If no arguments are given, the default message\nFailed will be used.

\n
const assert = require('assert').strict;\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError [ERR_ASSERTION]: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError [ERR_ASSERTION]: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError [ERR_ASSERTION]: whoops\n\nassert.fail(1, 2, new TypeError('need array'));\n// TypeError: need array\n
\n

Note: In the last two cases actual, expected, and operator have no\ninfluence on the error message.

\n
assert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail('a', 'b');\n// AssertionError [ERR_ASSERTION]: 'a' != 'b'\n
\n

Example use of stackStartFunction for truncating the exception's stacktrace:

\n
function suppressFrame() {\n  assert.fail('a', 'b', undefined, '!==', suppressFrame);\n}\nsuppressFrame();\n// AssertionError [ERR_ASSERTION]: 'a' !== 'b'\n//     at repl:1:1\n//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)\n//     ...\n
\n" }, { "textRaw": "assert.ifError(value)", "type": "method", "name": "ifError", "meta": { "added": [ "v0.1.97" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} ", "name": "value", "type": "any" } ] }, { "params": [ { "name": "value" } ] } ], "desc": "

Throws value if value is truthy. This is useful when testing the error\nargument in callbacks.

\n
const assert = require('assert').strict;\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// OK\nassert.ifError(1);\n// Throws 1\nassert.ifError('error');\n// Throws 'error'\nassert.ifError(new Error());\n// Throws Error\n
\n" }, { "textRaw": "assert.notDeepEqual(actual, expected[, message])", "type": "method", "name": "notDeepEqual", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15001", "description": "Error names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "Set and Map content is also compared" }, { "version": "v6.4.0, v4.7.1", "pr-url": "https://github.com/nodejs/node/pull/8002", "description": "Typed array slices are handled correctly now." }, { "version": "v6.1.0, v4.5.0", "pr-url": "https://github.com/nodejs/node/pull/6432", "description": "Objects with circular references can be used as inputs now." }, { "version": "v5.10.1, v4.4.3", "pr-url": "https://github.com/nodejs/node/pull/5910", "description": "Handle non-`Uint8Array` typed arrays correctly." } ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Strict mode

\n

An alias of assert.notDeepStrictEqual().

\n

Legacy mode

\n
\n

Stability: 0 - Deprecated: Use assert.notDeepStrictEqual() instead.

\n
\n

Tests for any deep inequality. Opposite of assert.deepEqual().

\n
const assert = require('assert');\n\nconst obj1 = {\n  a: {\n    b: 1\n  }\n};\nconst obj2 = {\n  a: {\n    b: 2\n  }\n};\nconst obj3 = {\n  a: {\n    b: 1\n  }\n};\nconst obj4 = Object.create(obj1);\n\nassert.notDeepEqual(obj1, obj1);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj2);\n// OK: obj1 and obj2 are not deeply equal\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK: obj1 and obj4 are not deeply equal\n
\n

If the values are deeply 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\nparameter is an instance of an [Error][] then it will be thrown instead of the\nAssertionError.

\n" }, { "textRaw": "assert.notDeepStrictEqual(actual, expected[, message])", "type": "method", "name": "notDeepStrictEqual", "meta": { "added": [ "v1.2.0" ], "changes": [ { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15398", "description": "-0 and +0 are not considered equal anymore." }, { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15036", "description": "NaN is now compared using the [SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero) comparison." }, { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15001", "description": "Error names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "Set and Map content is also compared" }, { "version": "v6.4.0, v4.7.1", "pr-url": "https://github.com/nodejs/node/pull/8002", "description": "Typed array slices are handled correctly now." }, { "version": "v6.1.0", "pr-url": "https://github.com/nodejs/node/pull/6432", "description": "Objects with circular references can be used as inputs now." }, { "version": "v5.10.1, v4.4.3", "pr-url": "https://github.com/nodejs/node/pull/5910", "description": "Handle non-`Uint8Array` typed arrays correctly." } ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Tests for deep strict inequality. Opposite of assert.deepStrictEqual().

\n
const assert = require('assert').strict;\n\nassert.notDeepStrictEqual({ a: 1 }, { a: '1' });\n// OK\n
\n

If the values are deeply and strictly equal, an AssertionError is thrown with\na message 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 an [Error][] then it will be thrown\ninstead of the AssertionError.

\n" }, { "textRaw": "assert.notEqual(actual, expected[, message])", "type": "method", "name": "notEqual", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Strict mode

\n

An alias of assert.notStrictEqual().

\n

Legacy mode

\n
\n

Stability: 0 - Deprecated: Use assert.notStrictEqual() instead.

\n
\n

Tests shallow, coercive inequality with the Abstract Equality Comparison\n( != ).

\n
const assert = require('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
\n

If the values are equal, an AssertionError is thrown with a message property\nset equal to 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 an [Error][] then it will be thrown instead of the\nAssertionError.

\n" }, { "textRaw": "assert.notStrictEqual(actual, expected[, message])", "type": "method", "name": "notStrictEqual", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Tests strict inequality between the actual and expected parameters as\ndetermined by the SameValue Comparison.

\n
const assert = require('assert').strict;\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError: 1 !== 1\n\nassert.notStrictEqual(1, '1');\n// OK\n
\n

If the values are strictly 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\nparameter is an instance of an [Error][] then it will be thrown instead of the\nAssertionError.

\n" }, { "textRaw": "assert.ok(value[, message])", "type": "method", "name": "ok", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} ", "name": "value", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "value" }, { "name": "message", "optional": true } ] } ], "desc": "

Tests if value is truthy. It is equivalent to\nassert.equal(!!value, true, message).

\n

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\nparameter is an instance of an [Error][] then it will be thrown instead of the\nAssertionError.

\n
const assert = require('assert').strict;\n\nassert.ok(true);\n// OK\nassert.ok(1);\n// OK\nassert.ok(false);\n// throws "AssertionError: false == true"\nassert.ok(0);\n// throws "AssertionError: 0 == true"\nassert.ok(false, 'it\\'s false');\n// throws "AssertionError: it's false"\n
\n" }, { "textRaw": "assert.strictEqual(actual, expected[, message])", "type": "method", "name": "strictEqual", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Tests strict equality between the actual and expected parameters as\ndetermined by the SameValue Comparison.

\n
const assert = require('assert').strict;\n\nassert.strictEqual(1, 2);\n// AssertionError: 1 === 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual(1, '1');\n// AssertionError: 1 === '1'\n
\n

If 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 an [Error][] then it will be thrown\ninstead of the AssertionError.

\n" }, { "textRaw": "assert.throws(block[, error][, message])", "type": "method", "name": "throws", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v9.9.0", "pr-url": "https://github.com/nodejs/node/pull/17584", "description": "The `error` parameter can now be an object as well." }, { "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": "`block` {Function} ", "name": "block", "type": "Function" }, { "textRaw": "`error` {RegExp|Function|object} ", "name": "error", "type": "RegExp|Function|object", "optional": true }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "block" }, { "name": "error", "optional": true }, { "name": "message", "optional": true } ] } ], "desc": "

Expects the function block to throw an error.

\n

If specified, error can be a constructor, RegExp, a validation\nfunction, or an object where each property will be tested for.

\n

If specified, message will be the message provided by the AssertionError if\nthe block fails to throw.

\n

Validate instanceof using constructor:

\n
assert.throws(\n  () => {\n    throw new Error('Wrong value');\n  },\n  Error\n);\n
\n

Validate error message using RegExp:

\n

Using a regular expression runs .toString on the error object, and will\ntherefore also include the error name.

\n
assert.throws(\n  () => {\n    throw new Error('Wrong value');\n  },\n  /^Error: Wrong value$/\n);\n
\n

Custom error validation:

\n
assert.throws(\n  () => {\n    throw new Error('Wrong value');\n  },\n  function(err) {\n    if ((err instanceof Error) && /value/.test(err)) {\n      return true;\n    }\n  },\n  'unexpected error'\n);\n
\n

Custom error object / error instance:

\n
assert.throws(\n  () => {\n    const err = new TypeError('Wrong value');\n    err.code = 404;\n    throw err;\n  },\n  {\n    name: 'TypeError',\n    message: 'Wrong value'\n    // Note that only properties on the error object will be tested!\n  }\n);\n
\n

Note that error can not 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 below carefully if using a string as the second argument gets\nconsidered:

\n\n
function throwingFirst() {\n  throw new Error('First');\n}\nfunction throwingSecond() {\n  throw new Error('Second');\n}\nfunction notThrowing() {}\n\n// The second argument is a string and the input function threw an Error.\n// In that case both cases do not throw as neither is going to try to\n// match for the error message thrown by the input function!\nassert.throws(throwingFirst, 'Second');\nassert.throws(throwingSecond, 'Second');\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:\nassert.throws(throwingSecond, /Second$/);\n// Does not throw because the error messages match.\nassert.throws(throwingFirst, /Second$/);\n// Throws a error:\n// Error: First\n//     at throwingFirst (repl:2:9)\n
\n

Due to the confusing notation, it is recommended not to use a string as the\nsecond argument. This might lead to difficult-to-spot errors.

\n" } ], "type": "module", "displayName": "Assert" } ] }