Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come Node.js Assert doesn't have a notOk() method?

I am learning about testing in Node.js using mocha and the assert module. assert has these types of methods:

assert.equal();
assert.deepEqual();
assert.deepStrict();
assert.strict();
assert.ok(); // Is the value true?

And then there are some opposites:

assert.notEqual();
assert.notDeepEqual();
assert.notDeepStrict();
assert.notStrict();

But there one missing... Why is there not a notOk() method for testing if the resulting value is false or not?

This got me thinking that maybe there is something fundamental I am missing about unit testing in general, in that maybe I should only ever be testing if values are true and never false...

For now, I have simply been doing this to test for falsey statements:

assert.ok(!myValue);

Is that how you are supposed to do it? Again, why isn't there just a notOk() method since all the other methods have a not version?

like image 980
Jake Wilson Avatar asked Nov 10 '16 04:11

Jake Wilson


People also ask

What is assert module in node JS?

The assert module provides a way of testing expressions. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated. This module was built to be used internally by Node.

What is assert deepEqual?

The assert. deepEqual() method tests if two objects, and their child objects, are equal, using the == operator. If the two objects are not equal, an assertion failure is being caused, and the program is terminated. To compare the objects using the === operator, use the assert.

Which assert module method is usually used to test the error first argument in callbacks?

The ifError method is commonly used to test error arguments in callbacks.


1 Answers

If you look at the docs, it notes that .ok(value, message) is equivalent to assert.equal(!!value, true, message). This appears to simply be a convenience function.

It should be noted that while the function is called ok(), that does not mean that it is expecting a true value from your code. It is expecting a statement to which you want to test the truthiness of. So while your usage of !myValue is valid, I think it would be more appropriate to write:

assert.ok(myValue === false);

The above makes sense semantically, as you are still testing the truthiness of your expression, it just so happens that you are expecting it to be false. To have included a notOk() function would imply some ambiguous meaning. Not OK implies failure, and so the failure of a notOk function might have just seemed a little convoluted.

As for the other methods having their not equivalents, I think the language is still meaningful. Saying notEqual is implying that you are testing specifically for an inequality, while notOk doesn't really make sense. Personally I would just leave out using ok() all together, as ok in and of itself it quite ambiguous anyway.

If you really feel the need, you could simply write your own convenience function too.


I think it is also worth mentioning that this is the source:

// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, !!guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.

function ok(value, message) {
    if (!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
like image 133
Matt Way Avatar answered Sep 30 '22 03:09

Matt Way