falsy. As a quick refresher, the
falsy values are:
false null undefined 0 (the number zero) "" (an empty string)
Everything else is
truthy. This is one of those things you just have to memorize, but it's simpler if you think of the
falsy stuff as things that are false or have no value. Just watch out for zero and empty string.
I got burned by a bug today in a passing Jasmine test by not paying enough attention to the
truthy values. The code I was testing was a little function to make sure
scaleMin was smaller than
scaleMax, but if they were both set to 0, that was OK. If
scaleMin was greater than or equal to
scaleMax, we'd return an error message.
Here are the function and tests I started with. All the tests pass. Done, right?!?!
Not so much.
toBeTruthy() checks for any
truthy value, and a string with something in it is
truthy. So in the test where I'm checking that it's OK if
scaleMax are both 0, I'm getting the error string, which is
truthy. Since the error string and the value true are both
truthy, my check for
toBeTruthy() will always pass. I'm getting false positives.
This is why you are supposed to write a failing test, then a passing test to prove your code was the thing that made the test pass. I skipped that here because this code was so simple. What could go wrong? Oops!
Here's the fixed function and test code, which uses Jasmine's
toBe(true) instead of
toBeTruthy() to tease apart error strings and the value true: