Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 9x 9x 9x 9x 9x 9x 9x 10x 10x 10x 10x 10x 10x 10x 10x 13x 10x 10x 18x 18x 9x 9x 9x 9x 18x 18x 18x 18x 18x 18x 18x 12x 96x 96x 24x 24x 24x 15x 9x 9x 9x 9x | /*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import fs from "node:fs";
import path from "node:path";
import type { Totals } from "@vitest/istanbul-lib-coverage";
import type { Context, FileWriter, ReportNode } from "../../index";
import ReportBase from "../../report-base";
import HtmlReport from "../html/index";
import type { HtmlOptions, LinkMapper } from "../html/index";
/** options accepted by {@link HtmlSpaReport} */
export interface HtmlSpaOptions extends Omit<HtmlOptions, "theme"> {
/** the metrics to show in the report UI, defaults to lines, branches and functions */
metricsToShow?: ("lines" | "branches" | "functions" | "statements")[];
}
/** a single metric as embedded into the SPA's `window.data` */
interface SpaMetric {
total: number;
covered: number;
skipped: number;
missed: number;
pct: number | "Unknown";
classForPercent: string;
}
/** the recursive node structure embedded into the SPA's `window.data` */
interface SpaDataStructure {
file: string;
isEmpty: boolean;
metrics: Record<"statements" | "branches" | "functions" | "lines", SpaMetric>;
children: false | SpaDataStructure[];
}
const standardLinkMapper: LinkMapper = {
getPath(node) {
Iif (typeof node === "string") {
return node;
}
let filePath = node.getQualifiedName();
if (node.isSummary()) {
Iif (filePath !== "") {
filePath += "/index.html";
} else {
filePath = "index.html";
}
} else E{
filePath += ".html";
}
return filePath;
},
relativePath(source, target) {
const targetPath = this.getPath(target);
const sourcePath = path.dirname(this.getPath(source));
return path.relative(sourcePath, targetPath);
},
assetPath(node, name) {
return this.relativePath(this.getPath(node), name);
},
};
class HtmlSpaReport extends ReportBase {
declare verbose: boolean;
declare linkMapper: LinkMapper;
declare subdir: string;
declare date: string;
declare skipEmpty: boolean | undefined;
declare htmlReport: HtmlReport;
declare metricsToShow: ("lines" | "branches" | "functions" | "statements")[];
constructor(opts: HtmlSpaOptions = {}) {
super({
// force the summarizer to nested for html-spa
summarizer: "nested",
});
this.verbose = opts.verbose || false;
this.linkMapper = opts.linkMapper || standardLinkMapper;
this.subdir = opts.subdir || "";
this.date = Date();
this.skipEmpty = opts.skipEmpty;
this.htmlReport = new HtmlReport(opts);
this.htmlReport.getBreadcrumbHtml = function () {
return '<a href="javascript:history.back()">Back</a>';
};
// the SPA's own pages are not themed, so its file pages keep matching them
this.htmlReport.theme = "legacy";
this.metricsToShow = opts.metricsToShow || ["lines", "branches", "functions"];
}
getWriter(context: Context): FileWriter {
Eif (!this.subdir) {
return context.writer;
}
return context.writer.writerForDir(this.subdir);
}
onStart(root: ReportNode, context: Context): void {
this.htmlReport.onStart(root, context);
const writer = this.getWriter(context);
const srcDir = path.resolve(import.meta.dirname, "./assets");
fs.readdirSync(srcDir).forEach((f) => {
const resolvedSource = path.resolve(srcDir, f);
const resolvedDestination = ".";
const stat = fs.statSync(resolvedSource);
let dest;
Eif (stat.isFile()) {
dest = resolvedDestination + "/" + f;
Iif (this.verbose) {
console.log("Write asset: " + dest);
}
writer.copyFile(resolvedSource, dest);
}
});
}
onDetail(node: ReportNode, context: Context): void {
this.htmlReport.onDetail(node, context);
}
getMetric(metric: Totals, type: string, context: Context): SpaMetric {
const isEmpty = metric.total === 0;
return {
total: metric.total,
covered: metric.covered,
skipped: metric.skipped,
missed: metric.total - metric.covered,
pct: isEmpty ? 0 : metric.pct,
classForPercent: isEmpty
? "empty"
: context.classForPercent(type, typeof metric.pct === "number" ? metric.pct : NaN),
};
}
toDataStructure(node: ReportNode, context: Context): SpaDataStructure {
const coverageSummary = node.getCoverageSummary()!;
const metrics = {
statements: this.getMetric(coverageSummary.statements, "statements", context),
branches: this.getMetric(coverageSummary.branches, "branches", context),
functions: this.getMetric(coverageSummary.functions, "functions", context),
lines: this.getMetric(coverageSummary.lines, "lines", context),
};
return {
file: node.getRelativeName(),
isEmpty: coverageSummary.isEmpty(),
metrics,
children:
node.isSummary() && node.getChildren().map((child) => this.toDataStructure(child, context)),
};
}
onEnd(rootNode: ReportNode, context: Context): void {
const data = this.toDataStructure(rootNode, context);
const cw = this.getWriter(context).writeFile(this.linkMapper.getPath(rootNode));
cw.write(
`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="spa.css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="app" class="app"></div>
<script>
window.data = ${JSON.stringify(data)};
window.generatedDatetime = ${JSON.stringify(String(Date()))};
window.metricsToShow = ${JSON.stringify(this.metricsToShow)};
</script>
<script src="bundle.js"></script>
</body>
</html>`,
);
cw.close();
}
}
export default HtmlSpaReport;
|