Skip to content

Commit 78f0c2a

Browse files
BridgeARaddaleax
authored andcommittedJul 24, 2017
doc,assert: document stackStartFunction in fail
PR-URL: #13862 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Backport-PR-URL: #14428 Backport-Reviewed-By: Anna Henningsen <[email protected]>
1 parent fa134dd commit 78f0c2a

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed
 

‎doc/api/assert.md

+35-6
Original file line numberDiff line numberDiff line change
@@ -257,33 +257,61 @@ 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}
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 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`]).
272277

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

276281
assert.fail(1, 2, undefined, '>');
277-
// AssertionError: 1 > 2
282+
// AssertionError [ERR_ASSERTION]: 1 > 2
283+
284+
assert.fail(1, 2, 'fail');
285+
// AssertionError [ERR_ASSERTION]: fail
278286

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

282298
assert.fail('boom');
283-
// AssertionError: boom
299+
// AssertionError [ERR_ASSERTION]: boom
284300

285301
assert.fail('a', 'b');
286-
// AssertionError: 'a' != 'b'
302+
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
303+
```
304+
305+
Example use of `stackStartFunction` for truncating the exception's stacktrace:
306+
```js
307+
function suppressFrame() {
308+
assert.fail('a', 'b', undefined, '!==', suppressFrame);
309+
}
310+
suppressFrame();
311+
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
312+
// at repl:1:1
313+
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
314+
// ...
287315
```
288316

289317
## assert.ifError(value)
@@ -590,6 +618,7 @@ For more information, see
590618
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
591619

592620
[`Error`]: errors.html#errors_class_error
621+
[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt
593622
[`Map`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
594623
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
595624
[`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

0 commit comments

Comments
 (0)
Please sign in to comment.