So you don’t write tests, eh? This is what I think about it.

In a lot of projects I work in, the people just don’t write tests, especially in the frontend. And everytime the reasons are as follows:

  • It takes so much time!
  • It is much more code than the code I need to write for the app!
  • We don’t need tests (because of reasons)!

And everytime the consequences are:

  • SO MUCH SENSELESS BUGS!!! (😭😭😭)

Some examples

1. The following line of code was added, without tests:

function doStuff(metaInfo) {
  // Context: The developer introduced a new flag which should be `true` by default
  metaInfo.sendPrintjob = metaInfo.sendPrintjob || true;
  ...
}

Question: what’s the value of metaInfo.sendPrintjob if metaInfo.sendPrintjob === false ?

Yeah, it’s true 😞. Bug in production. A simple test most probably got that covered…

You could argue that the developer was stupid. But we all make mistakes. Writing tests allows us to execute that code VERY FAST and get IMMEDIATE RESPONSE. Without reloading the page, adding console.logs, clicking through X screens just to get there and manually test it, maybe only once, like in this case with metaInfo.sendPrintjob = true

2. Following code was changed:

  // before:
  headers: options.headers.toJSON(),
  // after:
  headers: options.headers // Don't call toJSON

This change led to a bug in production. I had to fix the bug ticket in a new project. And it did cost me hours of debugging to find it. As there were no tests at all, this means starting the app, clicking through lots of pages, and setting breakpoints just to get to the point I could reproduce… (this was a big codebase and I was new to the project)

Nevermind the frustation and lost of motivation on that day… But hey, thanks to the comment Don't call toJSON!

If there was a simple test which expects that toJSON has to be called, it wouldn’t be a problem at all..

Some arguments for testing

  1. Tests are (living) documentation (= specification) - this means they are executable code documentation - see BDD on wikipedia
    -> Do you always update the code docs when changing code?! (If your answer is yes: 😂)

  2. Time! If you have no automated tests you need to manually test the whole app if you are making changes because you cannot know which parts you broke ;)

  3. Refactoring: How can you refactor and clean up some code without automated tests? Manually test everthing? Good luck!

  4. After updating dependencies tests can show you if something broke.

  5. Better Code-Design - if you are writing tests, you think more about the structure of your code resulting in more clean and readable code

  6. Fast feedback of code execution: You can run code without having to run the whole app. This can even speed up the development of a certain feature, especially UI related code. Who wants to click through 5 pages just to visit the part of the app you are working on?

  7. Less bugs will go to production because with tests they are detected much earlier.

  8. I hate to manually test my apps (of course I do it) but it’s just boring and error prone.

  9. Writing tests will give you more confidence about your code.

  10. 🤺 …and yes you will have more test-code that production-code, but:

So, always remember: