我正在使用 istanbul(通过 grunt、jasmine 和 phantomJS)为我的单元测试生成代码覆盖率报告。我得到了由模块引起的误报,这些模块具有我正在测试的模块作为调用其方法的依赖项,即
module1.js
加载 module2.js
作为依赖项。 module1 的单元测试调用 module1.method
,后者又调用 module2.method
。当我查看代码覆盖率报告时,module2.method
以绿色突出显示,即使没有对其进行测试。
这是我应该担心的事情吗?我该如何避免呢?
最佳答案
我创建了这个 grunt 任务来帮助识别自己的测试套件未明确覆盖的文件
grunt.registerTask('missingSpecs', 'Missing specs list', function (env, limit) {
var specs = getFileList(getSpecs(env)),
src = getFileList(getSrc(env)),
missingSpecs = [];
for (var file in src) {
if (specs.indexOf(src[file]) === -1) {
missingSpecs.push(src[file]);
}
}
if (missingSpecs.length) {
console.log(
'\n******************************************************************************************************\n' +
'*** The following common js files aren\'t covered by any tests. They won\'t write themselves, y\'know ***\n' +
'******************************************************************************************************\n'
);
missingSpecs.sort(function () {
return Math.random() - 0.5;
}).slice(0,limit).forEach(function (file) {
console.log(file);
});
}
})
其中 getFileList 使用 grunt.file.expand 和一些正则表达式来获取模块名称列表。
关于javascript - 代码覆盖率 - 消除误报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17575106/