在我的 gitlab 管道中,我想将总百分比值发送到服务器。但是 jest --coverage 只给了我 /coverage 中的这些大型报告文件。我似乎无法从中解析出总价值。我错过了一个参数吗?
L. *_*der 18
感谢 Teneff 的回答,我选择了 coverageReporter="json-summary"。
jest --coverage --coverageReporters="json-summary"
Run Code Online (Sandbox Code Playgroud)
这会生成一个可以轻松解析的coverage-summary.json 文件。我直接从 json 中获取总值:
"total": {
"lines": { "total": 21777, "covered": 65, "skipped": 0, "pct": 0.3 },
"statements": { "total": 24163, "covered": 72, "skipped": 0, "pct": 0.3 },
"functions": { "total": 5451, "covered": 16, "skipped": 0, "pct": 0.29 },
"branches": { "total": 6178, "covered": 10, "skipped": 0, "pct": 0.16 }
}
Run Code Online (Sandbox Code Playgroud)
Ten*_*eff 16
在内部,jest 使用Istanbul.js 来报告覆盖率,您可以使用CLI arg 将Jest 的配置修改为“text-summary”或任何其他替代报告者。
jest --coverageReporters="text-summary"
Run Code Online (Sandbox Code Playgroud)
text-summary output:=============================== Coverage summary ===============================
Statements : 100% ( 166/166 )
Branches : 75% ( 18/24 )
Functions : 100% ( 49/49 )
Lines : 100% ( 161/161 )
================================================================================
Run Code Online (Sandbox Code Playgroud)
或者你可以写你自己的记者。
我自己需要这个,所以我创建了一个自定义记者。您需要在coverageReporters 中启用json-summary 报告器,然后您可以使用此自定义报告器来显示总数:
const { readFile } = require('fs');
const { join } = require('path');
// Gitlab Regex: Total Coverage: (\d+\.\d+ \%)
module.exports = class CoverageReporter {
constructor(globalConfig) {
this.globalConfig = globalConfig;
this.jsonSummary = join(this.globalConfig.coverageDirectory, 'coverage-summary.json');
}
async onRunComplete() {
const coverage = require(this.jsonSummary);
const totalSum = ['lines', 'statements', 'functions', 'branches']
.map(i => coverage.total[i].pct)
.reduce((a, b) => a + b, 0)
const avgCoverage = totalSum / 4
console.debug()
console.debug('========= Total Coverage ============')
console.debug(`Total Coverage: ${avgCoverage.toFixed(2)} %`)
console.debug('=======================================')
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10467 次 |
| 最近记录: |