DEV Community

Michael Di Prisco
Michael Di Prisco

Posted on • Originally published at cadienvan.github.io

2 1 1

The truth about test coverage

A powerful truth.

Look at the following, simple and straightforward code:

function sum(a, b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Now, let's write some tests for it:

test('sum', () => {
  expect(sum(1, 2)).toBe(3);
  expect(sum(2, 3)).toBe(5);
  expect(sum(3, 4)).toBe(7);
  expect(sum(4, 5)).toBe(9);
});
Enter fullscreen mode Exit fullscreen mode

We got 100% coverage, right? Well, yes, we do, in fact we could say we got 400% coverage as all the code is fully tested 4 times, but do we?

The truth is that we don't. We are testing the function with a limited set of inputs, and we are not considering edge cases, nor we are testing the function with invalid inputs.

Consider the following:

sum(1, '2');
sum(1, null);
sum(1, undefined);
Enter fullscreen mode Exit fullscreen mode

What would happen in such a scenario? Would the function throw an error? Would it return a value? Would it break our application?

Be aware of the test coverage trap.

Test coverage is a powerful tool, but it's not the ultimate solution. It's a metric that can help you understand how much of your code is being tested, but it doesn't tell you how well it's being tested.

Test coverage can help you with quantity, but it can do little with quality. It's up to you to write good tests, to consider edge cases, to test your code with invalid inputs, and to make sure that your tests are meaningful and valuable.

Conclusion

This was a pretty short article, I admit, still, I hope it was useful to you as a reminder of the importance of writing good tests. Remember, test coverage is a tool, not a goal. It's up to you to make the most out of it.

Ciao,

Michael.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video