testez icon indicating copy to clipboard operation
testez copied to clipboard

Lack of easy way to assert that a value is true or false or truthy or falsy

Open christopherhafke opened this issue 5 years ago • 2 comments

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.

christopherhafke avatar Jul 17 '19 20:07 christopherhafke

What cases do you want to check for a value to be truthy where checking for non-nil isn't adequate?

LPGhatguy avatar Jul 17 '19 22:07 LPGhatguy

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.

christopherhafke avatar Jul 17 '19 22:07 christopherhafke