assert: throw without args in ok

`assert.ok()` should always receive a value. Otherwise there
might be a bug or it was intended to use `assert.fail()`.

PR-URL: https://github.com/nodejs/node/pull/17581
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ron Korving <ron@ronkorving.nl>
This commit is contained in:
Ruben Bridgewater 2018-01-15 23:37:09 +01:00
parent f76ef50432
commit d07c6f9739
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
3 changed files with 22 additions and 0 deletions

View File

@ -645,6 +645,11 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
## assert.ok(value[, message]) ## assert.ok(value[, message])
<!-- YAML <!-- YAML
added: v0.1.21 added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17581
description: assert.ok() will throw a `ERR_MISSING_ARGS` error.
Use assert.fail() instead.
--> -->
* `value` {any} * `value` {any}
* `message` {any} * `message` {any}

View File

@ -133,6 +133,9 @@ 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 errors.TypeError('ERR_MISSING_ARGS', 'value');
if (!value) { if (!value) {
if (message == null) { if (message == null) {
// Use the call as error message if possible. // Use the call as error message if possible.

View File

@ -754,6 +754,20 @@ common.expectsError(
assert.equal(Object.keys(assert).length, Object.keys(a).length); assert.equal(Object.keys(assert).length, Object.keys(a).length);
/* eslint-enable no-restricted-properties */ /* eslint-enable no-restricted-properties */
assert(7); assert(7);
common.expectsError(
() => assert(),
{
code: 'ERR_MISSING_ARGS',
type: TypeError
}
);
common.expectsError(
() => a(),
{
code: 'ERR_MISSING_ARGS',
type: TypeError
}
);
// Test setting the limit to zero and that assert.strict works properly. // Test setting the limit to zero and that assert.strict works properly.
const tmpLimit = Error.stackTraceLimit; const tmpLimit = Error.stackTraceLimit;