Skip to content

Commit 3cd7977

Browse files
committed
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: #18319 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 776f6cd commit 3cd7977

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

doc/api/assert.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,8 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
692692
added: v0.1.21
693693
changes:
694694
- version: REPLACEME
695-
pr-url: https://github.com/nodejs/node/pull/17581
696-
description: assert.ok() will throw a `ERR_MISSING_ARGS` error.
697-
Use assert.fail() instead.
695+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
696+
description: assert.ok() (no arguments) will now use a predefined error msg.
698697
-->
699698
* `value` {any}
700699
* `message` {any}
@@ -707,6 +706,8 @@ property set equal to the value of the `message` parameter. If the `message`
707706
parameter is `undefined`, a default error message is assigned. If the `message`
708707
parameter is an instance of an [`Error`][] then it will be thrown instead of the
709708
`AssertionError`.
709+
If no arguments are passed in at all `message` will be set to the string:
710+
"No value argument passed to assert.ok".
710711

711712
Be aware that in the `repl` the error message will be different to the one
712713
thrown in a file! See below for further details.
@@ -719,6 +720,10 @@ assert.ok(true);
719720
assert.ok(1);
720721
// OK
721722

723+
assert.ok();
724+
// throws:
725+
// "AssertionError: No value argument passed to `assert.ok`.
726+
722727
assert.ok(false, 'it\'s false');
723728
// throws "AssertionError: it's false"
724729

lib/assert.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ function getBuffer(fd, assertLine) {
137137
function innerOk(args, fn) {
138138
var [value, message] = args;
139139

140-
if (args.length === 0)
141-
throw new TypeError('ERR_MISSING_ARGS', 'value');
142-
143140
if (!value) {
144-
if (message == null) {
141+
if (args.length === 0) {
142+
message = 'No value argument passed to `assert.ok()`';
143+
} else if (message == null) {
145144
// Use the call as error message if possible.
146145
// This does not work with e.g. the repl.
147146
const err = new Error();

test/parallel/test-assert.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,18 +733,18 @@ common.expectsError(
733733
assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual);
734734
assert.equal(Object.keys(assert).length, Object.keys(a).length);
735735
assert(7);
736-
common.expectsError(
737-
() => assert(),
736+
assert.throws(
737+
() => assert(...[]),
738738
{
739-
code: 'ERR_MISSING_ARGS',
740-
type: TypeError
739+
message: 'No value argument passed to `assert.ok()`',
740+
name: 'AssertionError [ERR_ASSERTION]'
741741
}
742742
);
743-
common.expectsError(
743+
assert.throws(
744744
() => a(),
745745
{
746-
code: 'ERR_MISSING_ARGS',
747-
type: TypeError
746+
message: 'No value argument passed to `assert.ok()`',
747+
name: 'AssertionError [ERR_ASSERTION]'
748748
}
749749
);
750750

0 commit comments

Comments
 (0)