Skip to content

Commit 15b11f1

Browse files
committed
add: coverate rate test
1 parent 11f681e commit 15b11f1

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Test Coverage
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
9+
jobs:
10+
test-coverage:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20.x'
21+
cache: 'npm'
22+
23+
- name: Install dependencies
24+
run: npm ci
25+
26+
- name: Run tests with coverage
27+
run: npm run test:coverage
28+
29+
- name: Upload coverage reports
30+
uses: actions/upload-artifact@v4
31+
with:
32+
name: coverage-report
33+
path: coverage/
34+
retention-days: 30
35+
36+
- name: Coverage Report Summary
37+
if: always()
38+
run: |
39+
echo "## 📊 Test Coverage Summary" >> $GITHUB_STEP_SUMMARY
40+
echo "" >> $GITHUB_STEP_SUMMARY
41+
if [ -f coverage/coverage-summary.json ]; then
42+
echo "### Overall Coverage:" >> $GITHUB_STEP_SUMMARY
43+
echo "\`\`\`json" >> $GITHUB_STEP_SUMMARY
44+
cat coverage/coverage-summary.json | jq '.' >> $GITHUB_STEP_SUMMARY
45+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
46+
fi
47+
echo "" >> $GITHUB_STEP_SUMMARY
48+
echo "📁 [Download detailed coverage report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY
49+
50+
- name: Comment PR with coverage
51+
if: github.event_name == 'pull_request'
52+
uses: actions/github-script@v7
53+
with:
54+
script: |
55+
const fs = require('fs');
56+
if (fs.existsSync('coverage/coverage-summary.json')) {
57+
const coverage = JSON.parse(fs.readFileSync('coverage/coverage-summary.json', 'utf8'));
58+
const total = coverage.total;
59+
60+
const comment = `## 📊 Test Coverage Report
61+
62+
| Metric | Coverage |
63+
|--------|----------|
64+
| Lines | ${total.lines.pct}% (${total.lines.covered}/${total.lines.total}) |
65+
| Functions | ${total.functions.pct}% (${total.functions.covered}/${total.functions.total}) |
66+
| Branches | ${total.branches.pct}% (${total.branches.covered}/${total.branches.total}) |
67+
| Statements | ${total.statements.pct}% (${total.statements.covered}/${total.statements.total}) |
68+
69+
🔗 [View detailed report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})`;
70+
71+
github.rest.issues.createComment({
72+
issue_number: context.issue.number,
73+
owner: context.repo.owner,
74+
repo: context.repo.repo,
75+
body: comment
76+
});
77+
}

README.test-coverage.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 测试覆盖率 (Test Coverage)
2+
3+
本项目已经配置了自动化的测试覆盖率报告功能。
4+
5+
## 🚀 功能特性
6+
7+
- **自动触发**: 当代码推送到 `main``develop` 分支,或创建 Pull Request 时自动运行
8+
- **详细报告**: 生成包含行覆盖率、函数覆盖率、分支覆盖率和语句覆盖率的详细报告
9+
- **PR 评论**: 在 Pull Request 中自动添加覆盖率摘要评论
10+
- **工件保存**: 覆盖率报告作为 GitHub Actions 工件保存30天,可下载查看详细信息
11+
12+
## 📊 覆盖率阈值
13+
14+
当前配置的最低覆盖率要求:
15+
- 行覆盖率: 70%
16+
- 函数覆盖率: 70%
17+
- 分支覆盖率: 70%
18+
- 语句覆盖率: 70%
19+
20+
如果覆盖率低于这些阈值,测试将失败。
21+
22+
## 🛠️ 本地运行
23+
24+
### 运行测试并生成覆盖率报告
25+
```bash
26+
npm run test:coverage
27+
```
28+
29+
### 查看覆盖率报告
30+
```bash
31+
# 在浏览器中打开 HTML 报告
32+
open coverage/lcov-report/index.html
33+
```
34+
35+
## 📁 覆盖率文件说明
36+
37+
运行覆盖率测试后,会在 `coverage/` 目录生成以下文件:
38+
- `lcov.info`: LCOV 格式的覆盖率数据
39+
- `coverage-summary.json`: JSON 格式的覆盖率摘要
40+
- `lcov-report/`: HTML 格式的详细覆盖率报告
41+
- `coverage-final.json`: 完整的覆盖率数据
42+
43+
## ⚙️ 配置修改
44+
45+
### 修改覆盖率阈值
46+
编辑 `jest.config.js` 文件中的 `coverageThreshold` 部分:
47+
48+
```javascript
49+
coverageThreshold: {
50+
global: {
51+
branches: 80, // 修改分支覆盖率阈值
52+
functions: 80, // 修改函数覆盖率阈值
53+
lines: 80, // 修改行覆盖率阈值
54+
statements: 80 // 修改语句覆盖率阈值
55+
}
56+
}
57+
```
58+
59+
### 修改触发分支
60+
编辑 `.github/workflows/test-coverage.yaml` 文件中的 `on` 部分:
61+
62+
```yaml
63+
on:
64+
push:
65+
branches: [ main, develop, feature/* ] # 添加更多分支
66+
pull_request:
67+
branches: [ main, develop ]
68+
```
69+
70+
## 🔍 查看报告
71+
72+
1. **GitHub Actions 页面**: 在 Actions 标签页查看测试运行状态和覆盖率摘要
73+
2. **Pull Request 评论**: 每个 PR 都会自动添加覆盖率报告评论
74+
3. **下载详细报告**: 在 Actions 运行页面下载 `coverage-report` 工件
75+
76+
## 🚨 故障排除
77+
78+
### 测试失败
79+
如果覆盖率测试失败,请检查:
80+
1. 测试是否正常运行:`npm test`
81+
2. 覆盖率是否达到阈值
82+
3. 是否有未测试的代码文件
83+
84+
### 无法生成报告
85+
确保:
86+
1. Jest 配置正确
87+
2. 测试文件路径匹配 `testMatch` 模式
88+
3. 源代码路径在 `collectCoverageFrom` 中正确配置

jest.config.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,27 @@ module.exports = {
77
transform: {
88
'^.+\\.ts$': 'ts-jest',
99
},
10-
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!**/node_modules/**'],
10+
collectCoverageFrom: [
11+
'<rootDir>/src/**/*.ts',
12+
'!**/node_modules/**',
13+
'!<rootDir>/src/**/*.d.ts',
14+
'!<rootDir>/src/**/__tests__/**',
15+
'!<rootDir>/src/**/test/**'
16+
],
17+
coverageDirectory: 'coverage',
18+
coverageReporters: [
19+
'text',
20+
'text-summary',
21+
'lcov',
22+
'html',
23+
'json-summary'
24+
],
25+
coverageThreshold: {
26+
global: {
27+
branches: 70,
28+
functions: 70,
29+
lines: 70,
30+
statements: 70
31+
}
32+
}
1133
};

0 commit comments

Comments
 (0)