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?
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.
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.
The ifError method is commonly used to test error arguments in callbacks.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With