testez
testez copied to clipboard
Lack of easy way to assert that a value is true or false or truthy or falsy
The current way to check if a value is true is to use something like
expect(value).to.equal(true)
and to check if a value is false
expect(value).to.equal(false)
which is somewhat clunky, and can even lead to bugs, as it is tempting to write something like
expect(value).to.be.ok()
when value is a boolean which we expect to be either true or false, and we want the test to fail when value is false. Furthermore, the source code for ok()
even states that ok()
asserts that the value is truthy, when it really only checks that it is not nil.
The existence of functions like truthy()
and falsy()
could help to solve these problems.
What cases do you want to check for a value to be truthy where checking for non-nil isn't adequate?
One specific case that spawned this discussion was
expect(deepEqual(value1, value2)).to.equal(true)
Initially, I had written
expect(deepEqual(value1, value2)).to.be.ok()
not being aware that ok() only checks for being non-nil and not for being falsy, so my tests passed when they should have failed.