根据这个article我想获得 jest-junit 代码覆盖率报告(文章中的选项 2)
所以,在我的 package.json 中,我像这样调用 jest:"test": "jest --config=jest.config.js",
jest.config.js 包括以下设置:
module.exports = {
preset: 'react-native',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
setupFiles: ['./jest.setup.js'],
collectCoverage: true,
coverageDirectory: 'src/testCoverage',
coverageReporters: [ "text"],
reporters: ["default",
["jest-junit", {usePathForSuiteName: true, outputDirectory: 'src/testCoverage'}]
],
testResultsProcessor: "jest-junit"
};
我这样做是因为 package.json 中编写的 Jest 代码对我不起作用。
当我执行
npm run test
我在 src/testCoverage 文件夹和 junit.xml 中获得覆盖数据然后我在 Jenkins 管道中执行测试阶段:
stage('test') {
steps{
sh script:'''
#!/bin/bash
npm install -g yarn
yarn install
yarn add --dev jest-junit
npm run test
'''
}
post {
always {
junit 'src/testCoverage/junit.xml'
}
}
}
但是我在 Jenkins 中没有看到 junit 覆盖率报告,而文章说的是
The line calling junit will publish the report that npm run test created
我唯一拥有的 - 这是通过和失败测试的测试结果报告。
为什么我在 Jenkins 中没有得到 junit 覆盖率报告?我应该做什么或改变什么?
最佳答案
发生这种情况是因为 junit.xml 仅包含执行的测试用例列表及其持续时间。
为了导出线路覆盖率,您需要报告一些其他覆盖率,例如,cobertura:
coverageReporters: ['text', 'cobertura'],
它将生成包含覆盖信息的coverage/cobertura-coverage.xml 文件。现在您可以使用 junit.xml 报告测试列表,使用 cobertura-coverage.xml 向您的 CICD 系统报告线路覆盖率。
关于react-native - 如何从 React 项目中的 jest-junit 获取代码覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58621541/