Skip to content

Commit 7a2b3e2

Browse files
BridgeARrefack
authored andcommitted
doc,assert: document stackStartFunction in fail
PR-URL: #13862 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 5bbae25 commit 7a2b3e2

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

doc/api/assert.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,37 +257,62 @@ property set equal to the value of the `message` parameter. If the `message`
257257
parameter is undefined, a default error message is assigned.
258258

259259
## assert.fail([message])
260-
## assert.fail(actual, expected, message, operator)
260+
## assert.fail(actual, expected[, message[, operator[, stackStartFunction]]])
261261
<!-- YAML
262262
added: v0.1.21
263263
-->
264264
* `actual` {any}
265265
* `expected` {any}
266266
* `message` {any} (default: 'Failed')
267267
* `operator` {string} (default: '!=')
268+
* `stackStartFunction` {function} (default: `assert.fail`)
268269

269270
Throws an `AssertionError`. If `message` is falsy, the error message is set as
270271
the values of `actual` and `expected` separated by the provided `operator`.
271-
Otherwise, the error message is the value of `message`.
272-
If no arguments are provided at all, a default message will be used instead.
272+
If just the two `actual` and `expected` arguments are provided, `operator` will
273+
default to `'!='`. If `message` is provided only it will be used as the error
274+
message, the other arguments will be stored as properties on the thrown object.
275+
If `stackStartFunction` is provided, all stack frames above that function will
276+
be removed from stacktrace (see [`Error.captureStackTrace`]). If no arguments
277+
are given, the default message `Failed` will be used.
273278

274279
```js
275280
const assert = require('assert');
276281

277282
assert.fail(1, 2, undefined, '>');
278-
// AssertionError: 1 > 2
283+
// AssertionError [ERR_ASSERTION]: 1 > 2
284+
285+
assert.fail(1, 2, 'fail');
286+
// AssertionError [ERR_ASSERTION]: fail
279287

280288
assert.fail(1, 2, 'whoops', '>');
281-
// AssertionError: whoops
289+
// AssertionError [ERR_ASSERTION]: whoops
290+
```
291+
292+
*Note*: Is the last two cases `actual`, `expected`, and `operator` have no
293+
influence on the error message.
294+
295+
```js
296+
assert.fail();
297+
// AssertionError [ERR_ASSERTION]: Failed
282298

283299
assert.fail('boom');
284-
// AssertionError: boom
300+
// AssertionError [ERR_ASSERTION]: boom
285301

286302
assert.fail('a', 'b');
287-
// AssertionError: 'a' != 'b'
303+
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
304+
```
288305

289-
assert.fail();
290-
// AssertionError: Failed
306+
Example use of `stackStartFunction` for truncating the exception's stacktrace:
307+
```js
308+
function suppressFrame() {
309+
assert.fail('a', 'b', undefined, '!==', suppressFrame);
310+
}
311+
suppressFrame();
312+
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
313+
// at repl:1:1
314+
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
315+
// ...
291316
```
292317

293318
## assert.ifError(value)
@@ -594,6 +619,7 @@ For more information, see
594619
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
595620

596621
[`Error`]: errors.html#errors_class_error
622+
[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt
597623
[`Map`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
598624
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
599625
[`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

0 commit comments

Comments
 (0)