Skip to content

Commit

Permalink
test: report coverage as HTML (#938)
Browse files Browse the repository at this point in the history
* test: report coverage as HTML

* test: remove upload to Codecov

* ci: show coverage in test job summary

* ci: show coverage in test job summary

* test: enforce minimal coverage

* ci: show coverage in test job summary

* ci: pin major tsx version

* ci: post coverage as comment to a PR

* Revert "ci: post coverage as comment to a PR"

This reverts commit 2f13b9f.
  • Loading branch information
MikkCZ authored Oct 6, 2024
1 parent 7197c06 commit 5eacfee
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,16 @@ jobs:
uses: ./.github/actions/prepare
- name: Run tests
run: make test_in_container
- name: Upload code coverage to Codecov
uses: codecov/codecov-action@v4
- name: Upload code coverage report
uses: actions/upload-artifact@v4
with:
name: coverage
path: coverage
retention-days: 30
- name: Show code coverage in summary
run: |
summary="$(npx -y tsx@4 scripts/test-coverage-github-summary.ts)"
echo "${summary}" >> $GITHUB_STEP_SUMMARY
generated_code_consistency:
name: Verify generated code is consistent
needs: prepare_cache
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Pontoon Add-on

[![Build](https://github.com/MikkCZ/pontoon-addon/actions/workflows/build.yml/badge.svg)](https://github.com/MikkCZ/pontoon-addon/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/MikkCZ/pontoon-addon/branch/main/graph/badge.svg?token=wV84O1ujms)](https://codecov.io/gh/MikkCZ/pontoon-addon)

[![Mozilla Add-on](https://img.shields.io/amo/v/pontoon-tools.svg?label=Mozilla%20Add-ons&color=informational)](https://addons.mozilla.org/firefox/addon/pontoon-tools/)
[![Mozilla Add-on](https://img.shields.io/amo/users/pontoon-tools.svg?label=users&color=informational)](https://addons.mozilla.org/firefox/addon/pontoon-tools/statistics/)
Expand Down
9 changes: 9 additions & 0 deletions configs/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const config: Config.InitialOptions = {
'/generated/',
'.+\\.d.ts$',
],
coverageReporters: ['clover', 'json', 'json-summary', 'lcov', 'html'],
coverageThreshold: {
global: {
lines: 50,
statements: 50,
functions: 45,
branches: 30,
},
},
preset: 'ts-jest',
transform: {
'.+\\.jsx?$': ['babel-jest', { presets: [ '@babel/env' ] }],
Expand Down
59 changes: 59 additions & 0 deletions scripts/test-coverage-github-summary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fs from 'fs';
import path from 'path';

interface CoverageSummaryItem {
total: number,
covered: number,
skipped: number,
pct: number,
}

interface CoverageSummary {
total: {
lines: CoverageSummaryItem,
statements: CoverageSummaryItem,
functions: CoverageSummaryItem,
branches: CoverageSummaryItem,
branchesTrue: CoverageSummaryItem,
},
}

function formatCoverageDetailName(key: string): string {
return key.split(/(?=[A-Z])/)
.map(word => word.toLowerCase())
.join(' ');
}

function formatCoveragePct(pct: CoverageSummaryItem['pct']): string {
return `${pct.toFixed(2)} %`;
}

const coverageSummary = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../coverage/coverage-summary.json'), {
encoding: 'utf-8',
}),
) as CoverageSummary;

const itemsToAverage: Array<keyof CoverageSummary['total']> = [
'lines',
'statements',
'functions',
'branches',
];

const output = `
## Code coverage: ${formatCoveragePct(
itemsToAverage
.map(itemKey => coverageSummary.total[itemKey].pct)
.reduce((acc, curr) => acc + curr, 0) / itemsToAverage.length
)}
### Details:
${
Object.entries(coverageSummary.total)
.map(([key, {pct}]) => `- ${formatCoverageDetailName(key)}: ${formatCoveragePct(pct)}`)
.join('\n')
}
`.trim();

process.stdout.write(`${output}\n`);

0 comments on commit 5eacfee

Please sign in to comment.