isparta instrumenter loader for webpack
Instrument Babel code with isparta for subsequent code coverage reporting.
$ npm i -D isparta-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: {
// *optional* babel options: isparta will use it as well as babel-loader
babel: {
presets: ['es2015', 'stage-0', 'react']
},
// *optional* isparta options: istanbul behind isparta will use it
isparta: {
embedSource: true,
noAutoWrap: true,
// these babel options will be passed only to isparta and not to babel-loader
babel: {
presets: ['es2015', 'stage-0', 'react']
}
},
โฆ
module: {
preLoaders: [
// transpile all files except testing sources with babel as usual
{
test: /\.js$/,
exclude: [
path.resolve('src/components/'),
path.resolve('node_modules/')
],
loader: 'babel'
},
// transpile and instrument only testing sources with isparta
{
test: /\.js$/,
include: path.resolve('src/components/'),
loader: 'isparta'
}
]
}
โฆ
},
reporters: [ 'progress', 'coverage' ],
coverageReporter: {
type: 'text'
},
โฆ
});