Unit Testing Principles
olano.dev
olano.dev
What often misses from such articles though is an example of how to get there. Nobody sets out to write bad code or brittle tests. So following along a codebase that used to suck and is slowly getting better would be great! Though I understand it's a much more involved exercise, I'm not holding it against the author.
Hermetic = could run just fine on their own if run on a freshly installed OS that is cut off from the internet.
The first tests you build this way will be extraordinarily expensive (faking databases & http calls is fiddly), but they pay enormous dividends.
Once you have a large enough body of these and youve refactored some clean interfaces underneath, you can start writing future tests against those.
Otherwise I agree with you.
Which one you should do depends on how complex your interactions with the DB are.
Some apps (e.g. CRUD) have half of their business logic encoded in DB queries in which case faking the calls is a bad idea.
Others only do, like, 2 simple queries. In this case there's no point running an actual database outside of a couple of E2E tests.
If you’re really struggling to test a piece of code, sometimes it’s useful to stash all the changes and start with the tests. That’s a very good sign you’ve lost your way, and you can reflect later on whether it’s a pattern or just a bad day.
Pain is information. If you make use of the information then it redeems itself. If you don’t then it’s just needless suffering.