What is karma-coverage?
The karma-coverage npm package is a plugin for the Karma test runner that generates code coverage reports. It uses Istanbul under the hood to instrument the code and then record the coverage data when the tests are run. The reports can be generated in various formats and can be integrated into continuous integration workflows to ensure code quality.
What are karma-coverage's main functionalities?
Coverage Reporting
This feature allows you to generate coverage reports in various formats such as HTML, LCOV, and JSON. The code sample shows how to configure Karma to use the karma-coverage plugin to generate an HTML coverage report.
module.exports = function(config) {
config.set({
// other Karma configuration...
reporters: ['progress', 'coverage'],
preprocessors: {
'**/*.js': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
});
};
Threshold Enforcement
This feature allows you to enforce coverage thresholds. If the code does not meet the specified thresholds for statements, branches, functions, or lines, Karma will exit with an error. The code sample demonstrates how to set these thresholds in the Karma configuration.
module.exports = function(config) {
config.set({
// other Karma configuration...
reporters: ['progress', 'coverage'],
coverageReporter: {
check: {
global: {
statements: 80,
branches: 80,
functions: 80,
lines: 80
}
}
}
});
};
Other packages similar to karma-coverage
nyc
nyc is the official Istanbul command line interface. It can produce coverage reports similar to karma-coverage but is more often used with Node.js applications directly, rather than through a test runner like Karma.
jest
Jest is a testing framework that includes its own coverage reporting capabilities. It is an alternative to using Karma with karma-coverage, offering a more integrated solution with a focus on simplicity and ease of use.
c8
c8 is a Node.js code coverage tool built on top of V8's built-in coverage. It is similar to nyc but uses the native coverage features of V8, providing potentially more accurate coverage information for Node.js applications.
karma-coverage
Generate code coverage using Istanbul.
Installation
The easiest way is to install karma-coverage
as a devDependency
,
by running
npm install karma karma-coverage --save-dev
Configuration
For configuration details see docs/configuration.
Examples
Basic
module.exports = function(config) {
config.set({
files: [
'src/**/*.js',
'test/**/*.js'
],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
});
};
CoffeeScript
For an example on how to use with CoffeeScript
see examples/coffee. For an example of how to use with
CoffeeScript and the RequireJS module loader, see
examples/coffee-requirejs (and also see
the useJSExtensionForCoffeeScript
option in
docs/configuration.md).
Advanced, multiple reporters
module.exports = function(config) {
config.set({
files: [
'src/**/*.js',
'test/**/*.js'
],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
dir: 'build/reports/coverage',
reporters: [
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' },
{ type: 'cobertura', subdir: '.', file: 'cobertura.txt' },
{ type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' },
{ type: 'teamcity', subdir: '.', file: 'teamcity.txt' },
{ type: 'text', subdir: '.', file: 'text.txt' },
{ type: 'text-summary', subdir: '.', file: 'text-summary.txt' },
]
}
});
};
FAQ
Don't minify instrumenter output
When using the istanbul instrumenter (default), you can disable code compaction by adding the following to your configuration.
module.exports = function(config) {
config.set({
coverageReporter: {
instrumenterOptions: {
istanbul: { noCompact: true }
}
}
});
};
For more information on Karma see the homepage.