Home > Web Front-end > JS Tutorial > body text

The truth about test coverage

Mary-Kate Olsen
Release: 2024-09-28 16:23:01
Original
377 people have browsed it

The truth about test coverage

A powerful truth.

Look at the following, simple and straightforward code:

function sum(a, b) {
  return a + b;
}
Copy after login

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);
});
Copy after login

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);
Copy after login

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.

The above is the detailed content of The truth about test coverage. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!