Tests That Help You Find Defects Faster

20 min
Beginner - Intermediate

TestJS Summit 2021

Abstract

This talk is about common mistakes people make when writing tests. Mixing multiple concerns inside tests is tempting because it can feel like painting the whole picture. However, it obfuscates the root cause when a test fails. Setup methods are great, but when developers are too focused on keeping their tests DRY, they can easily cause test interdependence. Therefore, some principles we have learned to build our software we need to unlearn when it comes to testing. The talk highlights more aspects like bloated tests, making it hard to figure out what they are about, and proper assertions to get better error messages. Especially if you don't work with TDD, it can be easy to come up with a test that looks good but stands in your way when it fails. This talk focuses on the four scenarios I outlined above, explains why it makes sense to think about them, and provides actionable suggestions on improving tests.


Outline

Multiple Use-Cases

We'll learn how tests that cover multiple use cases can make it harder to figure out the root cause of an issue when they start to fail. Once we understand what use cases are, we look at how we can restructure our tests to be concise and lead us towards issues.

Generic Assertions

Even when tests are concise, it can be hard to understand why they fail when using too generic assertions. We'll look at common mistakes and learn to use assertions that help us express our intent when writing tests.

Bloated Tests

Once we reach the test implementation, we need to make sure that our test code is concise. Concise tests help others to understand what they test and how. We learn that bloating tests with too much information and unnecessary instructions can make understanding how they work a challenging task. We'll also look at improvements we can make and how they fit into the red-green-refactor cycle.

Accidental Coupling

The last section takes improving our tests to the extreme. We learn how keeping our tests completely DRY can introduce unwanted coupling between them. We'll learn how some repetition inside test cases is good when it helps to keep them isolated.