assert: use a default message in assert
In case no arguments are passed to `assert.ok` it should just use a default message. Otherwise `assert.ok` can not be used as a callback. PR-URL: https://github.com/nodejs/node/pull/18319 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
parent
776f6cdfc4
commit
3cd7977a42
@ -692,9 +692,8 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
|
|||||||
added: v0.1.21
|
added: v0.1.21
|
||||||
changes:
|
changes:
|
||||||
- version: REPLACEME
|
- version: REPLACEME
|
||||||
pr-url: https://github.com/nodejs/node/pull/17581
|
pr-url: https://github.com/nodejs/node/pull/REPLACEME
|
||||||
description: assert.ok() will throw a `ERR_MISSING_ARGS` error.
|
description: assert.ok() (no arguments) will now use a predefined error msg.
|
||||||
Use assert.fail() instead.
|
|
||||||
-->
|
-->
|
||||||
* `value` {any}
|
* `value` {any}
|
||||||
* `message` {any}
|
* `message` {any}
|
||||||
@ -707,6 +706,8 @@ property set equal to the value of the `message` parameter. If the `message`
|
|||||||
parameter is `undefined`, a default error message is assigned. If the `message`
|
parameter is `undefined`, a default error message is assigned. If the `message`
|
||||||
parameter is an instance of an [`Error`][] then it will be thrown instead of the
|
parameter is an instance of an [`Error`][] then it will be thrown instead of the
|
||||||
`AssertionError`.
|
`AssertionError`.
|
||||||
|
If no arguments are passed in at all `message` will be set to the string:
|
||||||
|
"No value argument passed to assert.ok".
|
||||||
|
|
||||||
Be aware that in the `repl` the error message will be different to the one
|
Be aware that in the `repl` the error message will be different to the one
|
||||||
thrown in a file! See below for further details.
|
thrown in a file! See below for further details.
|
||||||
@ -719,6 +720,10 @@ assert.ok(true);
|
|||||||
assert.ok(1);
|
assert.ok(1);
|
||||||
// OK
|
// OK
|
||||||
|
|
||||||
|
assert.ok();
|
||||||
|
// throws:
|
||||||
|
// "AssertionError: No value argument passed to `assert.ok`.
|
||||||
|
|
||||||
assert.ok(false, 'it\'s false');
|
assert.ok(false, 'it\'s false');
|
||||||
// throws "AssertionError: it's false"
|
// throws "AssertionError: it's false"
|
||||||
|
|
||||||
|
@ -137,11 +137,10 @@ function getBuffer(fd, assertLine) {
|
|||||||
function innerOk(args, fn) {
|
function innerOk(args, fn) {
|
||||||
var [value, message] = args;
|
var [value, message] = args;
|
||||||
|
|
||||||
if (args.length === 0)
|
|
||||||
throw new TypeError('ERR_MISSING_ARGS', 'value');
|
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
if (message == null) {
|
if (args.length === 0) {
|
||||||
|
message = 'No value argument passed to `assert.ok()`';
|
||||||
|
} else if (message == null) {
|
||||||
// Use the call as error message if possible.
|
// Use the call as error message if possible.
|
||||||
// This does not work with e.g. the repl.
|
// This does not work with e.g. the repl.
|
||||||
const err = new Error();
|
const err = new Error();
|
||||||
|
@ -733,18 +733,18 @@ common.expectsError(
|
|||||||
assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual);
|
assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual);
|
||||||
assert.equal(Object.keys(assert).length, Object.keys(a).length);
|
assert.equal(Object.keys(assert).length, Object.keys(a).length);
|
||||||
assert(7);
|
assert(7);
|
||||||
common.expectsError(
|
assert.throws(
|
||||||
() => assert(),
|
() => assert(...[]),
|
||||||
{
|
{
|
||||||
code: 'ERR_MISSING_ARGS',
|
message: 'No value argument passed to `assert.ok()`',
|
||||||
type: TypeError
|
name: 'AssertionError [ERR_ASSERTION]'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
common.expectsError(
|
assert.throws(
|
||||||
() => a(),
|
() => a(),
|
||||||
{
|
{
|
||||||
code: 'ERR_MISSING_ARGS',
|
message: 'No value argument passed to `assert.ok()`',
|
||||||
type: TypeError
|
name: 'AssertionError [ERR_ASSERTION]'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user