[英]Karma-Coverage Report Shows Code As Covered (which is obviously not covered)
我試圖生成HTML覆蓋率報告,但它不包含我期望的輸出。 也許我在這里錯了,但它應該只顯示那些從spec文件中調用的行和方法,對吧?
不知怎的,它沒有。
更新:
我創建了一個存儲庫來提供一個工作示例,概述了問題:
https://github.com/gearsdigital/stunning-octo-train
這是我的(測試)項目設置。 如果需要,我可以將它推送到GitHub倉庫,因為我不知道如何設置JSFiddle來運行此代碼。
有一個生成HTML覆蓋率報告的過程。 此報告顯示所涵蓋的代碼,由於沒有可用的測試,因此顯然沒有涵蓋。
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
});
};
var webpack = require('webpack');
var path = require('path');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
]
};
// 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);
import {Calculator} from './modules/Calculator';
let c = new Calculator();
c.add(1,2); // 3
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;
}
}
import {Calculator} from '../src/js/modules/Calculator';
describe('Calculator', function () {
it('should return 10', function () {
expect(true).toBe(false);
});
});
我希望發現add()
,因為它不是在任何測試中調用,而是在bootstrap.js
調用。
src/js/bootstrap.js
被加載; 這意味着這些行被執行。
import {Calculator} from './modules/Calculator';
let c = new Calculator();
c.add(1,2); // 3
我猜這塊是罪魁禍首:
{
test: /\.js$/,
include: /(src\/js)/,
exclude: /(vendor)/,
loaders: ['isparta']
}
最簡潔的答案是不。
導入模塊時,可能會覆蓋任何不在閉包/函數/方法中的內容。
// this will get coverage; because it is top-level code outside a function block
var dummy = null;
export class Calculator {
add(op1, op2) {
// this will get coverage only if you call explicitly the function.
return op1 + op2;
}
}
// this will get coverage.
var c = new Calculator();
// and now, Calculator.add will get coverage.
c.add(1,2); // 3
好的,所以我不熟悉Webpack或Babel,但我強烈懷疑,因為你將所有文件都包含在Karma配置中,所以無論測試中的代碼是什么,代碼都會被執行。 我已經檢查了你的GitHub的例子(關於這樣做的贊譽,它更容易調試),如果我注釋掉cart.js
的第3行和第4 cart.js
,那么覆蓋率報告並沒有將utilities.js
顯示為'ran' 。
簡而言之:顯然,將未封裝到函數中的代碼通過將文件包含在Karma中來運行。 另外,不要相信因果報應的時候那里都沒有測試(如,從字面上0 it
),它是在這種特殊情況下不可靠。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.