Skip to content

Commit 9140a00

Browse files
committed
Try using a default import for the coverage reporter
1 parent 4873bfb commit 9140a00

File tree

3 files changed

+161
-3
lines changed

3 files changed

+161
-3
lines changed
Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,159 @@
1-
"use strict";var b=Object.create;var o=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty;var S=(t,e)=>{for(var n in e)o(t,n,{get:e[n],enumerable:!0})},p=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of y(e))!k.call(t,s)&&s!==n&&o(t,s,{get:()=>e[s],enumerable:!(r=g(e,s))||r.enumerable});return t};var f=(t,e,n)=>(n=t!=null?b(v(t)):{},p(e||!t||!t.__esModule?o(n,"default",{value:t,enumerable:!0}):n,t)),E=t=>p(o({},"__esModule",{value:!0}),t);var F={};S(F,{default:()=>a});module.exports=E(F);var d=f(require("fs"),1),w=f(require("istanbul-lib-report"),1);function R(t){return t.statements.pct===100&&t.branches.pct===100&&t.functions.pct===100&&t.lines.pct===100}function L(t){if(t.isSummary())return[];let e=t.getCoverageSummary(!1),r=e.isEmpty()?0:e.lines.pct,s,m=t.getFileCoverage();if(r===100){let i=m.getBranchCoverageByLine();s=Object.entries(i).map(([c,{coverage:u}])=>[c,u===100])}else s=Object.entries(m.getLineCoverage());let l=!0;return s.reduce((i,[c,u])=>{if(u)l=!0;else{let h=parseInt(c);l?(i.push([h]),l=!1):i[i.length-1][1]=h}return i},[])}var C=["Statements","Branches","Functions","Lines"],a=class extends w.ReportBase{skipEmpty;skipFull;results={};cw=null;watermarks=null;constructor(e){super(e),this.skipEmpty=!!e.skipEmpty,this.skipFull=!!e.skipFull}onStart(e,n){if(!process.env.GITHUB_STEP_SUMMARY){console.log("Reporter not being executed in Github Actions environment");return}this.cw=d.default.createWriteStream(process.env.GITHUB_STEP_SUMMARY,{encoding:"utf-8",flags:"a"}),this.watermarks=n.watermarks,this.cw.write("<h2>Test Coverage</h2>"),this.cw.write("<table><thead><tr>");for(let r of["File",...C,"Uncovered Lines"])this.cw.write(`<th>${r}</th>`);this.cw.write("</tr></thead><tbody>")}onSummary(e){let n=e.getRelativeName()||"All Files",r=e.getCoverageSummary(!1),s=r.isEmpty();this.skipEmpty&&s||this.skipFull&&R(r)||(this.results[n]={statements:s?0:r.statements.pct,branches:s?0:r.branches.pct,functions:s?0:r.functions.pct,lines:s?0:r.lines.pct,uncoveredLines:L(e)})}onDetail(e){return this.onSummary(e)}formatter(e,n){if(!this.watermarks||this.watermarks[n]===void 0)return`<td>${e}%</td>`;let[r,s]=this.watermarks[n];return e<r?`<td><p style="color:red">${e}%</p></td>`:e>s?`<td><p style="color:green">${e}%</p></td>`:`<td><p style="color:yellow">${e}%</p></td>`}onEnd(){if(!this.cw)return;let e=Object.keys(this.results).sort();for(let n of e){let r=this.results[n];if(this.cw.write(`<tr><td><code>${n}</code></td>`),this.cw.write(this.formatter(r.statements,"statements")),this.cw.write(this.formatter(r.branches,"branches")),this.cw.write(this.formatter(r.functions,"functions")),this.cw.write(this.formatter(r.lines,"lines")),r.uncoveredLines.length>0){this.cw.write("<td><details><summary>Expand</summary><ul>");for(let s of r.uncoveredLines)s.length===1?this.cw.write(`<li>${s[0]}</li>`):this.cw.write(`<li>${s[0]}-${s[1]}</li>`);this.cw.write("</ul></details></td>")}else this.cw.write("<td></td>");this.cw.write("</tr>")}this.cw.write("</tbody></table>"),this.cw.close()}};
1+
"use strict";
2+
var __create = Object.create;
3+
var __defProp = Object.defineProperty;
4+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5+
var __getOwnPropNames = Object.getOwnPropertyNames;
6+
var __getProtoOf = Object.getPrototypeOf;
7+
var __hasOwnProp = Object.prototype.hasOwnProperty;
8+
var __export = (target, all) => {
9+
for (var name in all)
10+
__defProp(target, name, { get: all[name], enumerable: true });
11+
};
12+
var __copyProps = (to, from, except, desc) => {
13+
if (from && typeof from === "object" || typeof from === "function") {
14+
for (let key of __getOwnPropNames(from))
15+
if (!__hasOwnProp.call(to, key) && key !== except)
16+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17+
}
18+
return to;
19+
};
20+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21+
// If the importer is in node compatibility mode or this is not an ESM
22+
// file that has been converted to a CommonJS file using a Babel-
23+
// compatible transform (i.e. "__esModule" has not been set), then set
24+
// "default" to the CommonJS "module.exports" for node compatibility.
25+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26+
mod
27+
));
28+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29+
30+
// src/coverage-reporter.ts
31+
var coverage_reporter_exports = {};
32+
__export(coverage_reporter_exports, {
33+
default: () => GithubActionsCoverageReporter
34+
});
35+
module.exports = __toCommonJS(coverage_reporter_exports);
36+
var import_fs = __toESM(require("fs"), 1);
37+
var import_istanbul_lib_report = __toESM(require("istanbul-lib-report"), 1);
38+
function isFull(metrics) {
39+
return metrics.statements.pct === 100 && metrics.branches.pct === 100 && metrics.functions.pct === 100 && metrics.lines.pct === 100;
40+
}
41+
function getUncoveredLines(node) {
42+
if (node.isSummary()) {
43+
return [];
44+
}
45+
const metrics = node.getCoverageSummary(false);
46+
const isEmpty = metrics.isEmpty();
47+
const lines = isEmpty ? 0 : metrics.lines.pct;
48+
let coveredLines;
49+
const fileCoverage = node.getFileCoverage();
50+
if (lines === 100) {
51+
const branches = fileCoverage.getBranchCoverageByLine();
52+
coveredLines = Object.entries(branches).map(([key, { coverage }]) => [key, coverage === 100]);
53+
} else {
54+
coveredLines = Object.entries(fileCoverage.getLineCoverage());
55+
}
56+
let newRange = true;
57+
const ranges = coveredLines.reduce((acum, [line, hit]) => {
58+
if (hit) {
59+
newRange = true;
60+
} else {
61+
const linenum = parseInt(line);
62+
if (newRange) {
63+
acum.push([linenum]);
64+
newRange = false;
65+
} else {
66+
acum[acum.length - 1][1] = linenum;
67+
}
68+
}
69+
return acum;
70+
}, []);
71+
return ranges;
72+
}
73+
var headers = ["Statements", "Branches", "Functions", "Lines"];
74+
var GithubActionsCoverageReporter = class extends import_istanbul_lib_report.default.ReportBase {
75+
skipEmpty;
76+
skipFull;
77+
results = {};
78+
cw = null;
79+
watermarks = null;
80+
constructor(opts) {
81+
super(opts);
82+
this.skipEmpty = Boolean(opts.skipEmpty);
83+
this.skipFull = Boolean(opts.skipFull);
84+
}
85+
onStart(_node, context) {
86+
if (!process.env.GITHUB_STEP_SUMMARY) {
87+
console.log("Reporter not being executed in Github Actions environment");
88+
return;
89+
}
90+
this.cw = import_fs.default.createWriteStream(process.env.GITHUB_STEP_SUMMARY, { encoding: "utf-8", flags: "a" });
91+
this.watermarks = context.watermarks;
92+
this.cw.write("<h2>Test Coverage</h2>");
93+
this.cw.write("<table><thead><tr>");
94+
for (const heading of ["File", ...headers, "Uncovered Lines"]) {
95+
this.cw.write(`<th>${heading}</th>`);
96+
}
97+
this.cw.write("</tr></thead><tbody>");
98+
}
99+
onSummary(node) {
100+
const nodeName = node.getRelativeName() || "All Files";
101+
const rawMetrics = node.getCoverageSummary(false);
102+
const isEmpty = rawMetrics.isEmpty();
103+
if (this.skipEmpty && isEmpty) {
104+
return;
105+
}
106+
if (this.skipFull && isFull(rawMetrics)) {
107+
return;
108+
}
109+
this.results[nodeName] = {
110+
statements: isEmpty ? 0 : rawMetrics.statements.pct,
111+
branches: isEmpty ? 0 : rawMetrics.branches.pct,
112+
functions: isEmpty ? 0 : rawMetrics.functions.pct,
113+
lines: isEmpty ? 0 : rawMetrics.lines.pct,
114+
uncoveredLines: getUncoveredLines(node)
115+
};
116+
}
117+
onDetail(node) {
118+
return this.onSummary(node);
119+
}
120+
formatter(pct, watermark) {
121+
if (!this.watermarks || this.watermarks[watermark] === void 0) return `<td>${pct}%</td>`;
122+
const [low, high] = this.watermarks[watermark];
123+
if (pct < low) {
124+
return `<td><p style="color:red">${pct}%</p></td>`;
125+
}
126+
if (pct > high) {
127+
return `<td><p style="color:green">${pct}%</p></td>`;
128+
}
129+
return `<td><p style="color:yellow">${pct}%</p></td>`;
130+
}
131+
onEnd() {
132+
if (!this.cw) return;
133+
const fileNames = Object.keys(this.results).sort();
134+
for (const fileName of fileNames) {
135+
const metrics = this.results[fileName];
136+
this.cw.write(`<tr><td><code>${fileName}</code></td>`);
137+
this.cw.write(this.formatter(metrics.statements, "statements"));
138+
this.cw.write(this.formatter(metrics.branches, "branches"));
139+
this.cw.write(this.formatter(metrics.functions, "functions"));
140+
this.cw.write(this.formatter(metrics.lines, "lines"));
141+
if (metrics.uncoveredLines.length > 0) {
142+
this.cw.write("<td><details><summary>Expand</summary><ul>");
143+
for (const range of metrics.uncoveredLines) {
144+
if (range.length === 1) {
145+
this.cw.write(`<li>${range[0]}</li>`);
146+
} else {
147+
this.cw.write(`<li>${range[0]}-${range[1]}</li>`);
148+
}
149+
}
150+
this.cw.write("</ul></details></td>");
151+
} else {
152+
this.cw.write("<td></td>");
153+
}
154+
this.cw.write("</tr>");
155+
}
156+
this.cw.write("</tbody></table>");
157+
this.cw.close();
158+
}
159+
};

lib/vitest-reporter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"scripts": {
2121
"build": "yarn build:coverage-reporter && yarn build:test-reporter",
22-
"build:coverage-reporter": "esbuild --outfile=./build/coverage-reporter.cjs --format=cjs --bundle=true --minify --external:\"istanbul-lib-report\" --platform=node src/coverage-reporter.ts",
22+
"build:coverage-reporter": "esbuild --outfile=./build/coverage-reporter.cjs --format=cjs --bundle=true --minify=false --external:\"istanbul-lib-report\" --platform=node src/coverage-reporter.ts",
2323
"build:test-reporter": "esbuild --outfile=./build/test-reporter.js --format=esm --bundle=true --minify --external:\"vitest*\" --platform=node src/test-reporter.ts",
2424
"test": "vitest"
2525
}

lib/vitest-reporter/src/coverage-reporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import fs from 'fs';
88
import type { CoverageSummary } from 'istanbul-lib-coverage';
9-
import * as report from 'istanbul-lib-report';
9+
import report from 'istanbul-lib-report';
1010

1111
/**
1212
* Determines if the coverage summary has full coverage

0 commit comments

Comments
 (0)