DEV Community

Discussion on: What’s the point of unit tests?

Collapse
 
lexlohr profile image
Alex Lohr • Edited

Unit tests can be wrong on a few levels:

  • testing the test/mocks rather than the unit
  • testing the obvious
  • testing too much at once

Imagine you have a react component. You mock three event props and call them from the vDOM representation. Congratulations, you tested that your mocks work, appear in the obvious places, and not one of them is failing. You also managed to make all three aforementioned mistakes at once.

First, calling the mocks will merely test that they work, but they are neither part of your component nor of your actual application, so you just wasted time. You also tested the position of props in the vDOM representation, which should be a) obvious and b) could even be subject to change in which case your tests show a false positive. Lastly, you tested all events at once so if the test would fail, it'll be more difficult to know which event triggered the failure.

Try to make the most out of the least amount of tests. You want to test uncertainties. Also, your table is missing integration and e2e tests.

Collapse
 
marcinwosinek profile image
Marcin Wosinek

Thanks for your comment! This is a good topic for an article - how to write unit tests, and how to get the most value our of them.