Codecov、Github 操作和 Angular 11“未找到覆盖率报告”

问题描述

我正在尝试通过 Github 操作将 Angular (v11) 库的代码覆盖率发布到 Codecov.io

我已经设置了来自市场的官方 Codecov github 操作

name: tests

on:
  pull_request:
    branches: [ master ]

jobs:
  build:
    # Machine environment:
    # We specify the Node.js version manually below,and use versioned Chrome from Puppeteer.
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14
        uses: actions/setup-node@v1
        with:
          node-version: 14
      - name: Install dependencies
        run: npm install
      - name: Build
        run: npm run build-lib
      - name: Test
        run: npm run test-lib-headless

      - name: Codecov
        uses: codecov/codecov-action@v1.1.1

package.json 中的任务

"test-lib-headless": "ng test ngx-scrollbar --watch=false --no-progress --browsers=ChromeHeadless --code-coverage",

karma.conf.js

module.exports = function (config) {
  config.set({
    basePath: '',frameworks: ['jasmine','@angular-devkit/build-angular'],plugins: [
      require('karma-jasmine'),require('karma-chrome-launcher'),require('karma-jasmine-html-reporter'),require('karma-coverage'),require('@angular-devkit/build-angular/plugins/karma')
    ],client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example,you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
      },clearContext: false // leave Jasmine Spec Runner output visible in browser
    },jasmineHtmlReporter: {
      suppressAll: true // removes the duplicated traces
    },coverageReporter: {
      dir: require('path').join(__dirname,'./coverage'),subdir: '.',reporters: [
        { type: 'html' },{ type: 'text-summary' }
      ]
    },reporters: ['progress','kjhtml'],port: 9876,colors: true,logLevel: config.LOG_INFO,autoWatch: true,browsers: ['Chrome'],customLaunchers: {
      ChromeHeadlessCI: {
        base: 'ChromeHeadless',flags: ['--no-sandBox']
      }
    },singleRun: false,restartOnFileChange: true,capabilities: {
      chromeOptions: {
        args: ["--headless"]
      },'browserName': 'chrome'
    },});
};

coverage 文件coverage 目录中创建

enter image description here

在 Github actions CI 中,显示 codecov 没有找到文件

enter image description here

为什么本地生成文件也找不到? Codecov 会寻找不同的报告扩展吗?我怎样才能让它工作?

解决方法

这是因为 codecov 不支持默认生成的覆盖率报告。

简单的解决方案就是将 lcov 报告器添加到您的配置中。

https://istanbul.js.org/docs/advanced/alternative-reporters/#lcovonly

coverageReporter: {
      dir: require('path').join(__dirname,'./coverage/peach-tree'),subdir: '.',reporters: [
        { type: 'html' },{ type: 'text-summary' },{ type: 'lcovonly' },]
    },

然后由 codecov 提供的 bash 脚本将毫无问题地上传您的报告。