/
opt
/
alt
/
alt-nodejs10
/
root
/
usr
/
share
/
doc
/
alt-nodejs10-nodejs-10.24.1
/
html
/
api
/
Upload Filee
HOME
{ "type": "module", "source": "doc/api/assert.md", "modules": [ { "textRaw": "Assert", "name": "assert", "introduced_in": "v0.1.21", "stability": 2, "stabilityText": "Stable", "desc": "<p>The <code>assert</code> module provides a simple set of assertion tests that can be used to\ntest invariants.</p>\n<p>A <code>strict</code> and a <code>legacy</code> mode exist, while it is recommended to only use\n<a href=\"#assert_strict_mode\"><code>strict mode</code></a>.</p>\n<p>For more information about the used equality comparisons see\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness\">MDN's guide on equality comparisons and sameness</a>.</p>", "classes": [ { "textRaw": "Class: assert.AssertionError", "type": "class", "name": "assert.AssertionError", "desc": "<p>A subclass of <code>Error</code> that indicates the failure of an assertion. All errors\nthrown by the <code>assert</code> module will be instances of the <code>AssertionError</code> class.</p>", "signatures": [ { "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`message` {string} If provided, the error message is going to be set to this value.", "name": "message", "type": "string", "desc": "If provided, the error message is going to be set to this value." }, { "textRaw": "`actual` {any} The `actual` property on the error instance is going to contain this value. Internally used for the `actual` error input in case e.g., [`assert.strictEqual()`] is used.", "name": "actual", "type": "any", "desc": "The `actual` property on the error instance is going to contain this value. Internally used for the `actual` error input in case e.g., [`assert.strictEqual()`] is used." }, { "textRaw": "`expected` {any} The `expected` property on the error instance is going to contain this value. Internally used for the `expected` error input in case e.g., [`assert.strictEqual()`] is used.", "name": "expected", "type": "any", "desc": "The `expected` property on the error instance is going to contain this value. Internally used for the `expected` error input in case e.g., [`assert.strictEqual()`] is used." }, { "textRaw": "`operator` {string} The `operator` property on the error instance is going to contain this value. Internally used to indicate what operation was used for comparison (or what assertion function triggered the error).", "name": "operator", "type": "string", "desc": "The `operator` property on the error instance is going to contain this value. Internally used to indicate what operation was used for comparison (or what assertion function triggered the error)." }, { "textRaw": "`stackStartFn` {Function} If provided, the generated stack trace is going to remove all frames up to the provided function.", "name": "stackStartFn", "type": "Function", "desc": "If provided, the generated stack trace is going to remove all frames up to the provided function." } ] } ], "desc": "<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p>\n<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>)\nand:</p>\n<ul>\n<li><code>actual</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\"><any></a> Set to the actual value in case e.g.,\n<a href=\"#assert_assert_strictequal_actual_expected_message\"><code>assert.strictEqual()</code></a> is used.</li>\n<li><code>expected</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\"><any></a> Set to the expected value in case e.g.,\n<a href=\"#assert_assert_strictequal_actual_expected_message\"><code>assert.strictEqual()</code></a> is used.</li>\n<li><code>generatedMessage</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\"><boolean></a> Indicates if the message was auto-generated\n(<code>true</code>) or not.</li>\n<li><code>code</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> This is always set to the string <code>ERR_ASSERTION</code> to indicate\nthat the error is actually an assertion error.</li>\n<li><code>operator</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Set to the passed in operator value.</li>\n</ul>\n<pre><code class=\"language-js\">const assert = require('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 [ERR_ASSERTION]');\n assert.strictEqual(err.actual, 1);\n assert.strictEqual(err.expected, 2);\n assert.strictEqual(err.code, 'ERR_ASSERTION');\n assert.strictEqual(err.operator, 'strictEqual');\n assert.strictEqual(err.generatedMessage, true);\n}\n</code></pre>" } ] } ], "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": "<p>When using the <code>strict mode</code>, any <code>assert</code> function will use the equality used\nin the strict function mode. So <a href=\"#assert_assert_deepequal_actual_expected_message\"><code>assert.deepEqual()</code></a> will, for example,\nwork the same as <a href=\"#assert_assert_deepstrictequal_actual_expected_message\"><code>assert.deepStrictEqual()</code></a>.</p>\n<p>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.</p>\n<p>It can be accessed using:</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n</code></pre>\n<p>Example error diff:</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);\n// AssertionError: Input A expected to strictly deep-equal input B:\n// + expected - actual ... Lines skipped\n//\n// [\n// [\n// ...\n// 2,\n// - 3\n// + '3'\n// ],\n// ...\n// 5\n// ]\n</code></pre>\n<p>To deactivate the colors, use the <code>NODE_DISABLE_COLORS</code> environment variable.\nPlease note that this will also deactivate the colors in the REPL.</p>", "type": "module", "displayName": "Strict mode" }, { "textRaw": "Legacy mode", "name": "legacy_mode", "stability": 0, "stabilityText": "Deprecated: Use strict mode instead.", "desc": "<p>When accessing <code>assert</code> directly instead of using the <code>strict</code> property, the\n<a href=\"https://tc39.github.io/ecma262/#sec-abstract-equality-comparison\">Abstract Equality Comparison</a> will be used for any function without \"strict\"\nin its name, such as <a href=\"#assert_assert_deepequal_actual_expected_message\"><code>assert.deepEqual()</code></a>.</p>\n<p>It can be accessed using:</p>\n<pre><code class=\"language-js\">const assert = require('assert');\n</code></pre>\n<p>It is recommended to use the <a href=\"#assert_strict_mode\"><code>strict mode</code></a> instead as the\n<a href=\"https://tc39.github.io/ecma262/#sec-abstract-equality-comparison\">Abstract Equality Comparison</a> can often have surprising results. This is\nespecially true for <a href=\"#assert_assert_deepequal_actual_expected_message\"><code>assert.deepEqual()</code></a>, where the comparison rules are\nlax:</p>\n<pre><code class=\"language-js\">// WARNING: This does not throw an AssertionError!\nassert.deepEqual(/a/gi, new Date());\n</code></pre>", "type": "module", "displayName": "Legacy mode" } ], "methods": [ { "textRaw": "assert(value[, message])", "type": "method", "name": "assert", "meta": { "added": [ "v0.5.9" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} The input that is checked for being truthy.", "name": "value", "type": "any", "desc": "The input that is checked for being truthy." }, { "textRaw": "`message` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p>An alias of <a href=\"#assert_assert_ok_value_message\"><code>assert.ok()</code></a>.</p>" }, { "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": "The `Error` names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "The `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` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p><strong>Strict mode</strong></p>\n<p>An alias of <a href=\"#assert_assert_deepstrictequal_actual_expected_message\"><code>assert.deepStrictEqual()</code></a>.</p>\n<p><strong>Legacy mode</strong></p>\n<blockquote>\n<p>Stability: 0 - Deprecated: Use <a href=\"#assert_assert_deepstrictequal_actual_expected_message\"><code>assert.deepStrictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.\nPrimitive values are compared with the <a href=\"https://tc39.github.io/ecma262/#sec-abstract-equality-comparison\">Abstract Equality Comparison</a>\n( <code>==</code> ).</p>\n<p>Only <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties\">enumerable \"own\" properties</a> are considered. The\n<a href=\"#assert_assert_deepequal_actual_expected_message\"><code>assert.deepEqual()</code></a> implementation does not test the\n<a href=\"https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots\"><code>[[Prototype]]</code></a> of objects or enumerable own <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol\"><code>Symbol</code></a>\nproperties. For such checks, consider using <a href=\"#assert_assert_deepstrictequal_actual_expected_message\"><code>assert.deepStrictEqual()</code></a>\ninstead. <a href=\"#assert_assert_deepequal_actual_expected_message\"><code>assert.deepEqual()</code></a> can have potentially surprising results. The\nfollowing example does not throw an <code>AssertionError</code> because the properties on\nthe <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a> object are not enumerable:</p>\n<pre><code class=\"language-js\">// WARNING: This does not throw an AssertionError!\nassert.deepEqual(/a/gi, new Date());\n</code></pre>\n<p>An exception is made for <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map\"><code>Map</code></a> and <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set\"><code>Set</code></a>. <code>Map</code>s and <code>Set</code>s have their\ncontained items compared too, as expected.</p>\n<p>\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare evaluated also:</p>\n<pre><code class=\"language-js\">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\n\n// Values of b are different:\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n\nassert.deepEqual(obj1, obj3);\n// OK\n\n// Prototypes are ignored:\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n</code></pre>\n<p>If the values are not equal, an <code>AssertionError</code> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" }, { "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": "The `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": "The `Error` names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "The `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` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.\n\"Deep\" equality means that the enumerable \"own\" properties of child objects\nare recursively evaluated also by the following rules.</p>", "modules": [ { "textRaw": "Comparison details", "name": "comparison_details", "desc": "<ul>\n<li>Primitive values are compared using the <a href=\"https://tc39.github.io/ecma262/#sec-samevalue\">SameValue Comparison</a>, used by\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is\"><code>Object.is()</code></a>.</li>\n<li><a href=\"https://tc39.github.io/ecma262/#sec-object.prototype.tostring\">Type tags</a> of objects should be the same.</li>\n<li><a href=\"https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots\"><code>[[Prototype]]</code></a> of objects are compared using\nthe <a href=\"https://tc39.github.io/ecma262/#sec-strict-equality-comparison\">Strict Equality Comparison</a>.</li>\n<li>Only <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties\">enumerable \"own\" properties</a> are considered.</li>\n<li><a href=\"errors.html#errors_class_error\"><code>Error</code></a> names and messages are always compared, even if these are not\nenumerable properties.</li>\n<li>Enumerable own <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol\"><code>Symbol</code></a> properties are compared as well.</li>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript\">Object wrappers</a> are compared both as objects and unwrapped values.</li>\n<li><code>Object</code> properties are compared unordered.</li>\n<li><code>Map</code> keys and <code>Set</code> items are compared unordered.</li>\n<li>Recursion stops when both sides differ or both sides encounter a circular\nreference.</li>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap\"><code>WeakMap</code></a> and <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet\"><code>WeakSet</code></a> comparison does not rely on their values. See\nbelow for further details.</li>\n</ul>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\n// This fails because 1 !== '1'.\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: Input A expected to strictly deep-equal input B:\n// + expected - actual\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: Input A expected to strictly deep-equal input B:\n// + expected - actual\n// - {}\n// + Date {}\n\n// Different type tags:\nassert.deepStrictEqual(date, fakeDate);\n// AssertionError: Input A expected to strictly deep-equal input B:\n// + expected - actual\n// - 2018-04-26T00:49:08.604Z\n// + Date {}\n\nassert.deepStrictEqual(NaN, NaN);\n// OK, because of the SameValue comparison\n\n// Different unwrapped numbers:\nassert.deepStrictEqual(new Number(1), new Number(2));\n// AssertionError: Input A expected to strictly deep-equal input B:\n// + expected - actual\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 using the SameValue Comparison:\nassert.deepStrictEqual(0, -0);\n// AssertionError: Input A expected to strictly deep-equal input B:\n// + expected - actual\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.\nassert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });\n// AssertionError [ERR_ASSERTION]: Input objects not identical:\n// {\n// [Symbol()]: 1\n// }\n\nconst weakMap1 = new WeakMap();\nconst weakMap2 = new WeakMap([[{}, {}]]);\nconst weakMap3 = new WeakMap();\nweakMap3.unequal = true;\n\nassert.deepStrictEqual(weakMap1, weakMap2);\n// OK, because it is impossible to compare the entries\n\n// Fails because weakMap3 has a property that weakMap1 does not contain:\nassert.deepStrictEqual(weakMap1, weakMap3);\n// AssertionError: Input A expected to strictly deep-equal input B:\n// + expected - actual\n// WeakMap {\n// - [items unknown]\n// + [items unknown],\n// + unequal: true\n// }\n</code></pre>\n<p>If the values are not equal, an <code>AssertionError</code> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>", "type": "module", "displayName": "Comparison details" } ] }, { "textRaw": "assert.doesNotReject(asyncFn[, error][, message])", "type": "method", "name": "doesNotReject", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`asyncFn` {Function|Promise}", "name": "asyncFn", "type": "Function|Promise" }, { "textRaw": "`error` {RegExp|Function}", "name": "error", "type": "RegExp|Function", "optional": true }, { "textRaw": "`message` {string}", "name": "message", "type": "string", "optional": true } ] } ], "desc": "<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately\ncalls the function and awaits the returned promise to complete. It will then\ncheck that the promise is not rejected.</p>\n<p>If <code>asyncFn</code> is a function and it throws an error synchronously,\n<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If\nthe function does not return a promise, <code>assert.doesNotReject()</code> will return a\nrejected <code>Promise</code> with an <a href=\"errors.html#errors_err_invalid_return_value\"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases\nthe error handler is skipped.</p>\n<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little\nbenefit in catching a rejection and then rejecting it again. Instead, consider\nadding a comment next to the specific code path that should not reject and keep\nerror messages as expressive as possible.</p>\n<p>If specified, <code>error</code> can be a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\"><code>Class</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a> or a validation\nfunction. See <a href=\"#assert_assert_throws_fn_error_message\"><code>assert.throws()</code></a> for more details.</p>\n<p>Besides the async nature to await the completion behaves identically to\n<a href=\"#assert_assert_doesnotthrow_fn_error_message\"><code>assert.doesNotThrow()</code></a>.</p>\n<pre><code class=\"language-js\">(async () => {\n await assert.doesNotReject(\n async () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError\n );\n})();\n</code></pre>\n<pre><code class=\"language-js\">assert.doesNotReject(Promise.reject(new TypeError('Wrong value')))\n .then(() => {\n // ...\n });\n</code></pre>" }, { "textRaw": "assert.doesNotThrow(fn[, 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": "`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": "<p>Asserts that the function <code>fn</code> does not throw an error.</p>\n<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there\nis no benefit in catching an error and then rethrowing it. Instead, consider\nadding a comment next to the specific code path that should not throw and keep\nerror messages as expressive as possible.</p>\n<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code>\nfunction.</p>\n<p>If an error is thrown and it is the same type as that specified by the <code>error</code>\nparameter, then an <code>AssertionError</code> is thrown. If the error is of a different\ntype, or if the <code>error</code> parameter is undefined, the error is propagated back\nto the caller.</p>\n<p>If specified, <code>error</code> can be a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\"><code>Class</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a> or a validation\nfunction. See <a href=\"#assert_assert_throws_fn_error_message\"><code>assert.throws()</code></a> for more details.</p>\n<p>The following, for instance, will throw the <a href=\"errors.html#errors_class_typeerror\"><code>TypeError</code></a> because there is no\nmatching error type in the assertion:</p>\n<!-- eslint-disable no-restricted-syntax -->\n<pre><code class=\"language-js\">assert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError\n);\n</code></pre>\n<p>However, the following will result in an <code>AssertionError</code> with the message\n'Got unwanted exception...':</p>\n<!-- eslint-disable no-restricted-syntax -->\n<pre><code class=\"language-js\">assert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n TypeError\n);\n</code></pre>\n<p>If an <code>AssertionError</code> is thrown and a value is provided for the <code>message</code>\nparameter, the value of <code>message</code> will be appended to the <code>AssertionError</code>\nmessage:</p>\n<!-- eslint-disable no-restricted-syntax -->\n<pre><code class=\"language-js\">assert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n /Wrong value/,\n 'Whoops'\n);\n// Throws: AssertionError: Got unwanted exception: Whoops\n</code></pre>" }, { "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` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p><strong>Strict mode</strong></p>\n<p>An alias of <a href=\"#assert_assert_strictequal_actual_expected_message\"><code>assert.strictEqual()</code></a>.</p>\n<p><strong>Legacy mode</strong></p>\n<blockquote>\n<p>Stability: 0 - Deprecated: Use <a href=\"#assert_assert_strictequal_actual_expected_message\"><code>assert.strictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters\nusing the <a href=\"https://tc39.github.io/ecma262/#sec-abstract-equality-comparison\">Abstract Equality Comparison</a> ( <code>==</code> ).</p>\n<pre><code class=\"language-js\">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</code></pre>\n<p>If the values are not equal, an <code>AssertionError</code> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" }, { "textRaw": "assert.fail([message])", "type": "method", "name": "fail", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`message` {string|Error} **Default:** `'Failed'`", "name": "message", "type": "string|Error", "default": "`'Failed'`", "optional": true } ] } ], "desc": "<p>Throws an <code>AssertionError</code> with the provided error message or a default error\nmessage. If the <code>message</code> parameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it\nwill be thrown instead of the <code>AssertionError</code>.</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.fail();\n// AssertionError [ERR_ASSERTION]: Failed\n\nassert.fail('boom');\n// AssertionError [ERR_ASSERTION]: boom\n\nassert.fail(new TypeError('need array'));\n// TypeError: need array\n</code></pre>\n<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated.\nSee below for further details.</p>" }, { "textRaw": "assert.fail(actual, expected[, message[, operator[, stackStartFn]]])", "type": "method", "name": "fail", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18418", "description": "Calling `assert.fail()` with more than one argument is deprecated and emits a warning." } ] }, "stability": 0, "stabilityText": "Deprecated: Use `assert.fail([message])` or other assert\nfunctions 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 }, { "textRaw": "`operator` {string} **Default:** `'!='`", "name": "operator", "type": "string", "default": "`'!='`", "optional": true }, { "textRaw": "`stackStartFn` {Function} **Default:** `assert.fail`", "name": "stackStartFn", "type": "Function", "default": "`assert.fail`", "optional": true } ] } ], "desc": "<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and\n<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and\n<code>expected</code> arguments are provided, <code>operator</code> will default to <code>'!='</code>. If\n<code>message</code> is provided as third argument it will be used as the error message and\nthe other arguments will be stored as properties on the thrown object. If\n<code>stackStartFn</code> is provided, all stack frames above that function will be\nremoved from stacktrace (see <a href=\"errors.html#errors_error_capturestacktrace_targetobject_constructoropt\"><code>Error.captureStackTrace</code></a>). If no arguments are\ngiven, the default message <code>Failed</code> will be used.</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.fail('a', 'b');\n// AssertionError [ERR_ASSERTION]: 'a' != 'b'\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError [ERR_ASSERTION]: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError [ERR_ASSERTION]: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError [ERR_ASSERTION]: whoops\n\nassert.fail(1, 2, new TypeError('need array'));\n// TypeError: need array\n</code></pre>\n<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no\ninfluence on the error message.</p>\n<p>Example use of <code>stackStartFn</code> for truncating the exception's stacktrace:</p>\n<pre><code class=\"language-js\">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</code></pre>" }, { "textRaw": "assert.ifError(value)", "type": "method", "name": "ifError", "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": "<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when\ntesting the <code>error</code> argument in callbacks. The stack trace contains all frames\nfrom the error passed to <code>ifError()</code> including the potential new frames for\n<code>ifError()</code> itself.</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0\nassert.ifError('error');\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'\nassert.ifError(new Error());\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error\n\n// Create some random error frames.\nlet err;\n(function errorFrame() {\n err = new Error('test error');\n})();\n\n(function ifErrorFrame() {\n assert.ifError(err);\n})();\n// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error\n// at ifErrorFrame\n// at errorFrame\n</code></pre>" }, { "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": "The `Error` names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "The `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` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p><strong>Strict mode</strong></p>\n<p>An alias of <a href=\"#assert_assert_notdeepstrictequal_actual_expected_message\"><code>assert.notDeepStrictEqual()</code></a>.</p>\n<p><strong>Legacy mode</strong></p>\n<blockquote>\n<p>Stability: 0 - Deprecated: Use <a href=\"#assert_assert_notdeepstrictequal_actual_expected_message\"><code>assert.notDeepStrictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests for any deep inequality. Opposite of <a href=\"#assert_assert_deepequal_actual_expected_message\"><code>assert.deepEqual()</code></a>.</p>\n<pre><code class=\"language-js\">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\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK\n</code></pre>\n<p>If the values are deeply equal, an <code>AssertionError</code> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" }, { "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": "The `-0` and `+0` are not considered equal anymore." }, { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/15036", "description": "The `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": "The `Error` names and messages are now properly compared" }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/12142", "description": "The `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` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p>Tests for deep strict inequality. Opposite of <a href=\"#assert_assert_deepstrictequal_actual_expected_message\"><code>assert.deepStrictEqual()</code></a>.</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.notDeepStrictEqual({ a: 1 }, { a: '1' });\n// OK\n</code></pre>\n<p>If the values are deeply and strictly equal, an <code>AssertionError</code> is thrown with\na <code>message</code> property set equal to the value of the <code>message</code> parameter. If the\n<code>message</code> parameter is undefined, a default error message is assigned. If the\n<code>message</code> parameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown\ninstead of the <code>AssertionError</code>.</p>" }, { "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` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p><strong>Strict mode</strong></p>\n<p>An alias of <a href=\"#assert_assert_notstrictequal_actual_expected_message\"><code>assert.notStrictEqual()</code></a>.</p>\n<p><strong>Legacy mode</strong></p>\n<blockquote>\n<p>Stability: 0 - Deprecated: Use <a href=\"#assert_assert_notstrictequal_actual_expected_message\"><code>assert.notStrictEqual()</code></a> instead.</p>\n</blockquote>\n<p>Tests shallow, coercive inequality with the <a href=\"https://tc39.github.io/ecma262/#sec-abstract-equality-comparison\">Abstract Equality Comparison</a>\n( <code>!=</code> ).</p>\n<pre><code class=\"language-js\">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</code></pre>\n<p>If the values are equal, an <code>AssertionError</code> is thrown with a <code>message</code> property\nset equal to the value of the <code>message</code> parameter. If the <code>message</code> parameter is\nundefined, a default error message is assigned. If the <code>message</code> parameter is an\ninstance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" }, { "textRaw": "assert.notStrictEqual(actual, expected[, message])", "type": "method", "name": "notStrictEqual", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/17003", "description": "Used comparison changed from Strict Equality to `Object.is()`" } ] }, "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": "<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as\ndetermined by the <a href=\"https://tc39.github.io/ecma262/#sec-samevalue\">SameValue Comparison</a>.</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError [ERR_ASSERTION]: Identical input passed to notStrictEqual: 1\n\nassert.notStrictEqual(1, '1');\n// OK\n</code></pre>\n<p>If the values are strictly equal, an <code>AssertionError</code> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is undefined, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.</p>" }, { "textRaw": "assert.ok(value[, message])", "type": "method", "name": "ok", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18319", "description": "The `assert.ok()` (no arguments) will now use a predefined error message." } ] }, "signatures": [ { "params": [ { "textRaw": "`value` {any}", "name": "value", "type": "any" }, { "textRaw": "`message` {string|Error}", "name": "message", "type": "string|Error", "optional": true } ] } ], "desc": "<p>Tests if <code>value</code> is truthy. It is equivalent to\n<code>assert.equal(!!value, true, message)</code>.</p>\n<p>If <code>value</code> is not truthy, an <code>AssertionError</code> is thrown with a <code>message</code>\nproperty set equal to the value of the <code>message</code> parameter. If the <code>message</code>\nparameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code>\nparameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown instead of the\n<code>AssertionError</code>.\nIf no arguments are passed in at all <code>message</code> will be set to the string:\n<code>'No value argument passed to `assert.ok()`'</code>.</p>\n<p>Be aware that in the <code>repl</code> the error message will be different to the one\nthrown in a file! See below for further details.</p>\n<pre><code class=\"language-js\">const assert = require('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\n// Using `assert()` works the same:\nassert(0);\n// AssertionError: The expression evaluated to a falsy value:\n//\n// assert(0)\n</code></pre>" }, { "textRaw": "assert.rejects(asyncFn[, error][, message])", "type": "method", "name": "rejects", "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 } ] } ], "desc": "<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately\ncalls the function and awaits the returned promise to complete. It will then\ncheck that the promise is rejected.</p>\n<p>If <code>asyncFn</code> is a function and it throws an error synchronously,\n<code>assert.rejects()</code> will return a rejected <code>Promise</code> with that error. If the\nfunction does not return a promise, <code>assert.rejects()</code> will return a rejected\n<code>Promise</code> with an <a href=\"errors.html#errors_err_invalid_return_value\"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases the error\nhandler is skipped.</p>\n<p>Besides the async nature to await the completion behaves identically to\n<a href=\"#assert_assert_throws_fn_error_message\"><code>assert.throws()</code></a>.</p>\n<p>If specified, <code>error</code> can be a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\"><code>Class</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a>, a validation function,\nan object where each property will be tested for, or an instance of error where\neach property will be tested for including the non-enumerable <code>message</code> and\n<code>name</code> properties.</p>\n<p>If specified, <code>message</code> will be the message provided by the <code>AssertionError</code> if\nthe <code>asyncFn</code> fails to reject.</p>\n<pre><code class=\"language-js\">(async () => {\n await assert.rejects(\n async () => {\n throw new TypeError('Wrong value');\n },\n {\n name: 'TypeError',\n message: 'Wrong value'\n }\n );\n})();\n</code></pre>\n<pre><code class=\"language-js\">assert.rejects(\n Promise.reject(new Error('Wrong value')),\n Error\n).then(() => {\n // ...\n});\n</code></pre>\n<p>Note that <code>error</code> cannot be a string. If a string is provided as the second\nargument, then <code>error</code> is assumed to be omitted and the string will be used for\n<code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the\nexample in <a href=\"#assert_assert_throws_fn_error_message\"><code>assert.throws()</code></a> carefully if using a string as the second\nargument gets considered.</p>" }, { "textRaw": "assert.strictEqual(actual, expected[, message])", "type": "method", "name": "strictEqual", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/17003", "description": "Used comparison changed from Strict Equality to `Object.is()`" } ] }, "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": "<p>Tests strict equality between the <code>actual</code> and <code>expected</code> parameters as\ndetermined by the <a href=\"https://tc39.github.io/ecma262/#sec-samevalue\">SameValue Comparison</a>.</p>\n<pre><code class=\"language-js\">const assert = require('assert').strict;\n\nassert.strictEqual(1, 2);\n// AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:\n// + expected - actual\n// - 1\n// + 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual(1, '1');\n// AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:\n// + expected - actual\n// - 1\n// + '1'\n</code></pre>\n<p>If the values are not strictly equal, an <code>AssertionError</code> is thrown with a\n<code>message</code> property set equal to the value of the <code>message</code> parameter. If the\n<code>message</code> parameter is undefined, a default error message is assigned. If the\n<code>message</code> parameter is an instance of an <a href=\"errors.html#errors_class_error\"><code>Error</code></a> then it will be thrown\ninstead of the <code>AssertionError</code>.</p>" }, { "textRaw": "assert.throws(fn[, error][, message])", "type": "method", "name": "throws", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.2.0", "pr-url": "https://github.com/nodejs/node/pull/20485", "description": "The `error` parameter can be an object containing regular expressions now." }, { "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": "`fn` {Function}", "name": "fn", "type": "Function" }, { "textRaw": "`error` {RegExp|Function|Object|Error}", "name": "error", "type": "RegExp|Function|Object|Error", "optional": true }, { "textRaw": "`message` {string}", "name": "message", "type": "string", "optional": true } ] } ], "desc": "<p>Expects the function <code>fn</code> to throw an error.</p>\n<p>If specified, <code>error</code> can be a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\"><code>Class</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a>, a validation function,\na validation object where each property will be tested for strict deep equality,\nor an instance of error where each property will be tested for strict deep\nequality including the non-enumerable <code>message</code> and <code>name</code> properties. When\nusing an object, it is also possible to use a regular expression, when\nvalidating against a string property. See below for examples.</p>\n<p>If specified, <code>message</code> will be appended to the message provided by the\n<code>AssertionError</code> if the <code>fn</code> call fails to throw or in case the error validation\nfails.</p>\n<p>Custom validation object/error instance:</p>\n<pre><code class=\"language-js\">const 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 // Note that 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 otherErr.code = 404;\n throw otherErr;\n },\n err // This tests for `message`, `name` and `code`.\n);\n</code></pre>\n<p>Validate instanceof using constructor:</p>\n<pre><code class=\"language-js\">assert.throws(\n () => {\n throw new Error('Wrong value');\n },\n Error\n);\n</code></pre>\n<p>Validate error message using <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\"><code>RegExp</code></a>:</p>\n<p>Using a regular expression runs <code>.toString</code> on the error object, and will\ntherefore also include the error name.</p>\n<pre><code class=\"language-js\">assert.throws(\n () => {\n throw new Error('Wrong value');\n },\n /^Error: Wrong value$/\n);\n</code></pre>\n<p>Custom error validation:</p>\n<pre><code class=\"language-js\">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</code></pre>\n<p>Note that <code>error</code> cannot be a string. If a string is provided as the second\nargument, then <code>error</code> is assumed to be omitted and the string will be used for\n<code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same\nmessage as the thrown error message is going to result in an\n<code>ERR_AMBIGUOUS_ARGUMENT</code> error. Please read the example below carefully if using\na string as the second argument gets considered:</p>\n<!-- eslint-disable no-restricted-syntax -->\n<pre><code class=\"language-js\">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// 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 thrown an `ERR_AMBIGUOUS_ARGUMENT` error.\nassert.throws(throwingSecond, 'Second');\n// Throws an error:\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:\nassert.throws(throwingSecond, /Second$/);\n// Does not throw because the error messages match.\nassert.throws(throwingFirst, /Second$/);\n// Throws an error:\n// Error: First\n// at throwingFirst (repl:2:9)\n</code></pre>\n<p>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.</p>" } ], "type": "module", "displayName": "Assert" } ] }