上班途中想到的,到公司后一测,果然如此。看 test.js:
function testFn(a, b, c) {
var ret = 0;
if(a || b || c) {
ret++;
}
return ret;
}
testFn(1);
JSCoverage 覆盖率 100%:jscoverage.html?test.html
实际上,testFn(0, 1); testFn(0, 0, 1); 等情形都没覆盖到。原因很简单, if 语句中存在或,但 jscoverage 生成的代码,是以代码行为单位的:
_$jscoverage['test.js'][6]++;
if ((a || b || c)) {
_$jscoverage['test.js'][7]++;
(ret++);
}
能想到的一种完善方式:对于逻辑表达式,要进一步插入监测代码:
if (((_jsc() && a && _jsc()) || (_jsc() && b && _jsc()) || (_jsc() && c && _jsc()))) {
// ...
}
// _jsc() 是 _$jscoverage['test.js'][n]++ 的示意写法
不过貌似把问题搞复杂了。
总之目前我们要知道:在 JSCoverage 里,100% != 100%.
[882]