All files / istanbul-lib-report/src/reports/text-summary index.ts

0% Statements 0/27
0% Branches 0/10
0% Functions 0/4
0% Lines 0/27

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                                                                                                                                                 
/*
 Copyright 2012-2015, Yahoo Inc.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */
import type { CoverageSummary } from "@vitest/istanbul-lib-coverage";
 
import type { Context, ReportNode } from "../../index";
import ReportBase from "../../report-base";
 
/** the four coverage metrics printed in the summary */
type MetricKey = "statements" | "branches" | "functions" | "lines";
 
/** options accepted by {@link TextSummaryReport} */
export interface TextSummaryOptions {
  /** the file to write the report to, defaults to the console */
  file?: string | null;
}
 
class TextSummaryReport extends ReportBase {
  declare file: string | null;
 
  constructor(opts?: TextSummaryOptions) {
    super();
 
    opts = opts || {};
    this.file = opts.file || null;
  }
 
  onStart(node: ReportNode, context: Context): void {
    const summary = node.getCoverageSummary()!;
    const cw = context.writer.writeFile(this.file);
    const printLine = function (key: MetricKey) {
      const str = lineForKey(summary, key);
      const pct = summary[key].pct;
      const clazz = context.classForPercent(key, typeof pct === "number" ? pct : NaN);
      cw.println(cw.colorize(str, clazz));
    };
 
    cw.println("");
    cw.println("=============================== Coverage summary ===============================");
    printLine("statements");
    printLine("branches");
    printLine("functions");
    printLine("lines");
    cw.println("================================================================================");
    cw.close();
  }
}
 
function lineForKey(summary: CoverageSummary, key: MetricKey): string {
  const metrics = summary[key];
 
  let name: string = key.substring(0, 1).toUpperCase() + key.substring(1);
  if (name.length < 12) {
    name += "                   ".substring(0, 12 - name.length);
  }
  const result = [
    name,
    ":",
    `${metrics.pct}%`,
    "(",
    metrics.covered + "/" + metrics.total,
    ")",
  ].join(" ");
  const skipped = metrics.skipped;
  if (skipped > 0) {
    return result + ", " + skipped + " ignored";
  }
  return result;
}
 
export default TextSummaryReport;