Skip to content

Commit

Permalink
assert: fix assert.fail with zero arguments
Browse files Browse the repository at this point in the history
PR-URL: #13974
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and refack committed Jul 2, 2017
1 parent 7022260 commit fc46363
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,20 @@ If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned.

## assert.fail(message)
## assert.fail([message])
## assert.fail(actual, expected, message, operator)
<!-- YAML
added: v0.1.21
-->
* `actual` {any}
* `expected` {any}
* `message` {any}
* `message` {any} (default: 'Failed')
* `operator` {string} (default: '!=')

Throws an `AssertionError`. If `message` is falsy, the error message is set as
the values of `actual` and `expected` separated by the provided `operator`.
Otherwise, the error message is the value of `message`.
If no arguments are provided at all, a default message will be used instead.

```js
const assert = require('assert');
Expand All @@ -284,6 +285,9 @@ assert.fail('boom');

assert.fail('a', 'b');
// AssertionError: 'a' != 'b'

assert.fail();
// AssertionError: Failed
```

## assert.ifError(value)
Expand Down
7 changes: 6 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ const assert = module.exports = ok;
// display purposes.

function fail(actual, expected, message, operator, stackStartFunction) {
if (arguments.length === 1)
if (arguments.length === 0) {
message = 'Failed';
}
if (arguments.length === 1) {
message = actual;
actual = undefined;
}
if (arguments.length === 2)
operator = '!=';
const errors = lazyErrors();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ assert.throws(
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'undefined undefined undefined'
message: 'Failed'
})
);

Expand Down

0 comments on commit fc46363

Please sign in to comment.