
Three testers walk into a bar…and we all know that they order, one beer, zero beer and minus one beer! We also all know that they need to order zero beers because the behaviour of zero can be different from the behaviour of other numbers. We all test with zeros for this reason. Recently I have learned something more about the behaviour of zero that is helping me to test..
I am doing Typescript tutorials to help me automate tests and recently learned more about behaviour with zeros which helped me to understand the cause of a bug. I have recently learned about conditional narrowing, which can be used to check the type of a value in TypeScript. If the && operator or the || operator are used for truthiness narrowing then zero is coerced to false. The && and || operators treat zero in the same way as are NaN, “”(the empty string), 0n (the bigint version of zero), null and undefined. This means that truthiness narrowing does not treat zero as a number, it treats zero as false
A friend of mine used this knowledge and found a bug in which the user could enter zero but was not able to save the zero that they had entered. The application could not save zero because its logic said that zero was false so was not a number.
Zero is a curious thing and I am using what I have just learned about to help me test.
Additional Learning Resources:
- Conditional Narrowing
- Nullish coalescing – Nullish coalescing with the ?? operator treats zero in the same way as numbers.