本文介绍了获取预转换源代码的Karma代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Karma运行测试,webpack用于捆绑文件,而babel用于es6 - > es5转换。我已经运行了测试并生成了代码覆盖率,但代码覆盖率数字是在转换后的源文件。反正是为了获取原始源文件的代码覆盖率吗?
I'm using Karma to run tests, webpack to bundle files, and babel for es6 -> es5 transpilation. I've got the tests running and code coverage being generated, but the code coverage numbers are for the source files after being transpiled. Is there anyway to get code coverage for the original source files instead?
我尝试使用 sourcemap
预处理器但是它似乎没有做任何事情。我是否需要将其添加到某个地方的webpack配置中?
I tried using the sourcemap
preprocessor but it didn't seem to do anything. Do I need to add that to the webpack config somewhere?
karma.conf.js
config.set({
browsers: ['Chrome'], //run in Chrome
files: [
'src/**/*-test.js'
],
frameworks: ['mocha'], //use the mocha test framework
plugins: [
'karma-chrome-launcher',
'karma-mocha',
'karma-sourcemap-loader',
'karma-webpack',
'karma-coverage',
],
preprocessors: {
'src/**/*-test.js': ['webpack']
},
reporters: ['dots', 'coverage'], //report results in this format
coverageReporter: {
reporters: [{
type: 'text-summary',
}, {
type: 'html',
dir: 'build/reports/coverage'
}]
},
singleRun: true, //just run once by default
webpack: {
node: {
fs: 'empty'
},
// Instrument code that isn't test or vendor code.
module: {
loaders: [{
test: /\.js?$/,
include: path.join(__dirname, 'src/js'),
loader: 'babel?stage=0'
}],
postLoaders: [{
test: /\.js$/,
exclude: /(test|node_modules)\//,
loader: 'istanbul-instrumenter'
}]
}
},
webpackMiddleware: {
noInfo: true //please don't spam the console when running in karma!
}
});
推荐答案
以下配置正常工作:
var path = require('path');
module.exports = function(config) {
config.set({
browsers: [ 'Chrome' ], //run in Chrome
files: [
'src/**/*-test.js'
],
frameworks: [ 'mocha' ], //use the mocha test framework
preprocessors: {
'src/**/*-test.js': [ 'webpack' ]
},
reporters: [ 'dots', 'coverage' ], //report results in this format
coverageReporter: {
reporters: [
{
type: 'text-summary'
},
{
type: 'html',
dir: 'build/reports/coverage'
}
]
},
singleRun: true, //just run once by default
webpack: {
node : {
fs: 'empty'
},
// Instrument code that isn't test or vendor code.
module: {
preLoaders: [
{ test: /\.js$/, loader: 'isparta', include: path.join(__dirname, 'src/js') }
],
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'src/js'),
loader: 'babel?stage=0'
}
]
}
},
webpackMiddleware: {
noInfo: true //please don't spam the console when running in karma!
}
});
};
package.json
package.json
{
"devDependencies": {
"babel-core": "^5.8.22",
"babel-loader": "^5.3.2",
"chai": "^3.2.0",
"isparta-loader": "^0.2.0",
"karma": "^0.13.9",
"karma-chrome-launcher": "^0.2.0",
"karma-coverage": "^0.5.0",
"karma-mocha": "^0.2.0",
"karma-webpack": "^1.7.0",
"mocha": "^2.2.5",
"webpack": "^1.11.0"
}
}
这篇关于获取预转换源代码的Karma代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!