Skip to content

Commit 94fecdf

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

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
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

test/parallel/test-assert-fail.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,75 @@
11
'use strict';
2+
23
const common = require('../common');
34
const assert = require('assert');
45

5-
// no args
6+
// No args
67
assert.throws(
78
() => { assert.fail(); },
89
common.expectsError({
910
code: 'ERR_ASSERTION',
1011
type: assert.AssertionError,
12+
operator: undefined,
13+
actual: undefined,
14+
expected: undefined,
1115
message: 'undefined undefined undefined'
1216
})
1317
);
1418

15-
// one arg = message
19+
// One arg = message
1620
assert.throws(
1721
() => { assert.fail('custom message'); },
1822
common.expectsError({
1923
code: 'ERR_ASSERTION',
2024
type: assert.AssertionError,
25+
operator: undefined,
26+
actual: undefined,
27+
expected: undefined,
2128
message: 'custom message'
2229
})
2330
);
2431

25-
// two args only, operator defaults to '!='
32+
// Two args only, operator defaults to '!='
2633
assert.throws(
2734
() => { assert.fail('first', 'second'); },
2835
common.expectsError({
2936
code: 'ERR_ASSERTION',
3037
type: assert.AssertionError,
38+
operator: '!=',
39+
actual: 'first',
40+
expected: 'second',
3141
message: '\'first\' != \'second\''
3242
})
3343
);
3444

35-
// three args
45+
// Three args
3646
assert.throws(
3747
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
3848
common.expectsError({
3949
code: 'ERR_ASSERTION',
4050
type: assert.AssertionError,
51+
operator: undefined,
52+
actual: 'ignored',
53+
expected: 'ignored',
4154
message: 'another custom message'
4255
})
4356
);
4457

45-
// no third arg (but a fourth arg)
58+
// No third arg (but a fourth arg)
4659
assert.throws(
4760
() => { assert.fail('first', 'second', undefined, 'operator'); },
4861
common.expectsError({
4962
code: 'ERR_ASSERTION',
5063
type: assert.AssertionError,
64+
operator: 'operator',
65+
actual: 'first',
66+
expected: 'second',
5167
message: '\'first\' operator \'second\''
5268
})
5369
);
70+
71+
// The stackFrameFunction should exclude the foo frame
72+
assert.throws(
73+
function foo() { assert.fail('first', 'second', 'message', '!==', foo); },
74+
(err) => !/foo/m.test(err.stack)
75+
);

0 commit comments

Comments
 (0)