Seamless integration between Rollup and Istanbul.
If you're using Rollup to generate a standalone bundle you will probably need also to bundle your tests before running them, and if you want the code coverage report, you will need to instrument the program files before the bundle is generated to avoid instrumenting also the code of the test files.
That is the reason why rollup-plugin-istanbul exists.
npm install --save-dev rollup-plugin-istanbul
import { rollup } from 'rollup';
import istanbul from 'rollup-plugin-istanbul';
rollup({
entry: 'main.js',
plugins: [
istanbul({
exclude: ['test/**/*.js']
})
]
}).then(...)
All options are optional.
Can be a minimatch pattern or an array of minimatch patterns. If is omitted or of zero length, files should be included by default; otherwise they should only be included if the ID matches one of the patterns.
Can be a minimatch pattern or an array of minimatch patterns. Files to exclude, commonly the test files.
Array of strings that specifies the file extensions to process. default: ['js']
generate compact code. default: true
produce a source map for the instrumented code. default: true
An object of options that will be passed to the instrumenter.
Default value:
{
esModules: true,
compact: true,
produceSourceMap: true
}
More info about options.
Can be a replacement for the istanbul library, for example isparta. It should implement the same API as istanbul.
rollup-plugin-istanbul
can be used with karma or other test runners that allow preprocessors. Here you can see how to implement it with Karma with the help of the karma-rollup-preprocessor and karma-coverage:
// karma.conf.js
var istanbul = require('rollup-plugin-istanbul');
module.exports = function (config) {
config.set({
files: [
'test/*.js'
],
preprocessors: {
'test/*.js': ['rollup']
},
rollupPreprocessor: {
rollup: {
plugins: [
istanbul({
exclude: ['test/*.js']
})
]
}
},
reporters: ['coverage']
});
};
Going further, this is how you can implement it when you are using babel because you are writing ES2015 code:
// karma.conf.js
var babelrc = require('babelrc-rollup').default,
babel = require('rollup-plugin-babel'),
istanbul = require('rollup-plugin-istanbul');
module.exports = function (config) {
config.set({
files: [
'test/*.js'
],
preprocessors: {
'test/*.js': ['rollup']
},
rollupPreprocessor: {
plugins: [
istanbul({
exclude: ['test/*.js']
}),
babel(babelrc())
],
format: 'iife',
sourceMap: 'inline'
},
reporters: ['coverage']
});
};
Example of implementation with Mocha provided by @piuccio: artberri#11