GitHub Action执行Jest覆盖率检查时遇No package data found错误
解决GitHub Action中Code Coverage Summary的"No package data found"错误
问题场景
配置GitHub Action执行Jest单元测试并生成覆盖率报告,使用Code Coverage Summary Action检查行覆盖率是否在60-80区间,但运行作业时抛出错误:
Parsing Error: No package data found
当前使用的tests.yml配置:
name: Node tests (with coverage) on: push: branches: - '*' - '!master' - '!SS-test' - '!SS-UAT-test' - '!PS-prod' pull_request: branches: - 'master' jobs: unit_tests: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Setup node uses: actions/setup-node@v1 with: node-version: 14 - name: Install npm packages run: npm ci - name: Test run: npm run test:coverage - name: Upload coverage artifact uses: actions/upload-artifact@v3 with: name: my-artifact path: "coverage/" - name: Code Coverage Report uses: irongut/CodeCoverageSummary@v1.3.0 with: filename: "/coverage/cobertura-coverage.xml" badge: true fail_below_min: true format: markdown hide_branch_rate: false hide_complexity: true indicators: true output: both thresholds: '60 80'
生成的cobertura-coverage.xml内容为空包数据:
<?xml version="1.0" ?> <!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd"> <coverage lines-valid="0" lines-covered="0" line-rate="NaN" branches-valid="0" branches-covered="0" branch-rate="NaN" timestamp="1661235506222" complexity="0" version="0.1"> <sources> <source>C:\guardian group\c360-account-services</source> </sources> <packages> </packages> </coverage>
错误原因
- Jest未生成有效覆盖率数据:
cobertura-coverage.xml中<packages>为空、lines-valid=0,说明Jest未收集到任何代码覆盖率信息,可能是测试未执行、测试文件匹配规则错误,或覆盖率报告配置缺失。 - 文件路径错误:Code Coverage Summary Action中
filename使用了绝对路径/coverage/cobertura-coverage.xml,而GitHub Runner的工作目录是仓库根目录,绝对路径会导致文件找不到。 - 路径格式不兼容:
<source>中的Windows路径在Ubuntu Runner中无法识别,可能影响报告解析。
修复步骤
1. 修正Jest配置,生成有效Cobertura报告
在项目根目录的jest.config.js中添加或更新配置:
module.exports = { // 确保测试文件被正确匹配 testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)+(spec|test).js?(x)'], // 启用覆盖率收集并指定Cobertura格式 collectCoverage: true, coverageReporters: ['cobertura', 'html'], // 指定需要收集覆盖率的文件范围(根据项目结构调整) collectCoverageFrom: [ 'src/**/*.js', '!src/**/*.test.js' ] };
同时确保package.json中的test:coverage命令正确:
{ "scripts": { "test:coverage": "jest --coverage" } }
2. 修正GitHub Action中的文件路径
将Code Coverage Summary步骤的filename改为相对路径:
- name: Code Coverage Report uses: irongut/CodeCoverageSummary@v1.3.0 with: filename: "coverage/cobertura-coverage.xml" # 去掉开头的斜杠 badge: true fail_below_min: true format: markdown hide_branch_rate: false hide_complexity: true indicators: true output: both thresholds: '60 80'
3. 验证本地测试与报告生成
在本地运行npm run test:coverage,检查:
- 控制台是否显示测试用例执行成功
coverage/cobertura-coverage.xml中是否包含<package>、<class>等实际数据,例如:
<package name="src" line-rate="0.75" branch-rate="0.5" complexity="0"> <classes> <class name="utils.js" filename="src/utils.js" line-rate="0.75" branch-rate="0.5" complexity="0"> <!-- 具体行覆盖率数据 --> </class> </classes> </package>
验证
重新推送代码或触发Pull Request,GitHub Action作业应能正常解析覆盖率报告,并根据阈值判断是否通过。
内容的提问来源于stack exchange,提问作者karthik suryadevara
相关产品推荐
相关产品推荐

