Are Unit Tests Really Just Garbage? 💩😳
Tags: webdev, testing, programming, vue
Published: 2023-11-01 16:19:53

I always assumed that unit tests are just part of the job. They are just there, and it's a no-brainer to always test every new piece of code that you add to the codebase.

Until I recently stumbled upon a guy who leads an engineering team with a radically different approach. They refused to write even a single automated test for their entire production application.

You might say, "Well, this guy is just fooling around with his 10 users."

But when I dug a little deeper into the discussion, I found that there are teams at Facebook, Twitch, and Netflix that followed a similar strategy.

So what's that all about? Should we all just stop unit testing from now on?

## ✅ Supposed Benefits of Unit Tests
You can probably find more, but these benefits are mentioned over and over again:

Unit tests help you to:

- find bugs early on before you commit them to production,
- catch bugs when your code changes,
- improve your code design,
- document how your system works.

If we assume you are not writing garbage unit tests, these benefits are all reasonable. But they come at a cost.

## 💰 The Cost of Unit Testing

> Unit tests are expensive to set up and maintain.

The question is not whether unit tests are beneficial but rather if these benefits are worth it.

Developers constantly need to think about how to design their tests in a meaningful way, how to mock stuff, how to get more code coverage, and whether they covered all potential edge cases.

On top of that, when parts of the code change, they might have to refactor dozens of tests, no matter how well they were designed in the first place.

And all of this can lead to engineers becoming very shy to touch existing code that has been cemented in place by excessive unit testing. They tend to avoid rethinking existing solutions, trying out new things, and being bold in general.

## 🦧 What can you do instead?

**1. Use a strong toolset**
You can catch a lot of stuff in advance with a strong toolset. Code reviews, linters, strict type checking, and some solid coding guidelines provide a good basis to produce a robust product.

**2. Manual testing**
If you do need to test something, test it manually by hand. Yes, you will repeat yourself, but you are probably closer to the production environment and can easily test scenarios that might not even be testable automatically.

**3. Rely on users to find bugs instead of engineers guessing them**
It's likely you are already addressing the obvious stuff. But most of the bugs you'll face come from edge cases that you couldn't have guessed in your wildest dreams. If you accept that code will inevitably break in production, your focus will shift more towards how to fix it fast.

**4. Add safety nets**
Speaking of fast fixes. Focus your effort on creating safety nets to detect and address bugs as quickly as possible. These include gradual rollouts of new features, using good bug and performance monitoring tools, and feature flags to quickly roll back in case anything fails.

**5. Write stuff down**
If you run into weird edge cases, leave a quick comment in the code to explain why stuff is in there and discuss it with your teammates. That way, you can remember everything when trying to refactor pieces of your code.

**6. Code is written to be rewritten**
Establish that in your team. You will never get your code right on the first try. Encourage frequent improvement and don't let anyone shy away from touching someone else's code.

**7. Work on your code, not on the supporting structures**
If your code is fragile and keeps breaking when you add or modify parts of it, focus your energy on making it more robust instead of making the tests around it more complex.

## 🤔 So, should you ditch your tests?

There are cases where the cost of a single failure is simply unacceptable. Take banking, for example, or critical infrastructure. These systems need to deliver 100% of the time, and it's just not worth messing with that.

Also, at a certain scale of a company, unit tests will for sure be worth it. When there are too many people involved and things keep constantly breaking.

But this point comes later than you probably think. Netflix apparently went to 100M users without a robust testing suite in place ([Ex Netflix engineer on unit testing](https://www.youtube.com/watch?v=pvBHyip4peo&t=1545s)).

So if you are a startup or medium-sized company and your priority is moving fast, then you can probably get away without unit testing for longer than you think. And going even further, it might even benefit your company because you can focus on setting up processes to fix bugs faster.

What do you think about unit testing?

_EDIT:
Going without automated tests is not an excuse to go wild and crazy with your code. The time you gain needs to be invested into alternative failsafe mechanisms. It's like driving a sports car in race mode to make progress fast, but you better know what you are doing. It requires you to be always alert and you need to feel comfortable with running into some road bumps along the way._

## Side notes:
If you want to dive deeper, check out this interesting discussion on the topic:
https://www.youtube.com/watch?v=pvBHyip4peo

Need a helping hand on your Vue/Nuxt project? Reach out to me through [https://nuxt.wimadev.de](https://nuxt.wimadev.de?utm_source=ditch-unit-testing) (We do tests)