Node: assert: should assert.ok(0) fail ?

Created on 12 Jan 2017  路  2Comments  路  Source: nodejs/node

  • Version: v6.9.1
  • Platform: linux

Hey guys,

Recently I got a couple of tests failing on my project, I was doing an assertion like:

var response = {
  deposit: true,
  amount: 0
}

assert.ok(response.amount); // just wanted to make sure the balance field was set , regardless of the value

But it was failing because assert.ok(0) throws, ended up doing

assert.ok(response.amount != undefined);

Im pretty sure its because of the underlying v8 javascript interpretation which gives:

> 0 == true
false
> 1 == true
true
> 2 == true
false

Thus all the following works:

assert.ok(1);
assert.ok(10);
assert.ok(3);

expect for

assert.ok(0)

I wonder if .ok() should treat all numbers equally, since its treating 0 as false instead of a number value ? is this something .ok() should handle ? makes no sense to pass with all other numbers expect 0.

Thanks !

assert question

Most helpful comment

As documentation says:
assert.ok(value[, message])

It is equivalent to assert.equal(!!value, true, message).

So your case makes sense.

All 2 comments

Unfortunately we're pretty much stuck with the current behavior of the assert module. assert.ok() has always failed on falsy values (e.g. assert.ok('')) would fail also. Changing or special casing that behavior for numbers would cause too much breakage. Also, the assert module API is considered to be locked and the CTC has consistently held the position of not making any changes to the API.

As documentation says:
assert.ok(value[, message])

It is equivalent to assert.equal(!!value, true, message).

So your case makes sense.

Was this page helpful?
0 / 5 - 0 ratings