Skip to content

覆盖率服务 ​

先在 CI 中运行测试并生成服务能读取的报告,再上传对应文件。下面的步骤假设 outputDir 为 ./coverage-reports;lcov: true 会额外输出 lcov.info。如果测试分片各自生成报告,请先决定是在 MCR 中合并,还是交给覆盖率服务合并。

Codecov ​

codecov 使用内置的 codecov 报告生成 codecov.json。

js
const coverageOptions = {
    outputDir: './coverage-reports',
    reports: [
        ['codecov']
    ]
};

GitHub Actions 上传步骤:

yml
- name: Codecov
  uses: codecov/codecov-action@v7
  with:
    token: ${{ secrets.CODECOV_TOKEN }}
    files: ./coverage-reports/codecov.json
    disable_search: true
    fail_ci_if_error: true

disable_search 确保只上传指定的文件;fail_ci_if_error 让上传失败时 CI 报错。如果不想保存上传 token,可按 Codecov Action 文档配置 OIDC。

Codacy ​

Codacy 使用 LCOV 报告:

js
const coverageOptions = {
    outputDir: './coverage-reports',
    lcov: true
};

GitHub Actions 上传步骤:

yml
- name: Codacy Coverage Reporter
  uses: codacy/codacy-coverage-reporter-action@v1
  with:
    project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
    coverage-reports: ./coverage-reports/lcov.info

Coveralls ​

Coverage Status 使用 LCOV 报告:

js
const coverageOptions = {
    outputDir: './coverage-reports',
    lcov: true
};

GitHub Actions 上传步骤:

yml
- name: Coveralls
  uses: coverallsapp/github-action@v2
  with:
    file: ./coverage-reports/lcov.info
    format: lcov

如果每个分片分别上传,请按 Coveralls Action 文档配置 parallel,并在所有分片完成后发送 parallel-finished。

SonarQube Cloud ​

Coverage 覆盖率需要 CI 分析,不支持自动分析;如果项目开启了自动分析,请先关闭。检出代码时为 actions/checkout 设置 fetch-depth: 0,并在扫描前生成 lcov.info。请替换示例中的组织、项目键和目录,参见官方 LCOV 配置说明。

yml
- name: Analyze with SonarQube Cloud
  uses: SonarSource/sonarqube-scan-action@v8
  env:
    SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  with:
    projectBaseDir: ./
    args: >
      -Dsonar.organization=your-organization
      -Dsonar.projectKey=your-project-key
      -Dsonar.javascript.lcov.reportPaths=coverage-reports/lcov.info
      -Dsonar.sources=lib
      -Dsonar.tests=test