@pkg-nec/babel-plugin-istanbul
v9.0.1
Published
Security-maintained compatible fork of the Babel plugin for Istanbul code coverage instrumentation
Maintainers
Readme
@pkg-nec/babel-plugin-istanbul
@pkg-nec/babel-plugin-istanbul is a security-maintained compatible fork of istanbuljs/babel-plugin-istanbul. It keeps the upstream plugin API and instrumentation behavior while independently maintaining its dependency and supply-chain security posture.
For bugs and feature requests, use the pkg-nec/babel-plugin-istanbul issue tracker. For suspected vulnerabilities, follow the security policy.
A Babel plugin that instruments your code with Istanbul coverage. It can instantly be used with karma-coverage and mocha on Node.js (through nyc).
Note: This plugin does not generate any report or save any data to any file; it only adds instrumenting code to your JavaScript source code. To integrate with testing tools, please see the Integrations section.
Usage
Install it:
npm install --save-dev @pkg-nec/babel-plugin-istanbulAdd it to .babelrc in test mode:
{
"env": {
"test": {
"plugins": [ "@pkg-nec/babel-plugin-istanbul" ]
}
}
}Optionally, use cross-env to set
NODE_ENV=test:
{
"scripts": {
"test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.js"
}
}Integrations
karma
It just works with Karma. First, make sure that the code is already transpiled by Babel (either using karma-babel-preprocessor, karma-webpack, or karma-browserify). Then, simply set up karma-coverage according to the docs, but don’t add the coverage preprocessor. This plugin has already instrumented your code, and Karma should pick it up automatically.
It has been tested with bemusic/bemuse project, which contains ~2400 statements.
mocha on node.js (through nyc)
Configure Mocha to transpile JavaScript code using Babel, then you can run your tests with nyc, which will collect all the coverage report.
babel-plugin-istanbul respects the include/exclude configuration options from nyc,
but you also need to configure NYC not to instrument your code by adding these settings in your package.json:
"nyc": {
"sourceMap": false,
"instrument": false
},Security
See the security policy to report suspected vulnerabilities. GitHub's dependency graph and Dependabot surface newly disclosed dependency vulnerabilities. The Dependency Review workflow prevents pull requests from adding high-or-critical vulnerable dependencies. The project treats transitive and development-tooling dependency risk as a maintenance concern, even when it does not ship in the npm tarball.
Funding
Support maintenance at Buy Me a Coffee.
Compatibility
This fork preserves the upstream plugin API and instrumentation behavior. It will not intentionally diverge unless a future release documents that change.
Releases
Maintainers: see the release versioning guide. Published npm releases include npm provenance. Documentation-, CI-, funding-, and package-discovery-metadata-only changes do not cause an npm release.
Ignoring files
You don't want to cover your test files as this will skew your coverage results. You can configure this by providing plugin options matching nyc's exclude/include rules:
{
"env": {
"test": {
"plugins": [
["@pkg-nec/babel-plugin-istanbul", {
"exclude": [
"**/*.spec.js"
]
}]
]
}
}
}If you don't provide options in your Babel config, the plugin will look for exclude/include config under an "nyc" key in package.json.
You can also use istanbul's ignore hints to specify specific lines of code to skip instrumenting.
Source Maps
By default, this plugin will pick up inline source maps and attach them to the instrumented code such that code coverage can be remapped back to the original source, even for multi-step build processes. This can be memory intensive. Set useInlineSourceMaps to prevent this behavior.
{
"env": {
"test": {
"plugins": [
["@pkg-nec/babel-plugin-istanbul", {
"useInlineSourceMaps": false
}]
]
}
}
}If you're instrumenting code programatically, you can pass a source map explicitly.
import babelPluginIstanbul from '@pkg-nec/babel-plugin-istanbul'
function instrument(sourceCode, sourceMap, filename) {
return babel.transform(sourceCode, {
filename,
plugins: [
[babelPluginIstanbul, {
inputSourceMap: sourceMap
}]
]
})
}Upstream credit
The approach used in babel-plugin-istanbul was inspired by Thai Pangsakulyanont's original library babel-plugin-__coverage__.
