即使未达到测试覆盖率阈值,Karma-coverage 退出代码也始终为 0

Posted

技术标签:

【中文标题】即使未达到测试覆盖率阈值,Karma-coverage 退出代码也始终为 0【英文标题】:Karma-coverage exit code is always 0 even when test coverage threshold is not met 【发布时间】:2021-02-19 21:43:31 【问题描述】:

我正在尝试为 UI5 应用程序构建一个 gitlab-ci 管道,以包括使用 Karma runner 和 karma-ui5 的测试阶段。如果未达到测试覆盖率阈值,我希望管道失败。我的 karma.conf.js 看起来像这样:

module.exports = function(config) 
    "use strict";

    config.set(

        frameworks: ["ui5"],

        preprocessors: 
            "webapp,webapp/!(test)/*.js": ["coverage"]
        ,

        coverageReporter: 
            includeAllSources: true,
            reporters: [
                
                    type: "html",
                    dir: "coverage"
                ,
                
                    type: "text"
                
            ],
            check: 
                each: 
                    statements: 100,
                    branches: 100,
                    functions: 100,
                    lines: 100
                
            
        ,

        reporters: ["progress", "coverage"],

        browsers: ["CustomChromeHeadless"],
        customLaunchers: 
            CustomChromeHeadless: 
                base: 'ChromeHeadless',
                flags: ['--no-sandbox']
            
        ,

        browserConsoleLogOptions: 
            level: "error"
        ,

        singleRun: true
    );
;

Karma 覆盖率报告不满足覆盖率阈值但退出代码始终为 0 的文件,因此管道不会失败。

输出如下所示:

TOTAL: 2 SUCCESS
07 11 2020 10:08:46.033:ERROR [coverage]: HeadlessChrome 64.0.3282 (Linux 0.0.0): Coverage for branches (50%) does not meet per-file (/builds/younang/ci/web/webapp/controller/View.controller.js)  threshold (100%)
07 11 2020 10:08:46.034:ERROR [coverage]: HeadlessChrome 64.0.3282 (Linux 0.0.0): Coverage for statements (0%) does not meet per-file (/builds/younang/ci/web/webapp/localService/mockserver.js)  threshold (100%)
07 11 2020 10:08:46.034:ERROR [coverage]: HeadlessChrome 64.0.3282 (Linux 0.0.0): Coverage for branches (0%) does not meet per-file (/builds/younang/ci/web/webapp/localService/mockserver.js)  threshold (100%)
07 11 2020 10:08:46.034:ERROR [coverage]: HeadlessChrome 64.0.3282 (Linux 0.0.0): Coverage for lines (0%) does not meet per-file (/builds/younang/ci/web/webapp/localService/mockserver.js)  threshold (100%)
07 11 2020 10:08:46.034:ERROR [coverage]: HeadlessChrome 64.0.3282 (Linux 0.0.0): Coverage for functions (0%) does not meet per-file (/builds/younang/ci/web/webapp/localService/mockserver.js)  threshold (100%)
-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |      25 |     4.35 |   41.18 |      25 |                   
 webapp            |     100 |      100 |     100 |     100 |                   
  Component.js     |     100 |      100 |     100 |     100 |                   
 webapp/controller |     100 |       50 |     100 |     100 |                   
  ...controller.js |     100 |       50 |     100 |     100 | 16                
 ...p/localService |       0 |        0 |       0 |       0 |                   
  mockserver.js    |       0 |        0 |       0 |       0 | 1-115             
 webapp/model      |     100 |      100 |     100 |     100 |                   
  models.js        |     100 |      100 |     100 |     100 |                   
-------------------|---------|----------|---------|---------|------------------

你可以在Gitlab here看到完整的设置

知道我在这里缺少什么吗?非常感谢您的帮助。

谢谢

【问题讨论】:

【参考方案1】:

看起来这是known issue。解决方案是将业力升级到5.2.0。试试看吧?

【讨论】:

以上是关于即使未达到测试覆盖率阈值,Karma-coverage 退出代码也始终为 0的主要内容,如果未能解决你的问题,请参考以下文章

测试计划

如果 JUnit 覆盖率低于某个阈值,如何使 Maven 构建失败

软件测试上线标准

软件测试上线标准

Jacoco Gradle 中的最小代码覆盖率阈值

没有覆盖设置的“由于覆盖阈值违规”开玩笑失败