我试图生成 HTML 覆盖率报告,但它不包含我期望的输出。也许我在这里错了,但它应该只显示那些从规范文件中调用的行和方法,对吗?
不知何故,它没有。
更新:
我创建了一个存储库来提供一个工作示例,概述了问题:
https://github.com/gearsdigital/stunning-octo-train
这是我的(测试)项目设置。如果需要,我可以将其推送到 GitHub 存储库,因为我不知道如何设置 JSFiddle 来运行此代码。
TL;博士
有一个生成 HTML 覆盖率报告的过程。该报告显示了已覆盖的代码,显然没有覆盖,因为没有可用的测试。
业力.conf.js:
var webpack = require('webpack');
var path = require('path');
// Reference webpack.config.js, don't repeat it!
var webpackConfig = require('./webpack.config.js');
// The entry point from the referenced Webpack configuration has to be
// removed or tests will fail in weird and inscrutable ways.
// Easy enough, just define an empty entry object (null won't work).
webpackConfig.entry = {};
webpackConfig.module = {
preLoaders: [
{
test: /\.js$/,
// files within these directories should be excluded
// for babel processing
exclude: /(node_modules)/,
loaders: ['babel?cacheDirectory']
},
{
test: /\.js$/,
include: /(src\/js)/,
exclude: /(vendor)/,
loaders: ['isparta']
}
]
};
/**
* Karma configuration
* @param config
*/
module.exports = function (config) {
config.set({
browsers: ['PhantomJS'],
coverageReporter: {
dir: 'test-results',
reporters: [
{type: 'text-summary'},
{type: 'html', subdir: 'coverage'}
]
},
files: [
'webpack.test.config.js'
],
frameworks: [
'jasmine'
],
preprocessors: {
'webpack.test.config.js': ['webpack']
},
reporters: ['spec', 'coverage'],
webpack: webpackConfig
});
};
webpack.config.js:
var webpack = require('webpack');
var path = require('path');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
]
};
webpack.test.config.js:
// make sure the file name regexp matches your test files.
var testsContext = require.context('./tests', true, /\.spec\.js$/);
testsContext.keys().forEach(testsContext);
// make sure the file name regexp matches your test files.
var srcContext = require.context('./src/js', true, /\.js$/);
srcContext.keys().forEach(srcContext);
bootstrap.js:
import {Calculator} from './modules/Calculator';
let c = new Calculator();
c.add(1,2); // 3
计算器.js:
export class Calculator {
add(op1, op2) {
return op1 + op2;
}
sub(op1, op2) {
if (typeof op1 !== 'number') {
return false;
}
return op1 - op2;
}
mul(op1, op2) {
return op1 * op2;
}
div(op1, op2) {
return op1 / op2;
}
}
bootstrap.spec.js:
import {Calculator} from '../src/js/modules/Calculator';
describe('Calculator', function () {
it('should return 10', function () {
expect(true).toBe(false);
});
});
生成的报告:
我希望add()
被发现,因为它没有在任何测试中调用,而是在bootstrap.js
.