简体   繁体   English

当不满足覆盖要求时,业力覆盖不会使构建失败

[英]Karma coverage does not fail the build when the coverage requirements are not met

I'm using NX from NRWL with my Angular app and I run the unit tests in my CI using the following command:我将NRWL 的 NX与我的Angular应用程序一起使用,并使用以下命令在我的 CI 中运行单元测试:

nx affected:test --base=main --watch=false --browsers=ChromeHeadlessNoSandbox --codeCoverage --parallel=true --maxParallel=4

when the code coverage requirements are not met the build still passes despite I can clearly see the coverage error in the build log:当不满足代码覆盖率要求时,构建仍然通过,尽管我可以在构建日志中清楚地看到覆盖率错误:

07 12 2022 16:06:14.750:ERROR [coverage]: Chrome Headless 106.0.5249.91 (Linux x86_64): Coverage for branches (99.68%) does not meet global threshold (100%) 07 12 2022 16:06:14.750:ERROR [coverage]:Chrome Headless 106.0.5249.91 (Linux x86_64):分支机构的覆盖率 (99.68%) 未达到全局阈值 (100%)

how can I tell nx to make this command fail as soon as the coverage requirements are not met?一旦不满足覆盖要求,我如何告诉 nx 让此命令失败?

EDIT: even running just one single project's test has the same problem:编辑:即使只运行一个项目的测试也有同样的问题:

nx run one-single-project:test --watch=false --browsers=ChromeHeadlessNoSandbox --codeCoverage=true

If I now echo $?如果我现在echo $? it will be 0 but it should be error instead.它将是0但它应该是错误的。

The problem was unrelated to nx .该问题与nx无关。

I figured out after a couple of hours of debugging that it was about karma-coverage and its config.经过几个小时的调试后,我发现它与karma-coverage及其配置有关。

In my karma.config.js :在我的karma.config.js

    coverageReporter: {
      dir: join(__dirname, 'coverage'),
      subdir: '.',
      fixWebpackSourcePaths: true,
      reporters: [{ type: 'text-summary'}, { type: 'html' }, { type: 'lcovonly', subdir: './' }],
      check: {
        global: {
          statements: 100,
          lines: 100,
          branches: 100,
          functions: 100,
        },
      },
    },

More specifically, inside reporters I had { type: 'text-summary' } which was missing the file property and for some reasons it failed the coverage check.更具体地说,在reporters内部我有{ type: 'text-summary' }缺少file属性并且由于某些原因它未能通过覆盖率检查。 So the correct way is: { type: 'text-summary', file: 'coverage.txt' }所以正确的方法是: { type: 'text-summary', file: 'coverage.txt' }

The full coverageReporter config now looks like this:完整的coverageReporter配置现在看起来像这样:

    coverageReporter: {
      dir: join(__dirname, 'coverage'),
      subdir: '.',
      fixWebpackSourcePaths: true,
      reporters: [{ type: 'text-summary', file: 'coverage.txt' }, { type: 'html' }, { type: 'lcovonly', subdir: './' }],
      check: {
        global: {
          statements: 100,
          lines: 100,
          branches: 100,
          functions: 100,
        },
      },
    },

I believe this is a bug of karma-coverage and I will submit a fix soon.我相信这是业力覆盖的错误,我会尽快提交修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM