Skip to content
Small example of how code coverage highlights insufficient unit testing
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
CodeCoverageSummary.png
CodeCoverageTests.png
Coverage.png
LICENSE
README.md
example.js
example.test.js
jest.config.js
package-lock.json
package.json

README.md

Code coverage rocks

Small example of how code coverage highlights insufficient unit testing and shows exactly which parts of your code need to be refactored.

Installation

The repo was created with Node.js version 10.15.1, the latest LTS version should be sufficient. Just run npm install to prepare the project. The code provided here is not intended to do anything, it is just an example, so run the test and code coverage using

npm test

The coverage results are output to the coverage folder and you can see the html summary report at coverage/lcov-report/index.html which should open locally in your browser.

Results

The code example is contrived and not something that would pass a code review! However the code looks reasonable at first glance (if you squint).

The test results look sensible enough

If we pull in the coverage report too, we immediately see there is a problem

If we look at the coverage report on the code itself the problems stick out like a sore thumb!

Now we know exactly where the tests and the code are not up to standard - time to refactor and get our gold-plated 100% coverage.

You can’t perform that action at this time.