Code Coverage Reports and Custom Configuration with Istanbul, Jest, and React (2025)

Code Coverage Reports and Custom Configuration with Istanbul, Jest, and React (3)

A few months ago, I met the one and only Robert “Uncle Bob” Martin (“Clean Code,” “The Clean Coder,” “Clean Architecture,” etc ). One of many questions people had for him was, “What parts on my application need to be covered with unit tests?” His response was, “Well, only the ones you want to work.”

Great response, right?

The more I write code, the more I believe that the 100% (or close to it) line-coverage threshold is not crazy but mandatory. One of the best things about good coverage is coverage reports that are green across all files.

Seeing all green makes people happy, and they tend to keep it green (i.e., adding new tests when they add new code). No one wants to be that guy that made a file red after it was green for a long time.

Code Coverage Reports and Custom Configuration with Istanbul, Jest, and React (4)

So it’s really important to have code-coverage reports and thresholds from the start. In this article, we configure Istanbul on the React application, using Jest as the test runner.

Let’s start with the React app. We will use Facebook’s Create React App template.

Create React App requires no configuration and creates a very simple React application with a single command:

npx create-react-app my-app-name

Now you have the React application. What’s even better: You have one Jest test in src/App.test.js.

That’s all you need for trying out Istanbul. If you want more data, you can add few more files and write tests for them, or you can use your existing React projects.

The good news is that Istanbul is built into Jest. That means configuration is handled directly by Jest.

The catch here is that Istanbul is not running by default when you create your app with the Create React App template.

There are two ways of running Istanbul alongside Jest when test scripts are executed. Both are really simple.

The first is running your tests with a --coverage flag. Let’s add a new npm script called test:coverage that will use Istanbul to generate coverage report after running tests:

//package.json
{
...
"scripts": {
...
// test run without coverage report
"test": "set CI=true && react-scripts test",

// run tests in watch mode
"test:watch": "react-scripts test",

// test run that generates coverage reports
"test:coverage": "set CI=true
&& react-scripts test --coverage"
}
}

This is the best way of introducing Istanbul to your app because you control whether or not reports will be generated based on what script you use.
The reason why you don’t want to always create reports is performance — generating reports on every run may significantly slow down your tests.
So when you need a coverage report, you run npm run test:coverage, but when you don't, you use npm run test, which has faster and less expensive execution.

The second way of turning on Istanbul is using the collectCoverage property that is false by default.

//package.json
{
...
"jest": {
collectCoverage: true
}
}

Doing this will make every test run collect coverage and generate reports, which is way less flexible then using the --coverage flag and can have a big performance impact.

I would suggest always setting Istanbul using the first way we discussed with the coverage flag. That way you will have a completely flexible configuration that will allow you to only pay the overhead of collecting coverage when you actually need coverage report.

Istanbul has many properties that let you customize your coverage report. Let’s take a look at the most useful or interesting ones.

Property collectCoverageFrom lets you define from what sources you want and, more importantly, don’t want to collect coverage from.

You always want to exclude the node_modules directory, some third party code, test files, and so on. Using the collectCoverageFrom property makes configuring that really easy. Excluding is done with a negation operator: “!”.

"jest": {
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/node_modules/**",
"!**/coverage/**",
"!**/serviceWorker.js",
"!**/index.js"
],
}

The property coverageThreshold will enforce minimum threshold for coverage results. You can enforce minimums for every entry Istanbul measures: branches, methods, lines, and statements. The cool thing here is that other then global thresholds, you can set them up for any separate directory and even every file.

"jest": {
"coverageThreshold": {
"global": { // global thresholds
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/App.js": { // file level thresholds
"lines": 100
}
}
}

There are many more Jest properties. You can really go deep and make configuration that’s perfect for your project.

Having coverage reports and thresholds from the start will make a big difference in the long run. It will keep your project clean, long lasting, and resilient to bugs.

Trying to include test coverage thresholds, linters, and other rules late in the project lifetime almost always ends up as a failure that no one respects.

Be smart and do it from the start.

Code Coverage Reports and Custom Configuration with Istanbul, Jest, and React (2025)

FAQs

How to get 100% coverage in Jest? ›

In order to achieve 100% test coverage for an Angular component using Jest, you should test all of the component's functionality and behavior. This includes testing the component's inputs, outputs, methods, and any other behavior that is specific to your component.

