我已经配置了我的 karma.conf.js 并启用了预处理,以获得有关我的测试代码覆盖率的报告。我已将此行添加到 preprocessors 部分。
preprocessors: {
'public/js/app.js': ['coverage'],
'public/js/filters.js': ['coverage'],
'public/js/directives.js': ['coverage'],
'public/js/services/*.js': ['coverage'],
'public/js/controllers/*.js': ['coverage'],
},
我得到的是一份完全错误的报告。我知道我已经为每个模块和其中的功能编写了测试。但是覆盖率报告只正确显示了服务测试。
例如指令测试。我知道我已经写了一些测试,测试也会被执行。但报告显示我只测试了 36% 的代码行。
这种奇怪行为的原因可能是什么?
更新:
我从 spec reporter 看到了这个输出:
Directives:
bsTooltip:
when the element was created:
PASSED - should call the popup function
bsSwitchtext:
when the model isBusy changes to true:
PASSED - should call the button method with loading
when the model isBusy changes to false changes:
PASSED - should call the button method with loading
所以我认为我的测试会全部执行。
最佳答案
看起来 Angular 使用的 Typescript 和 Jasmine 有问题。为测试构建启用源映射似乎可以解决此问题。
我在 Angular 6.1 中启用了 source map,如下所示,
转到angular.json
,在主项目中,找到test
,并添加sourceMap:true
,为测试运行启用source map .
要在 CLI 中启用它,请使用命令 --source-map
或 --sm=true
运行
Github 问题链接
- Code coverage report issue with branch coverage (if path not taken)
- ng test --code-coverage in 6.1 improperly detecting branches
我有义务写下这个答案,因为我遇到了同样的问题,这是谷歌搜索的第一个问题。
关于karma-runner - karma 报应中的错误报道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065792/