Loading
Current section: Setup 5 exercises
Problem

Code Coverage

Enable and configure code coverage in Vitest with the v8 provider to review test gaps and identify untested parts of your code.

Loading exercise

Transcript

00:00 One common mistake that developers make is use terms like test coverage and code coverage interchangeably, when in fact those are two different terms that describe different things. So before we continue, let's make sure that we know the difference between the two. Test coverage describes how well your software is tested.

00:17 So for example, if you have zero tests in your app, you can say, I have a poor test coverage. When we talk about test coverage, we often mention things like the testing levels, so unit tests, integration tests, end-to-end tests, and so on. And if you happen to have all of those in your software, then you can say, I have a pretty extensive test coverage.

00:33 Code coverage, on the other hand, is a precise metric that measures the percentage of code that run in the test for that code. Let me give you an example. Take a look at this sum function here. It accepts two numbers and returns the result of adding those numbers together. So in the simple test case for this function, where we provide it with numbers 1 and 2

00:52 and expect it to return 3, we're achieving a 100% code coverage. Why? Because when we called this function this way, it executed the entirety of its implementation, which is 100%. From the definition alone, you can spot a slight problem with code coverage. It is implementation detail-centric by design,

01:10 because it measures the percentage of those implementation details that was evaluated during the test run. And we've long established that we're not going to test implementation details. This is not what automated testing is about. That being said, code coverage can still be a useful metric at your disposal if you use it right.

01:26 In this exercise, your task will be to enable and configure code coverage in VTest. And once you do, see if there are any areas in the current test suite that you can improve, and improve them.