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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | 17x 9x 17x 68x 17x 17x 17x 17x 51x 17x 17x 17x 17x 17x 144x 144x 12x 144x 12x 9x 9x 9x 9x 425x 272x 153x 153x 45x 45x 108x 153x 136x 136x 136x 136x 14x 14x 14x 14x 14x 14x 14x 4x 4x 4x 4x 4x 4x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 45x 45x 14x 14x 28x 28x 28x 154x 154x 154x 154x 140x 140x 140x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x | /*
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 { CoverageSummary, Totals } from "@vitest/istanbul-lib-coverage";
import * as html from "../../html-escape";
import type { Context, FileWriter, ReportNode } from "../../index";
import ReportBase from "../../report-base";
import annotator from "./annotator";
import type { AnnotatedData } from "./annotator";
/** the four coverage metrics reported per node */
type MetricKey = "statements" | "branches" | "functions" | "lines";
/** maps report nodes to output paths, see the html report's `linkMapper` option */
export interface LinkMapper {
getPath(node: ReportNode | string): string;
relativePath(source: ReportNode | string, target: ReportNode | string): string;
assetPath(node: ReportNode, name: string): string;
}
/** color themes of the html report, `legacy` being the classic istanbul colors */
export type HtmlTheme = "light" | "dark" | "legacy";
/** options accepted by {@link HtmlReport} */
export interface HtmlOptions {
/** show extra logging while the report is generated */
verbose?: boolean;
/** maps report nodes to output paths */
linkMapper?: LinkMapper;
/** subdirectory (under the report dir) to write the report to */
subdir?: string;
/** skip nodes with no coverage */
skipEmpty?: boolean;
/**
* fixes the report's theme and hides the theme picker; when unset, viewers
* pick a theme in the report, which otherwise follows their light/dark
* preference, or the page embedding the report, like Vitest UI
*/
theme?: HtmlTheme;
}
/** data threaded through the header/footer templates while rendering a page */
interface TemplateData {
datetime: string;
fixedTheme: HtmlTheme | undefined;
entity: string;
metrics: CoverageSummary;
reportClass: string;
pathHtml: string;
base: { css: string };
sorter: { js: string; image: string };
blockNavigation: { js: string };
theme: { js: string };
prettify: { js: string; css: string };
favicon: string;
}
/** data for a single row of a summary page */
interface SummaryLineData {
reportClasses: Record<MetricKey, string>;
metrics: CoverageSummary;
file: string;
output: string;
}
/** narrows a possibly-`"Unknown"` pct before handing it to `classForPercent` */
function classForPercent(context: Context, type: string, pct: Totals["pct"]): string {
return context.classForPercent(type, typeof pct === "number" ? pct : NaN);
}
const themePickerTemplate = `
<label class="theme-select quiet">
Theme
<select id="themeSelect" autocomplete="off">
<option value="light">Light</option>
<option value="dark">Dark</option>
<option value="legacy">Legacy</option>
</select>
</label>
`;
function htmlHead(details: TemplateData): string {
return `
<head>
<title>Code coverage report for ${html.escape(details.entity)}</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="${html.escape(details.prettify.css)}" />
<link rel="stylesheet" href="${html.escape(details.base.css)}" />
<link rel="shortcut icon" type="image/x-icon" href="${html.escape(details.favicon)}" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
${details.fixedTheme ? "" : `<script src="${html.escape(details.theme.js)}"></script>`}
<style type='text/css'>
.coverage-summary .sorter {
background-image: url(${html.escape(details.sorter.image)});
}
</style>
</head>
`;
}
function headerTemplate(details: TemplateData): string {
function metricsTemplate({ pct, covered, total }: Totals, kind: string): string {
return `
<div class='fl pad1y space-right2'>
<span class="strong">${pct}% </span>
<span class="quiet">${kind}</span>
<span class='fraction'>${covered}/${total}</span>
</div>
`;
}
function skipTemplate(metrics: CoverageSummary): string {
const statements = metrics.statements.skipped;
const branches = metrics.branches.skipped;
const functions = metrics.functions.skipped;
const countLabel = (c: number, label: string, plural: string): string[] | string =>
c === 0 ? [] : `${c} ${label}${c === 1 ? "" : plural}`;
const skips = ([] as string[]).concat(
countLabel(statements, "statement", "s"),
countLabel(functions, "function", "s"),
countLabel(branches, "branch", "es"),
);
Eif (skips.length === 0) {
return "";
}
return `
<div class='fl pad1y'>
<span class="strong">${skips.join(", ")}</span>
<span class="quiet">Ignored</span>
</div>
`;
}
return `
<!doctype html>
<html lang="en"${details.fixedTheme ? ` data-theme="${html.escape(details.fixedTheme)}"` : ""}>
${htmlHead(details)}
<body>
<div class='wrapper'>
<div class='pad1'>
${details.fixedTheme ? "" : themePickerTemplate}
<h1>${details.pathHtml}</h1>
<div class='clearfix'>
${metricsTemplate(details.metrics.statements, "Statements")}
${metricsTemplate(details.metrics.branches, "Branches")}
${metricsTemplate(details.metrics.functions, "Functions")}
${metricsTemplate(details.metrics.lines, "Lines")}
${skipTemplate(details.metrics)}
</div>
<p class="quiet">
Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
</p>
<template id="filterTemplate">
<div class="quiet">
Filter:
<input type="search" id="fileSearch">
</div>
</template>
</div>
<div class='status-line ${details.reportClass}'></div>
`;
}
function footerTemplate(details: TemplateData): string {
return `
<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
at ${html.escape(details.datetime)}
</div>
<script src="${html.escape(details.prettify.js)}"></script>
<script>
window.onload = function () {
prettyPrint();
};
</script>
<script src="${html.escape(details.sorter.js)}"></script>
<script src="${html.escape(details.blockNavigation.js)}"></script>
</body>
</html>
`;
}
function detailTemplate(data: AnnotatedData): string {
const lineNumbers = new Array(data.maxLines).fill(undefined).map((_, i) => i + 1);
const lineLink = (num: number) => `<a name='L${num}'></a><a href='#L${num}'>${num}</a>`;
const lineCount = (line: { covered: string | null; hits: number | string }) =>
`<span class="cline-any cline-${line.covered}">${line.hits}</span>`;
/* This is rendered in a `<pre>`, need control of all whitespace. */
return [
"<tr>",
`<td class="line-count quiet">${lineNumbers.map(lineLink).join("\n")}</td>`,
`<td class="line-coverage quiet">${data.lineCoverage.map(lineCount).join("\n")}</td>`,
`<td class="text"><pre class="prettyprint lang-js">${data.annotatedCode.join("\n")}</pre></td>`,
"</tr>",
].join("");
}
const summaryTableHeader = [
'<div class="pad1">',
'<table class="coverage-summary">',
"<thead>",
"<tr>",
' <th data-col="file" data-fmt="html" data-html="true" class="file">File</th>',
' <th data-col="pic" data-type="number" data-fmt="html" data-html="true" class="pic"></th>',
' <th data-col="statements" data-type="number" data-fmt="pct" class="pct">Statements</th>',
' <th data-col="statements_raw" data-type="number" data-fmt="html" class="abs"></th>',
' <th data-col="branches" data-type="number" data-fmt="pct" class="pct">Branches</th>',
' <th data-col="branches_raw" data-type="number" data-fmt="html" class="abs"></th>',
' <th data-col="functions" data-type="number" data-fmt="pct" class="pct">Functions</th>',
' <th data-col="functions_raw" data-type="number" data-fmt="html" class="abs"></th>',
' <th data-col="lines" data-type="number" data-fmt="pct" class="pct">Lines</th>',
' <th data-col="lines_raw" data-type="number" data-fmt="html" class="abs"></th>',
"</tr>",
"</thead>",
"<tbody>",
].join("\n");
function summaryLineTemplate(details: SummaryLineData): string {
const { reportClasses, metrics, file, output } = details;
const percentGraph = (pct: Totals["pct"]): string => {
if (typeof pct !== "number" || !isFinite(pct)) {
return "";
}
const cls = ["cover-fill"];
if (pct === 100) {
cls.push("cover-full");
}
pct = Math.floor(pct);
return [
`<div class="${cls.join(" ")}" style="width: ${pct}%"></div>`,
`<div class="cover-empty" style="width: ${100 - pct}%"></div>`,
].join("");
};
const summaryType = (type: MetricKey, showGraph = false): string[] => {
const info = metrics[type];
const reportClass = reportClasses[type];
const result = [
`<td data-value="${info.pct}" class="pct ${reportClass}">${info.pct}%</td>`,
`<td data-value="${info.total}" class="abs ${reportClass}">${info.covered}/${info.total}</td>`,
];
if (showGraph) {
result.unshift(
`<td data-value="${info.pct}" class="pic ${reportClass}">`,
`<div class="chart">${percentGraph(info.pct)}</div>`,
`</td>`,
);
}
return result;
};
return ([] as string[])
.concat(
"<tr>",
`<td class="file ${
reportClasses.statements
}" data-value="${html.escape(file)}"><a href="${html.escape(
output,
)}">${html.escape(file)}</a></td>`,
summaryType("statements", true),
summaryType("branches"),
summaryType("functions"),
summaryType("lines"),
"</tr>\n",
)
.join("\n\t");
}
const summaryTableFooter = ["</tbody>", "</table>", "</div>"].join("\n");
const emptyClasses: Record<MetricKey, string> = {
statements: "empty",
lines: "empty",
functions: "empty",
branches: "empty",
};
const standardLinkMapper: LinkMapper = {
getPath(node) {
if (typeof node === "string") {
return node;
}
let filePath = node.getQualifiedName();
if (node.isSummary()) {
Iif (filePath !== "") {
filePath += "/index.html";
} else {
filePath = "index.html";
}
} else {
filePath += ".html";
}
return filePath;
},
relativePath(source, target) {
const targetPath = this.getPath(target);
const sourcePath = path.dirname(this.getPath(source));
return path.posix.relative(sourcePath, targetPath);
},
assetPath(node, name) {
return this.relativePath(this.getPath(node), name);
},
};
function fixPct(metrics: CoverageSummary): CoverageSummary {
(Object.keys(emptyClasses) as MetricKey[]).forEach((key) => {
metrics[key].pct = 0;
});
return metrics;
}
class HtmlReport extends ReportBase {
declare verbose: boolean | undefined;
declare linkMapper: LinkMapper;
declare subdir: string;
declare date: string;
declare skipEmpty: boolean | undefined;
declare theme: HtmlTheme | undefined;
constructor(opts: HtmlOptions) {
super();
this.verbose = opts.verbose;
this.linkMapper = opts.linkMapper || standardLinkMapper;
this.subdir = opts.subdir || "";
this.date = new Date().toISOString();
this.skipEmpty = opts.skipEmpty;
this.theme = ["light", "dark", "legacy"].includes(opts.theme!) ? opts.theme : undefined;
}
getBreadcrumbHtml(node: ReportNode): string {
let parent = node.getParent();
const nodePath: ReportNode[] = [];
while (parent) {
nodePath.push(parent);
parent = parent.getParent();
}
const linkPath = nodePath.map((ancestor) => {
const target = this.linkMapper.relativePath(node, ancestor);
const name = ancestor.getRelativeName() || "All files";
return '<a href="' + target + '">' + name + "</a>";
});
linkPath.reverse();
return linkPath.length > 0 ? linkPath.join(" / ") + " " + node.getRelativeName() : "All files";
}
fillTemplate(node: ReportNode, templateData: TemplateData, context: Context): void {
const linkMapper = this.linkMapper;
const summary = node.getCoverageSummary()!;
templateData.entity = node.getQualifiedName() || "All files";
templateData.metrics = summary;
templateData.reportClass = classForPercent(context, "statements", summary.statements.pct);
templateData.pathHtml = this.getBreadcrumbHtml(node);
templateData.base = {
css: linkMapper.assetPath(node, "base.css"),
};
templateData.sorter = {
js: linkMapper.assetPath(node, "sorter.js"),
image: linkMapper.assetPath(node, "sort-arrow-sprite.png"),
};
templateData.blockNavigation = {
js: linkMapper.assetPath(node, "block-navigation.js"),
};
templateData.theme = {
js: linkMapper.assetPath(node, "theme.js"),
};
templateData.prettify = {
js: linkMapper.assetPath(node, "prettify.js"),
css: linkMapper.assetPath(node, "prettify.css"),
};
templateData.favicon = linkMapper.assetPath(node, "favicon.png");
}
getTemplateData(): TemplateData {
return { datetime: this.date, fixedTheme: this.theme } as TemplateData;
}
getWriter(context: Context): FileWriter {
Eif (!this.subdir) {
return context.writer;
}
return context.writer.writerForDir(this.subdir);
}
onStart(root: ReportNode, context: Context): void {
const assetHeaders: Record<string, string> = {
".js": "/* eslint-disable */\n",
};
[".", "vendor"].forEach((subdir) => {
const writer = this.getWriter(context);
const srcDir = path.resolve(import.meta.dirname, "assets", subdir);
fs.readdirSync(srcDir).forEach((f) => {
const resolvedSource = path.resolve(srcDir, f);
const resolvedDestination = ".";
const stat = fs.statSync(resolvedSource);
let dest;
if (stat.isFile()) {
dest = resolvedDestination + "/" + f;
Iif (this.verbose) {
console.log("Write asset: " + dest);
}
writer.copyFile(resolvedSource, dest, assetHeaders[path.extname(f)]);
}
});
});
}
onSummary(node: ReportNode, context: Context): void {
const linkMapper = this.linkMapper;
const templateData = this.getTemplateData();
const children = node.getChildren();
const skipEmpty = this.skipEmpty;
this.fillTemplate(node, templateData, context);
const cw = this.getWriter(context).writeFile(linkMapper.getPath(node));
cw.write(headerTemplate(templateData));
cw.write(summaryTableHeader);
children.forEach((child) => {
const metrics = child.getCoverageSummary()!;
const isEmpty = metrics.isEmpty();
if (skipEmpty && isEmpty) {
return;
}
const reportClasses = isEmpty
? emptyClasses
: {
statements: classForPercent(context, "statements", metrics.statements.pct),
lines: classForPercent(context, "lines", metrics.lines.pct),
functions: classForPercent(context, "functions", metrics.functions.pct),
branches: classForPercent(context, "branches", metrics.branches.pct),
};
const data = {
metrics: isEmpty ? fixPct(metrics) : metrics,
reportClasses,
file: child.getRelativeName(),
output: linkMapper.relativePath(node, child),
};
cw.write(summaryLineTemplate(data) + "\n");
});
cw.write(summaryTableFooter);
cw.write(footerTemplate(templateData));
cw.close();
}
onDetail(node: ReportNode, context: Context): void {
const linkMapper = this.linkMapper;
const templateData = this.getTemplateData();
this.fillTemplate(node, templateData, context);
const cw = this.getWriter(context).writeFile(linkMapper.getPath(node));
cw.write(headerTemplate(templateData));
cw.write('<pre><table class="coverage">\n');
cw.write(detailTemplate(annotator(node.getFileCoverage(), context)));
cw.write("</table></pre>\n");
cw.write(footerTemplate(templateData));
cw.close();
}
}
export default HtmlReport;
|