【问题标题】:Code coverage from Jest to stdout to GitLab从 Jest 到标准输出到 GitLab 的代码覆盖率
【发布时间】:2018-06-11 13:21:23
【问题描述】:

我在 GitLab CI 中运行代码覆盖率的玩笑测试,GitLab 从 gitlab 中跑步者的标准输出中捕获百分比。

jest --coverage 在 stdout 中生成覆盖范围,gitlab 使用 /All files[^|]*\|[^|]*\s+([\d\.]+)/ 正则表达式捕获它,但是当我运行时 jest --coverage --json --outputFile=xyz.json 遗憾的是,jest 没有将代码覆盖率打印到标准输出。

--json 参数被提供给 jest 时,我该怎么做才能从 jest 中获取标准输出中的代码覆盖率?

jest version : v22.4.3 与 jest-cli 相同

【问题讨论】:

    标签: jestjs code-coverage gitlab-ci


    【解决方案1】:

    下面的配置会让GitLab解释Jest生成的覆盖率报告:

    stages:
      - test
    
    Unit tests:
      image: node:12.17.0
      stage: test
      script:
        - jest --coverage
      coverage: /All\sfiles.*?\s+(\d+.\d+)/
    

    GitLab 上有一个 open issue,其中包含使用 Jest 生成的覆盖率报告的正确正则表达式(由 Create React App 使用)。

    【讨论】:

    【解决方案2】:

    我正在使用以下正则表达式来解析来自 Jest for Gitlab 的 text-summary 覆盖率报告:^(?:Statements|Branches|Functions|Lines)\s*:\s*([^%]+)

    请注意,Gitlab 只会考虑 last match。所以上面可以写成^Lines\s*:\s*([^%]+)。我包含了完整的示例,以便您可以选择最适合您的项目的示例。

    “文本摘要”报告在 StdOut 中如下所示:

    =============================== Coverage summary ===============================
    Statements   : 80.49% ( 2611/3244 )
    Branches     : 65.37% ( 923/1412 )
    Functions    : 76.48% ( 582/761 )
    Lines        : 80.44% ( 2583/3211 )
    ================================================================================
    

    确保您在jest.config.js 中包含text-summary 作为报道记者:

    coverageReporters: ['text-summary', 'lcov', 'cobertura'],
    

    【讨论】:

      【解决方案3】:

      我不熟悉 Jest,但如果您要创建 JSON,最简单的方法是简单地 cat JSON 然后相应地更改正则表达式

      【讨论】:

      • json 存储有关通过和失败测试的快照和静态数据,它没有任何数据相关的覆盖率
      • 我什至尝试查看覆盖文件夹coverage-final.json 仅包含有关哪些行被覆盖而不是覆盖多少行的信息?
      • github.com/facebook/jest/issues/2927 这个人有同样的问题,但他们关闭了它
      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多