What is istanbul-lib-coverage?
The istanbul-lib-coverage package is a library for JavaScript test coverage measurement and reporting. It provides a set of APIs to instrument code, collect and aggregate coverage data, and generate reports in various formats. It's part of the Istanbul project, which is widely used for tracking code coverage in JavaScript applications.
What are istanbul-lib-coverage's main functionalities?
Creating a Coverage Map
This feature allows you to create a new coverage map. A coverage map is a data structure that holds coverage data for a set of files. This is the first step in collecting and storing coverage information.
const libCoverage = require('istanbul-lib-coverage');
const map = libCoverage.createCoverageMap();
Adding File Coverage
After creating a coverage map, you can add coverage data for individual files. This involves creating a file coverage object for each file and adding it to the coverage map. This is how you populate the coverage map with data.
const fileCoverage = libCoverage.createFileCoverage('path/to/file.js');
map.addFileCoverage(fileCoverage);
Generating Summary Reports
Once you have a populated coverage map, you can generate summary reports. These summaries provide an overview of the coverage data, including statistics like lines covered, statements covered, and so forth. This is useful for getting a quick insight into the coverage of your project.
const summary = map.getCoverageSummary();
console.log(summary.toJSON());
Other packages similar to istanbul-lib-coverage
nyc
nyc is a command-line-interface tool that wraps istanbul-lib-coverage and other Istanbul libraries to provide a simplified user experience for instrumenting code, running tests, and generating coverage reports. It's more of a complete toolset compared to the lower-level API functionality provided by istanbul-lib-coverage.
c8
c8 leverages the built-in V8 coverage tool to collect test coverage. Unlike istanbul-lib-coverage, which instruments the source code to track coverage, c8 works at the V8 engine level, providing potentially more accurate coverage metrics without the need to modify the source code. This difference in approach makes c8 a compelling alternative for projects that can benefit from native coverage collection.
jest
Jest is a popular testing framework that includes built-in coverage reporting capabilities. While istanbul-lib-coverage is focused solely on coverage collection and reporting, Jest provides a comprehensive testing solution that includes assertions, mocks, and coverage. Jest uses Istanbul under the hood for coverage reporting, making it a higher-level alternative that integrates testing and coverage.
istanbul-lib-coverage
An API that provides a read-only view of coverage information with the ability
to merge and summarize coverage info.
Supersedes object-utils
and collector
from the v0 istanbul API.
See the docs for the full API.
var libCoverage = require('istanbul-lib-coverage');
var map = libCoverage.createCoverageMap(globalCoverageVar);
var summary = libCoverage.createCoverageSummary();
map.merge(otherCoverageMap);
map.files().forEach(function(f) {
var fc = map.fileCoverageFor(f),
s = fc.toSummary();
summary.merge(s);
});
console.log('Global summary', summary);