@@ -257,33 +257,61 @@ 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}
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 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 ` ] ).
272
277
273
278
``` js
274
279
const assert = require (' assert' );
275
280
276
281
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
278
286
279
287
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
281
297
282
298
assert .fail (' boom' );
283
- // AssertionError: boom
299
+ // AssertionError [ERR_ASSERTION] : boom
284
300
285
301
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
+ // ...
287
315
```
288
316
289
317
## assert.ifError(value)
@@ -590,6 +618,7 @@ For more information, see
590
618
[ MDN's guide on equality comparisons and sameness] [ mdn-equality-guide ] .
591
619
592
620
[ `Error` ] : errors.html#errors_class_error
621
+ [ `Error.captureStackTrace` ] : errors.html#errors_error_capturestacktrace_targetobject_constructoropt
593
622
[ `Map` ] : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
594
623
[ `Object.is()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
595
624
[ `RegExp` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
0 commit comments