istanbul-instrumenter-loader
istanbul-instrumenter-loader
使用 istanbul-lib-instrument 将仪器 JS 文件用于随后的代码覆盖率报告
安装
npm i -D istanbul-instrumenter-loader
用法
结构
├─ src
│ |– components
│ | |– bar
│ | │ |─ index.js
│ | |– foo/
│ |– index.js
|– test
| |– src
| | |– components
| | | |– foo
| | | | |– index.js
为生成所有组件(包括你没写测试
的那些)的代码覆盖率报告,你需要 require 所有业务
和测试
的代码。相关内容在 karma-webpack 其他用法中有涉及
test/index.js
// requires all tests in `project/test/src/components/**/index.js`
const tests = require.context('./src/components/', true, /index\.js$/
tests.keys().forEach(tests
// requires all components in `project/src/components/**/index.js`
const components = require.context('../src/components/', true, /index\.js$/
components.keys().forEach(components
ℹ️以下为
karma
的唯一入口
起点文件
karma.conf.js
config.set{
...
files: [
'test/index.js'
],
preprocessors: {
'test/index.js': 'webpack'
},
webpack: {
...
module: {
rules: [
// instrument only testing sources with Istanbul
{
test: /\.js$/,
use: { loader: 'istanbul-instrumenter-loader' },
include: path.resolve('src/components/')
}
]
}
...
},
reporters: [ 'progress', 'coverage-istanbul' ],
coverageIstanbulReporter: {
reports: [ 'text-summary' ],
fixWebpackSourcePaths: true
}
...
}
使用 Babel
您必须将该检测作为后续步骤运行
webpack.config.js
{
test: /\.js$|\.jsx$/,
use: {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
},
enforce: 'post',
exclude: /node_modules|\.spec\.js$/,
}
选项
此 loader 支持 istanbul-lib-instrument
的所有配置选项
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
debug | {Boolean} | false | 打开调试模式 |
compact | {Boolean} | true | |
autoWrap | {Boolean} | false | 生成紧凑的代码 |
esModules | {Boolean} | false | 设置为true以检测ES2015模块 |
coverageVariable | {String} | __coverage__ | 全局覆盖变量的名称 |
preserveComments | {Boolean} | false | 保留输出中的注释 |
produceSourceMap | {Boolean} | false | 设置为true以生成已检测代码的source map |
sourceMapUrlCallback | {Function} | null | 在原始代码中找到源映射URL时调用的回调函数。 使用源文件名和源映射URL调用此函数 |
webpack.config.js
{
test: /\.js$/,
use: {
loader: 'istanbul-instrumenter-loader',
options: {...options}
}
}