Last time I shared a quick tidbit about running individual unit tests. This time, I want to share a few interesting things I learned while debugging a failing unit test.
if statements can only contain boolean expressions - either a boolean variable, or some other expression which returns a boolean value, such as: greater than, less than, equal to, not equal to, etc.
Where this becomes problematic is that it's SO easy to misuse this or have subtle errors in your code. In other languages, if you put a string inside of an
The important thing to remember is that ONLY the following values evaluate to
false - ALL others evaluate to
- 0n (BigInt 0)
- "" (empty string)
- NaN (not a number)
Frankly, this list doesn't look all that bad. It kind of makes sense (don't get me started on
-0 (negative zero) - that just has danger written all over it), but having an empty string evaluate to false sort of makes sense. What doesn't make sense, however, is that an empty array
 evaluates to
true. C'mon guys - if you're going to have a very flexible/dynamic type system, at least make it consistent! Arrays and strings can both be
undefined - so why is an empty string treated differently from an empty array?
undefined) evaluate to
Anyways, as you may have gathered by now, I experienced this exact problem, where I was expecting an empty array to be
false, but it was in fact
true, and it took me running a unit test through a debugger to figure it out.
The moral of the story is, if you're unfamiliar/new to a particular language, make sure you carefully read (and re-read) the documentation, and always challenge your assumptions when debugging.