11/**
2- * Aggregates Jest coverage-summary.json files from all workspace packages
3- * and outputs a GitHub Actions Job Summary.
2+ * Aggregates Jest coverage-summary.json files from all workspace packages.
3+ *
4+ * Generates:
5+ * 1. GitHub Actions Job Summary (if running in CI)
6+ * 2. Combined coverage-summary.json for PR comment actions
47 */
58
69const fs = require ( 'fs' ) ;
@@ -101,6 +104,49 @@ function generateSummary(results) {
101104 return md ;
102105}
103106
107+ /**
108+ * Combines multiple coverage summaries into a single coverage-summary.json
109+ * that matches Jest's format. Used by coverage comment actions to display
110+ * aggregated coverage across all monorepo packages.
111+ */
112+ function generateCombinedCoverageSummary ( results ) {
113+ if ( results . length === 0 ) {
114+ return null ;
115+ }
116+
117+ const metrics = [ 'lines' , 'statements' , 'functions' , 'branches' ] ;
118+ const combined = {
119+ total : { } ,
120+ } ;
121+
122+ // Calculate weighted averages across all packages
123+ metrics . forEach ( ( metric ) => {
124+ let totalCount = 0 ;
125+ let coveredCount = 0 ;
126+ let skippedCount = 0 ;
127+
128+ results . forEach ( ( { summary } ) => {
129+ const m = summary [ metric ] ;
130+ if ( m && typeof m . total === 'number' ) {
131+ totalCount += m . total ;
132+ coveredCount += m . covered || 0 ;
133+ skippedCount += m . skipped || 0 ;
134+ }
135+ } ) ;
136+
137+ const pct = totalCount > 0 ? ( coveredCount / totalCount ) * 100 : 0 ;
138+
139+ combined . total [ metric ] = {
140+ total : totalCount ,
141+ covered : coveredCount ,
142+ skipped : skippedCount ,
143+ pct : Math . round ( pct * 100 ) / 100 , // Round to 2 decimal places
144+ } ;
145+ } ) ;
146+
147+ return combined ;
148+ }
149+
104150function main ( ) {
105151 const results = readAllCoverage ( ) ;
106152
@@ -114,6 +160,16 @@ function main() {
114160 core . summary . addRaw ( ghMd ) . write ( ) ;
115161 console . info ( '✅ Coverage summary written to GitHub Actions job summary' ) ;
116162 }
163+
164+ // 3) Generate combined coverage-summary.json for PR comment actions
165+ const combined = generateCombinedCoverageSummary ( results ) ;
166+ if ( combined ) {
167+ const outputPath = path . join ( COVERAGE_ROOT , 'coverage-summary.json' ) ;
168+ fs . writeFileSync ( outputPath , JSON . stringify ( combined , null , 2 ) , 'utf8' ) ;
169+ console . info ( `✅ Combined coverage-summary.json written to ${ outputPath } ` ) ;
170+ } else {
171+ console . warn ( '⚠️ No coverage data to combine' ) ;
172+ }
117173}
118174
119175if ( require . main === module ) {
0 commit comments