@@ -257,37 +257,62 @@ property set equal to the value of the `message` parameter. If the `message`
257
257
parameter is undefined, a default error message is assigned.
258
258
259
259
## assert.fail([ message] )
260
- ## assert.fail(actual, expected, message, operator)
260
+ ## assert.fail(actual, expected[ , message[ , operator[ , stackStartFunction ]]] )
261
261
<!-- YAML
262
262
added: v0.1.21
263
263
-->
264
264
* ` actual ` {any}
265
265
* ` expected ` {any}
266
266
* ` message ` {any} (default: 'Failed')
267
267
* ` operator ` {string} (default: '!=')
268
+ * ` stackStartFunction ` {function} (default: ` assert.fail ` )
268
269
269
270
Throws an ` AssertionError ` . If ` message ` is falsy, the error message is set as
270
271
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.
273
278
274
279
``` js
275
280
const assert = require (' assert' );
276
281
277
282
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
279
287
280
288
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
282
298
283
299
assert .fail (' boom' );
284
- // AssertionError: boom
300
+ // AssertionError [ERR_ASSERTION] : boom
285
301
286
302
assert .fail (' a' , ' b' );
287
- // AssertionError: 'a' != 'b'
303
+ // AssertionError [ERR_ASSERTION]: 'a' != 'b'
304
+ ```
288
305
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
+ // ...
291
316
```
292
317
293
318
## assert.ifError(value)
@@ -594,6 +619,7 @@ For more information, see
594
619
[ MDN's guide on equality comparisons and sameness] [ mdn-equality-guide ] .
595
620
596
621
[ `Error` ] : errors.html#errors_class_error
622
+ [ `Error.captureStackTrace` ] : errors.html#errors_error_capturestacktrace_targetobject_constructoropt
597
623
[ `Map` ] : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
598
624
[ `Object.is()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
599
625
[ `RegExp` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
0 commit comments