Skip to content

Commit 5bdd6a7

Browse files
committed
doc: properly document AssertionError
The AssertionError was always exposed but never properly documented. This explains how it is used and what options it accepts. PR-URL: #19724 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ceaeee0 commit 5bdd6a7

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

doc/api/assert.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,70 @@ A `strict` and a `legacy` mode exist, while it is recommended to only use
1313
For more information about the used equality comparisons see
1414
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
1515

16+
## Class: assert.AssertionError
17+
18+
A subclass of `Error` that indicates the failure of an assertion. All errors
19+
thrown by the `assert` module will be instances of the `AssertionError` class.
20+
21+
### new assert.AssertionError(options)
22+
<!-- YAML
23+
added: v0.1.21
24+
-->
25+
* `options` {Object}
26+
* `message` {string} If provided, the error message is going to be set to this
27+
value.
28+
* `actual` {any} The `actual` property on the error instance is going to
29+
contain this value. Internally used for the `actual` error input in case
30+
e.g., [`assert.strictEqual()`] is used.
31+
* `expected` {any} The `expected` property on the error instance is going to
32+
contain this value. Internally used for the `expected` error input in case
33+
e.g., [`assert.strictEqual()`] is used.
34+
* `operator` {string} The `operator` property on the error instance is going
35+
to contain this value. Internally used to indicate what operation was used
36+
for comparison (or what assertion function triggered the error).
37+
* `stackStartFn` {Function} If provided, the generated stack trace is going to
38+
remove all frames up to the provided function.
39+
40+
A subclass of `Error` that indicates the failure of an assertion.
41+
42+
All instances contain the built-in `Error` properties (`message` and `name`)
43+
and:
44+
45+
* `actual` {any} Set to the actual value in case e.g.,
46+
[`assert.strictEqual()`] is used.
47+
* `expected` {any} Set to the expected value in case e.g.,
48+
[`assert.strictEqual()`] is used.
49+
* `generatedMessage` {boolean} Indicates if the message was auto-generated
50+
(`true`) or not.
51+
* `code` {string} This is always set to the string `ERR_ASSERTION` to indicate
52+
that the error is actually an assertion error.
53+
* `operator` {string} Set to the passed in operator value.
54+
55+
```js
56+
const assert = require('assert');
57+
58+
// Generate an AssertionError to compare the error message later:
59+
const { message } = new assert.AssertionError({
60+
actual: 1,
61+
expected: 2,
62+
operator: 'strictEqual'
63+
});
64+
65+
// Verify error output:
66+
try {
67+
assert.strictEqual(1, 2);
68+
} catch (err) {
69+
assert(err instanceof assert.AssertionError);
70+
assert.strictEqual(err.message, message);
71+
assert.strictEqual(err.name, 'AssertionError [ERR_ASSERTION]');
72+
assert.strictEqual(err.actual, 1);
73+
assert.strictEqual(err.expected, 2);
74+
assert.strictEqual(err.code, 'ERR_ASSERTION');
75+
assert.strictEqual(err.operator, 'strictEqual');
76+
assert.strictEqual(err.generatedMessage, true);
77+
}
78+
```
79+
1680
## Strict mode
1781
<!-- YAML
1882
added: v9.9.0

doc/api/errors.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,8 @@ detailed [here](#errors_system_errors).
364364

365365
## Class: AssertionError
366366

367-
A subclass of `Error` that indicates the failure of an assertion. Such errors
368-
commonly indicate inequality of actual and expected value.
369-
370-
```js
371-
assert.strictEqual(1, 2);
372-
// AssertionError [ERR_ASSERTION]: 1 === 2
373-
```
367+
A subclass of `Error` that indicates the failure of an assertion. For details,
368+
see [`Class: assert.AssertionError`][].
374369

375370
## Class: RangeError
376371

@@ -1680,6 +1675,7 @@ Creation of a [`zlib`][] object failed due to incorrect configuration.
16801675
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn
16811676
[`require('crypto').setEngine()`]: crypto.html#crypto_crypto_setengine_engine_flags
16821677
[`server.listen()`]: net.html#net_server_listen
1678+
[`Class: assert.AssertionError`]: assert.html#assert_class_assert_assertionerror
16831679
[ES6 module]: esm.html
16841680
[Node.js Error Codes]: #nodejs-error-codes
16851681
[V8's stack trace API]: https://github.com/v8/v8/wiki/Stack-Trace-API

0 commit comments

Comments
 (0)