11# Vitest Coverage Reporter GitHub Action
22
3- A GitHub Action that automatically creates beautiful coverage reports in pull requests using vitest coverage data.
3+ A GitHub Action that automatically creates beautiful coverage reports in pull requests using vitest coverage data. Built with pure YAML and shell commands for maximum reliability.
44
55## Features
66
@@ -9,7 +9,9 @@ A GitHub Action that automatically creates beautiful coverage reports in pull re
99- 📁 ** File-level Details** : Option to show individual file coverage breakdowns
1010- 🔄 ** Smart Updates** : Updates existing comments instead of creating duplicates
1111- 🎨 ** Visual Indicators** : Uses emojis to quickly identify coverage status
12- - ⚡ ** Lightweight** : Fast execution with minimal dependencies
12+ - 🏷️ ** Coverage Badges** : Generates shields.io compatible badges locally
13+ - ⚡ ** Lightweight** : Pure YAML and shell commands, no JavaScript complexity
14+ - 🔧 ** Reliable** : No module system issues or permission headaches
1315
1416## Usage
1517
@@ -45,16 +47,19 @@ jobs:
4547 uses : glideapps/vitest-v8-json-coverage-summary@v1
4648` ` `
4749
48- ### Advanced Usage
50+ ### Advanced Usage with Badge Upload
4951
5052` ` ` yaml
51- name : Coverage Report
53+ name : Coverage Report with Badge Upload
5254on :
5355 pull_request :
5456 branches : [main]
57+ push :
58+ branches : [main]
5559
5660permissions :
5761 pull-requests : write
62+ contents : write # Required for GitHub Pages upload
5863
5964jobs :
6065 coverage :
@@ -73,24 +78,56 @@ jobs:
7378 - name : Run tests with coverage
7479 run : npm test
7580
81+ # Coverage reporting (runs on PRs and main)
7682 - name : Report Coverage
77837884 with :
7985 coverage-file : " coverage/coverage-summary.json"
8086 title : " 🧪 Test Coverage Report"
8187 show-files : " true"
8288 coverage-threshold : " 90"
89+ make-badges : " true"
90+
91+ # Badge upload (only runs on main branch)
92+ upload-badges :
93+ if : github.ref == 'refs/heads/main'
94+ runs-on : ubuntu-latest
95+ needs : coverage
96+ steps :
97+ - uses : actions/checkout@v4
98+ with :
99+ fetch-depth : 0 # Needed for git operations
100+
101+ - name : Upload Badges to GitHub Pages
102+ 103+ with :
104+ action : " badge-upload-action.yml"
105+ badges-dir : " badges"
106+ pages-branch : " gh-pages"
107+ pages-badges-dir : " badges"
83108` ` `
84109
85110## Inputs
86111
87- | Input | Description | Required | Default |
88- | -------------------- | ------------------------------------------------------- | -------- | -------------------------------- |
89- | ` coverage-file` | Path to the coverage summary JSON file | No | `coverage/coverage-summary.json` |
90- | `token` | GitHub token for creating comments | No | `${{ github.token }}` |
91- | `title` | Title for the coverage report comment | No | `📊 Coverage Report` |
92- | `show-files` | Whether to show individual file coverage details | No | `true` |
93- | `coverage-threshold` | Minimum coverage percentage to consider as good (0-100) | No | `80` |
112+ | Input | Description | Required | Default |
113+ | -------------------- | --------------------------------------------------------- | -------- | -------------------------------- |
114+ | ` coverage-file` | Path to the coverage summary JSON file | No | `coverage/coverage-summary.json` |
115+ | `token` | GitHub token for creating comments | No | `${{ github.token }}` |
116+ | `title` | Title for the coverage report comment | No | `📊 Coverage Report` |
117+ | `show-files` | Whether to show individual file coverage details | No | `true` |
118+ | `coverage-threshold` | Minimum coverage percentage to consider as good (0-100) | No | `80` |
119+ | `make-badges` | Whether to generate coverage badges in a badges directory | No | `true` |
120+
121+ # # Badge Upload Action
122+
123+ For uploading badges to GitHub Pages, use the separate `badge-upload-action.yml` :
124+
125+ | Input | Description | Required | Default |
126+ | ------------------ | ------------------------------------------------- | -------- | ---------------------------------- |
127+ | `badges-dir` | Directory containing the badges to upload | No | `badges` |
128+ | `pages-branch` | Branch to upload badges to for GitHub Pages | No | `gh-pages` |
129+ | `pages-badges-dir` | Directory within the pages branch to store badges | No | `badges` |
130+ | `commit-message` | Commit message for badge updates | No | `Update coverage badges [skip ci]` |
94131
95132# # Coverage File Format
96133
@@ -182,3 +219,98 @@ Contributions are welcome! Please feel free to submit a Pull Request.
182219## License
183220
184221MIT License - see the [ LICENSE] ( LICENSE ) file for details.
222+
223+ ## Coverage Badges
224+
225+ When ` make-badges ` is enabled (default: ` true ` ), the action creates a ` badges ` directory with shields.io compatible JSON files:
226+
227+ ### Generated Badges
228+
229+ - ` badges/coverage.json ` - Overall coverage percentage
230+ - ` badges/statements.json ` - Statement coverage
231+ - ` badges/branches.json ` - Branch coverage
232+ - ` badges/functions.json ` - Function coverage
233+ - ` badges/lines.json ` - Line coverage
234+
235+ ### Badge Colors
236+
237+ - 🟢 ** Bright Green** : 90% or higher
238+ - 🟢 ** Green** : 80-89%
239+ - 🟡 ** Yellow** : 70-79%
240+ - 🟠 ** Orange** : 60-69%
241+ - 🔴 ** Red** : Below 60%
242+
243+ ### Using Badges with GitHub Pages
244+
245+ The badge upload action automatically uploads badges to your GitHub Pages branch.
246+
247+ ** Automatic Setup:**
248+
249+ - Creates the ` gh-pages ` branch if it doesn't exist
250+ - Uploads badges to the specified directory
251+ - Commits and pushes changes automatically
252+
253+ ** Enable GitHub Pages** in your repository settings:
254+
255+ 1 . Go to Settings → Pages
256+ 2 . Set source to "Deploy from a branch"
257+ 3 . Select your ` gh-pages ` branch (or the branch specified in ` pages-branch ` )
258+ 4 . Set folder to ` / (root) ` or ` /badges ` depending on your preference
259+
260+ ** Badge URLs** will be available at:
261+
262+ ```
263+ https://yourusername.github.io/yourrepo/badges/coverage.json
264+ https://yourusername.github.io/yourrepo/badges/statements.json
265+ https://yourusername.github.io/yourrepo/badges/branches.json
266+ https://yourusername.github.io/yourrepo/badges/functions.json
267+ https://yourusername.github.io/yourrepo/badges/lines.json
268+ ```
269+
270+ ** Use in your README.md:**
271+
272+ ``` markdown
273+ 
274+ 
275+ 
276+ ```
277+
278+ ### Required Permissions
279+
280+ For basic functionality, your workflow needs:
281+
282+ ``` yaml
283+ permissions :
284+ pull-requests : write # For creating/updating PR comments
285+ ` ` `
286+
287+ For GitHub Pages deployment, add:
288+
289+ ` ` ` yaml
290+ permissions :
291+ contents : write # For uploading to GitHub Pages
292+ ` ` `
293+
294+ ## How It Works
295+
296+ The action is built as a **composite action** using pure YAML and shell commands:
297+
298+ 1. **File Validation** - Checks if coverage file exists
299+ 2. **Badge Generation** - Creates shields.io compatible JSON badges
300+ 3. **Report Generation** - Builds formatted markdown coverage report
301+ 4. **PR Comment** - Posts report to pull request using GitHub API
302+
303+ ### Shell Tools Used
304+
305+ - **` jq`** - JSON parsing and data extraction
306+ - **`bc`** - Mathematical calculations
307+ - **`curl`** - GitHub API communication
308+ - **`bash`** - Scripting and logic
309+
310+ # ## No More JavaScript Headaches
311+
312+ - ✅ No ESM/CJS confusion
313+ - ✅ No module system warnings
314+ - ✅ No permission issues with git operations
315+ - ✅ No complex dependency management
316+ - ✅ No build/compilation steps
0 commit comments