assert: fix assert.fail with zero arguments
PR-URL: https://github.com/nodejs/node/pull/13974 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
7022260b55
commit
fc463639fa
@ -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`
|
property set equal to the value of the `message` parameter. If the `message`
|
||||||
parameter is undefined, a default error message is assigned.
|
parameter is undefined, a default error message is assigned.
|
||||||
|
|
||||||
## assert.fail(message)
|
## assert.fail([message])
|
||||||
## assert.fail(actual, expected, message, operator)
|
## assert.fail(actual, expected, message, operator)
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.1.21
|
added: v0.1.21
|
||||||
-->
|
-->
|
||||||
* `actual` {any}
|
* `actual` {any}
|
||||||
* `expected` {any}
|
* `expected` {any}
|
||||||
* `message` {any}
|
* `message` {any} (default: 'Failed')
|
||||||
* `operator` {string} (default: '!=')
|
* `operator` {string} (default: '!=')
|
||||||
|
|
||||||
Throws an `AssertionError`. If `message` is falsy, the error message is set as
|
Throws an `AssertionError`. If `message` is falsy, the error message is set as
|
||||||
the values of `actual` and `expected` separated by the provided `operator`.
|
the values of `actual` and `expected` separated by the provided `operator`.
|
||||||
Otherwise, the error message is the value of `message`.
|
Otherwise, the error message is the value of `message`.
|
||||||
|
If no arguments are provided at all, a default message will be used instead.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
@ -284,6 +285,9 @@ assert.fail('boom');
|
|||||||
|
|
||||||
assert.fail('a', 'b');
|
assert.fail('a', 'b');
|
||||||
// AssertionError: 'a' != 'b'
|
// AssertionError: 'a' != 'b'
|
||||||
|
|
||||||
|
assert.fail();
|
||||||
|
// AssertionError: Failed
|
||||||
```
|
```
|
||||||
|
|
||||||
## assert.ifError(value)
|
## assert.ifError(value)
|
||||||
|
@ -52,8 +52,13 @@ const assert = module.exports = ok;
|
|||||||
// display purposes.
|
// display purposes.
|
||||||
|
|
||||||
function fail(actual, expected, message, operator, stackStartFunction) {
|
function fail(actual, expected, message, operator, stackStartFunction) {
|
||||||
if (arguments.length === 1)
|
if (arguments.length === 0) {
|
||||||
|
message = 'Failed';
|
||||||
|
}
|
||||||
|
if (arguments.length === 1) {
|
||||||
message = actual;
|
message = actual;
|
||||||
|
actual = undefined;
|
||||||
|
}
|
||||||
if (arguments.length === 2)
|
if (arguments.length === 2)
|
||||||
operator = '!=';
|
operator = '!=';
|
||||||
const errors = lazyErrors();
|
const errors = lazyErrors();
|
||||||
|
@ -8,7 +8,7 @@ assert.throws(
|
|||||||
common.expectsError({
|
common.expectsError({
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: 'undefined undefined undefined'
|
message: 'Failed'
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user