Skip to content

Commit d07c6f9

Browse files
committed
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: #17581 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ron Korving <ron@ronkorving.nl>
1 parent f76ef50 commit d07c6f9

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/api/assert.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
645645
## assert.ok(value[, message])
646646
<!-- YAML
647647
added: v0.1.21
648+
changes:
649+
- version: REPLACEME
650+
pr-url: https://github.com/nodejs/node/pull/17581
651+
description: assert.ok() will throw a `ERR_MISSING_ARGS` error.
652+
Use assert.fail() instead.
648653
-->
649654
* `value` {any}
650655
* `message` {any}

lib/assert.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ function getBuffer(fd, assertLine) {
133133
function innerOk(args, fn) {
134134
var [value, message] = args;
135135

136+
if (args.length === 0)
137+
throw new errors.TypeError('ERR_MISSING_ARGS', 'value');
138+
136139
if (!value) {
137140
if (message == null) {
138141
// Use the call as error message if possible.

test/parallel/test-assert.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,20 @@ common.expectsError(
754754
assert.equal(Object.keys(assert).length, Object.keys(a).length);
755755
/* eslint-enable no-restricted-properties */
756756
assert(7);
757+
common.expectsError(
758+
() => assert(),
759+
{
760+
code: 'ERR_MISSING_ARGS',
761+
type: TypeError
762+
}
763+
);
764+
common.expectsError(
765+
() => a(),
766+
{
767+
code: 'ERR_MISSING_ARGS',
768+
type: TypeError
769+
}
770+
);
757771

758772
// Test setting the limit to zero and that assert.strict works properly.
759773
const tmpLimit = Error.stackTraceLimit;

0 commit comments

Comments
 (0)