如何更改 Karma 执行的 LCOV 报告的格式?

我已将 Karma 配置为报告我的 JavaScript 代码的覆盖率.以下是 karma.conf.js 文件中的部分配置:

I've configured Karma to report the coverage of my JavaScript code. Here is the part of the configuration in the karma.conf.js file:

coverageReporter: {
  reporters: [
    {
      type: 'html',
      dir: 'build/karma/coverage'
    },
    {
      type: 'lcov',
      dir: 'build/karma/coverage',
      subdir: '.'
    },
    {
      type: 'cobertura',
      dir: 'build/karma/coverage'
    }
  ]
},

我的 lcov.info 文件格式如下:

My lcov.info file has the following format:

TN:
SF:./app/scripts/app.js
FN:16,(anonymous_1)
FN:26,(anonymous_2)
FNF:2
FNH:1
FNDA:1,(anonymous_1)
FNDA:0,(anonymous_2)
DA:2,1
DA:20,1
DA:29,0
DA:34,0
LF:4
LH:2
BRF:0
BRH:0
end_of_record

很遗憾,Sonarqube JavaScript 插件 只考虑以 SF: 开头的行,DA:BRDA:(参见 LCOVParser).

Unfortunately, the Sonarqube JavaScript plugin only considers the lines that start with SF:, DA: or BRDA: (cf LCOVParser).

因此,LCOV HTML 报告(由伊斯坦布尔制作)在相同数据上为我提供了比 Sonar 更高的代码覆盖率.

Due to that, the LCOV HTML report (made by Istanbul) gives me a higher code coverage than Sonar on the same data.

有没有办法改变生成的lcov.info的格式?

Is there a way to change the format of the lcov.info generated?

如果我查看伊斯坦布尔代码,我可以想象不同的标签:

If I look in Istanbul code, I can imagine the meaning of the different labels:

  • BRFBRHBRDA 用于分支.
  • FNFNFFNHFNDA 用于函数.
  • LNLFLH 用于行.
  • *F为总数,*H为覆盖信息.
  • BRF, BRH, BRDA are for branches.
  • FN, FNF, FNH, FNDA are for functions.
  • LN, LF, LH are for lines.
  • *F is the total, while *H is the covered information.

Istanbul 和 Sonar 覆盖范围之间的差异似乎是由于后者完全忽略了 Functions 和 Branches 覆盖范围.

The difference between the Istanbul and Sonar coverage seems to be due to the fact that the latter completely ignores the Functions and Branches coverage.

有什么办法解决这个问题吗?

Any idea to solve that?

推荐答案

您可以运行以下脚本:cat lcov.info |egrep "^(SF|DA|BRDA):" >lcov.info.new;mv lcov.info.new lcov.info.

You could run a script that does: cat lcov.info | egrep "^(SF|DA|BRDA):" > lcov.info.new; mv lcov.info.new lcov.info.

我得到了:

SF:./app/scripts/app.js
DA:2,1
DA:20,1
DA:29,0
DA:34,0

相关文章