Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
istanbul-instrumenter-loader
Advanced tools
The 'istanbul-instrumenter-loader' is a webpack loader that instruments JavaScript files with Istanbul coverage, which is useful for generating code coverage reports for your tests.
Instrumenting JavaScript files
This feature allows you to instrument JavaScript files for code coverage. The code sample shows how to configure webpack to use 'istanbul-instrumenter-loader' for all JavaScript files in the 'src/' directory.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
include: path.resolve('src/'),
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
}
]
}
};
Using with Babel
This feature demonstrates how to use 'istanbul-instrumenter-loader' in conjunction with 'babel-loader' to transpile and instrument JavaScript files. The code sample shows the configuration for webpack to use both loaders.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
include: path.resolve('src/'),
use: [
'babel-loader',
{
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
}
]
}
]
}
};
The 'babel-plugin-istanbul' is a Babel plugin that instruments your code with Istanbul coverage. It is similar to 'istanbul-instrumenter-loader' but is used as a Babel plugin instead of a webpack loader. This can be more convenient if you are already using Babel for transpilation.
The 'nyc' package is a command-line interface for Istanbul. It provides a more comprehensive solution for code coverage, including running tests and generating reports. Unlike 'istanbul-instrumenter-loader', which is a webpack loader, 'nyc' can be used independently of webpack.
Instrument JS files with Istanbul for subsequent code coverage reporting.
Using Babel to transpile ES6/ES7? Use isparta-loader to coverage your original code.
$ npm i -D istanbul-instrumenter-loader
Let's say you have the following:
├── src/
│ └── components/
│ ├── bar/
│ │ └── index.js
│ └── foo/
│ └── index.js
└── test/
└── src/
└── components/
└── foo/
└── index.js
To create a code coverage report for all components (even for those for which you have no tests yet) you have to require all the 1) sources and 2) tests. Something like it's described in "alternative usage" of karma-webpack:
// require all `test/components/**/index.js`
const testsContext = require.context('./src/components/', true, /index\.js$/);
testsContext.keys().forEach(testsContext);
// require all `src/components/**/index.js`
const componentsContext = require.context('../src/components/', true, /index\.js$/);
componentsContext.keys().forEach(componentsContext);
This file will be the only entry point for Karma:
config.set({
…
files: [
'test/index.js'
],
preprocessors: {
'test/index.js': 'webpack'
},
webpack: {
…
module: {
preLoaders: [
// instrument only testing sources with Istanbul
{
test: /\.js$/,
include: path.resolve('src/components/'),
loader: 'istanbul-instrumenter'
}
]
}
…
},
reporters: [ 'progress', 'coverage' ],
coverageReporter: {
type: 'text'
},
…
});
FAQs
Istanbul instrumenter loader for webpack
We found that istanbul-instrumenter-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.