DEV Community

Discussion on: Unit Tests and Snapshots are Fake News

Collapse
 
almenon profile image
Almenon

"When writing a unit test I end up stubbing out an entirely fake environment for a given function to run in. This fake environment yields the results I expect every time because I mocked it to do so. So the test passes or fails as expected."

If your unit tests always pass it either means that:
A. you are literally a perfect programmer. Your code works 💯% of the time. "bugs" are a strange foreign concept.
B. Your unit tests are not thorough enough. You are not testing enough edge cases.

Given that A. is impossible as you are a mere mortal1 it must be B.
Maybe you are a really good programmer, but even the greatest makes mistakes. Unit tests help catch those mistakes by double-checking that your assumptions are correct.

Bugs usually happen in rare edge cases. Take a bit of time to think of those edge cases. For example, what if a user has two last names? Three? What if you get a negative number? A float? What if the timezone is different? What if it's a leap year? There's all sorts of possibilities with some imagination. By testing those scenarios you will make CERTAIN that they pass and those tests will remain to validate those edge cases for future programmers in the same codebase.

Also note that running unit tests in CI and preventing PR merge until test pass will make sure that nobody ignores broken tests.


  1. If you're not a mortal I apologize. Please don't smite me. 🙏