Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions .github/workflows/coverage-experiment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: Coverage Delta

on:
pull_request:
branches: [main]

jobs:
compare-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v3
with:
node-version: 24

- name: Checkout Main
uses: actions/checkout@v4
with:
ref: main

- name: Install & Test (Main)
run: |
npm ci
npm run coverage
mv ./coverage/coverage-summary.json ./coverage-main-summary.json

- name: Checkout PR Branch
uses: actions/checkout@v4
with:
clean: true

- name: Install & Test (PR)
run: |
npm ci
npm run coverage

- name: Report Coverage Delta
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');

const loadSummary = (path) => {
try {
return JSON.parse(fs.readFileSync(path, 'utf8'));
} catch (error) {
console.log(`Could not load ${path}: ${error.message}`);
return null;
}
};

const mainSummary = loadSummary('./coverage-main-summary.json');
const prSummary = loadSummary('./coverage/coverage-summary.json');

if (!mainSummary || !prSummary) {
core.setFailed('Could not load coverage summaries for comparison.');
return;
}

const metrics = ['lines', 'statements', 'functions', 'branches'];

const getPct = (summary, metric) => summary.total[metric].pct;

let markdown = `### 🧪 Code Coverage Delta\n\n`;
markdown += `| Metric | Main | PR | Delta |\n`;
markdown += `| :--- | :---: | :---: | :---: |\n`;

metrics.forEach(metric => {
const oldPct = getPct(mainSummary, metric);
const newPct = getPct(prSummary, metric);
const diff = (newPct - oldPct).toFixed(2);

let icon = '';
if (diff > 0) icon = '🟢';
else if (diff < 0) icon = '🔴';
else icon = '⚪️';

// Format with + sign for positive numbers
const diffStr = diff > 0 ? `+${diff}%` : `${diff}%`;

markdown += `| **${metric}** | ${oldPct}% | ${newPct}% | ${icon} ${diffStr} |\n`;
});

markdown += `\n_Generated by c8 and GitHub Actions_`;

const { owner, repo } = context.repo;
const issue_number = context.issue.number;

const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number,
});

const botComment = comments.data.find(c =>
c.body.includes('### 🧪 Code Coverage Delta') &&
c.user.type === 'Bot'
);

if (botComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: botComment.id,
body: markdown
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: markdown
});
}
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
include: ['test/**/*.spec.ts'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
reporter: ['text', 'json', 'html', 'json-summary'],
},
},
});
Loading