Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: use a default message in assert #18319

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,8 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
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.
pr-url: https://github.com/nodejs/node/pull/REPLACEME
description: assert.ok() (no arguments) will now use a predefined error msg.
-->
* `value` {any}
* `message` {any}
Expand All @@ -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 an instance of an [`Error`][] then it will be thrown instead of the
`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
thrown in a file! See below for further details.
Expand All @@ -719,6 +720,10 @@ assert.ok(true);
assert.ok(1);
// OK

assert.ok();
// throws:
// "AssertionError: No value argument passed to `assert.ok`.

assert.ok(false, 'it\'s false');
// throws "AssertionError: it's false"

Expand Down
7 changes: 3 additions & 4 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ function getBuffer(fd, assertLine) {
function innerOk(args, fn) {
var [value, message] = args;

if (args.length === 0)
throw new TypeError('ERR_MISSING_ARGS', '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.
// This does not work with e.g. the repl.
const err = new Error();
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,18 +733,18 @@ common.expectsError(
assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual);
assert.equal(Object.keys(assert).length, Object.keys(a).length);
assert(7);
common.expectsError(
() => assert(),
assert.throws(
() => assert(...[]),
{
code: 'ERR_MISSING_ARGS',
type: TypeError
message: 'No value argument passed to `assert.ok()`',
name: 'AssertionError [ERR_ASSERTION]'
}
);
common.expectsError(
assert.throws(
() => a(),
{
code: 'ERR_MISSING_ARGS',
type: TypeError
message: 'No value argument passed to `assert.ok()`',
name: 'AssertionError [ERR_ASSERTION]'
}
);

Expand Down