How to generate coverage report using Jest? ›

  1. 1 Install Jest and babel-jest. To use Jest, you need to install it as a dev dependency in your project. ...
  2. 2 Configure Jest in package. json. ...
  3. 3 Write and run tests. To generate code coverage reports, you need to write and run tests using Jest. ...
  4. 4 View and analyze reports. ...
  5. 5 Here's what else to consider.
Feb 17, 2024

What is the difference between Istanbul and Jest? ›

Comparison of the Tools

Istanbul: Choose Istanbul if you want a high-performance tool with detailed reporting and strong community support. Jest: Jest is a great choice if you're looking for a comprehensive testing solution that includes code coverage.

How much code coverage is enough? ›

It's impractical to dictate that every team should achieve a specific percentage of code coverage; the product owners best make such decisions with specialized domain knowledge. However, it is generally agreed in the industry that each software project should aim for a code coverage percentage of between 60% and 90%.

How to achieve 100% test coverage in react? ›

We will talk about several methods for enhancing test coverage in a JavaScript React project in this post.
  1. Make sure all new code is tested. ...
  2. Use tools for code coverage You may find out whether portions of your codebase are not being tested using a variety of methods. ...
  3. For edge cases, create tests.
Jan 6, 2023

How can I improve my Jest coverage? ›

Jest: Increase Coverage in Unit Tests
  1. Test the initial coverage with the Parent file/function. Suppose, you are having three files in your project. ...
  2. Start from the lower most file in the hierarchy. ...
  3. Write test cases even for the constants declared. ...
  4. Prefer Mocking Packages instead of a file.
Jul 8, 2023

How do I create a code coverage report? ›

To create a code coverage report, you run a build project that is configured with at least one code coverage report group in its buildspec file. CodeBuild will interpret the code coverage results and provide a code coverage report for the run.

What is Istanbul code coverage? ›

GitHub - gotwarlost/istanbul: Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests.

What is Istanbul called now? ›

On this day many years ago, Constantinople was renamed İstanbul. To celebrate, let's look at some other historic city name changes. Once the Ancient Greek city of Byzantium, when the Roman Emperor Constantine moved the capital of his Empire to the Bosporus strait, it was renamed Constantinople in his honour.

Why choose Istanbul? ›

It offers a rich mix of history, culture, and modern energy. Here are some of the reasons why Istanbul is a must-visit destination: Historic landmarks: Istanbul is home to a rich collection of historic landmarks, including the Hagia Sophia, Topkapi Palace, and the Blue Mosque.

What are the three names of Istanbul? ›

The city of Istanbul has been known by a number of different names. The most notable names besides the modern Turkish name are Byzantium, Constantinople, and Stamboul. Different names are associated with different phases of its history, with different languages, and with different portions of it.

Is 70% code coverage good? ›

Test Coverage: Test coverage is a technique where our test cases cover application code and on specific conditions those test cases are met. Minimum Test Coverage Rate: Keeping it between 60 - 70%. Optimal Test Coverage Rate: Keeping it between 70 - 80%. Overkill Test Coverage Rate: Keeping it between 80 - 100%.

Is 100% code coverage possible? ›

Code coverage is a measurement of how much of your codebase is exercised by your test suite. Having 100% coverage means that every line of code is invoked when your tests are run. This is often held up as an ideal target to aim for in testing. However, 100% code coverage can be misleading and counterproductive.

Is 80% code coverage good? ›

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.

Is it possible to have 100% coverage? ›

However, it is important to note that achieving 100% test coverage might not be possible in the literal sense. Instead, testing teams should aim at achieving maximum test coverage and ensure that there are sufficient tests to cover the maximum of code in the application.

How do I get high test coverage? ›

  1. Identify which tests to automate. Your team should develop specific guidelines for deciding what to test with automated testing tools and what to test manually. ...
  2. Choose the right testing tools. ...
  3. Select the right test coverage technique. ...
  4. Establish metrics for evaluating automated test coverage. ...
  5. Invest in test maintenance.
Dec 4, 2023

How many test cases are needed to achieve 100 statement coverage? ›

So, 2 test cases are needed to execute every line of code. And 2 test cases to execute the True and False conditions. So 2 test cases are needed for 100% Statement and Decision coverage.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6161

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.