@@ -192,29 +192,47 @@ If the values are not equal, an `AssertionError` is thrown with a `message`
192
192
property set equal to the value of the ` message ` parameter. If the ` message `
193
193
parameter is undefined, a default error message is assigned.
194
194
195
- ## assert.fail(actual, expected, message, operator)
195
+ ## assert.fail(actual, expected[ , message[ , operator[ , stackStartFunction ]]] )
196
196
<!-- YAML
197
197
added: v0.1.21
198
198
-->
199
199
* ` actual ` {any}
200
200
* ` expected ` {any}
201
201
* ` message ` {any}
202
202
* ` operator ` {string}
203
+ * ` stackStartFunction ` {function} (default: ` assert.fail ` )
203
204
204
205
Throws an ` AssertionError ` . If ` message ` is falsy, the error message is set as
205
206
the values of ` actual ` and ` expected ` separated by the provided ` operator ` .
206
207
Otherwise, the error message is the value of ` message ` .
208
+ If ` stackStartFunction ` is provided, all stack frames above that function will
209
+ be removed from stacktrace (see [ ` Error.captureStackTrace ` ] ).
207
210
208
211
``` js
209
212
const assert = require (' assert' );
210
213
211
214
assert .fail (1 , 2 , undefined , ' >' );
212
215
// AssertionError: 1 > 2
213
216
217
+ assert .fail (1 , 2 , ' fail' );
218
+ // AssertionError: fail
219
+
214
220
assert .fail (1 , 2 , ' whoops' , ' >' );
215
221
// AssertionError: whoops
216
222
```
217
223
224
+ Example use of ` stackStartFunction ` for truncating the exception's stacktrace:
225
+ ``` js
226
+ function suppressFrame () {
227
+ assert .fail (' a' , ' b' , undefined , ' !==' , suppressFrame);
228
+ }
229
+ suppressFrame ();
230
+ // AssertionError: 'a' !== 'b'
231
+ // at repl:1:1
232
+ // at ContextifyScript.Script.runInThisContext (vm.js:44:33)
233
+ // ...
234
+ ```
235
+
218
236
## assert.ifError(value)
219
237
<!-- YAML
220
238
added: v0.1.97
@@ -492,5 +510,6 @@ assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
492
510
[ `assert.ok()` ] : #assert_assert_ok_value_message
493
511
[ `assert.throws()` ] : #assert_assert_throws_block_error_message
494
512
[ `Error` ] : errors.html#errors_class_error
513
+ [ `Error.captureStackTrace` ] : errors.html#errors_error_capturestacktrace_targetobject_constructoropt
495
514
[ `RegExp` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
496
515
[ `TypeError` ] : errors.html#errors_class_typeerror
0 commit comments