nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
101 lines (90 loc) • 2.84 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/test_runner/reporter/utils.js
import * as colors from "nstdlib/lib/internal/util/colors";
import { inspectWithNoCustomRetry } from "nstdlib/lib/internal/errors";
const indentMemo = new Map();
const inspectOptions = {
__proto__: null,
colors: colors.shouldColorize(process.stdout),
breakLength: Infinity,
};
const reporterUnicodeSymbolMap = {
__proto__: null,
"test:fail": "\u2716 ",
"test:pass": "\u2714 ",
"test:diagnostic": "\u2139 ",
"test:coverage": "\u2139 ",
"arrow:right": "\u25B6 ",
"hyphen:minus": "\uFE63 ",
};
const reporterColorMap = {
__proto__: null,
get "test:fail"() {
return colors.red;
},
get "test:pass"() {
return colors.green;
},
get "test:diagnostic"() {
return colors.blue;
},
};
function indent(nesting) {
let value = indentMemo.get(nesting);
if (value === undefined) {
value = String.prototype.repeat.call(" ", nesting);
indentMemo.set(nesting, value);
}
return value;
}
function formatError(error, indent) {
if (!error) return "";
const err = error.code === "ERR_TEST_FAILURE" ? error.cause : error;
const message = Array.prototype.join.call(
RegExp.prototype[Symbol.split].call(
hardenRegExp(/\r?\n/),
inspectWithNoCustomRetry(err, inspectOptions),
),
`\n${indent} `,
);
return `\n${indent} ${message}\n`;
}
function formatTestReport(
type,
data,
prefix = "",
indent = "",
hasChildren = false,
) {
let color = reporterColorMap[type] ?? colors.white;
let symbol = reporterUnicodeSymbolMap[type] ?? " ";
const { skip, todo } = data;
const duration_ms = data.details?.duration_ms
? ` ${colors.gray}(${data.details.duration_ms}ms)${colors.white}`
: "";
let title = `${data.name}${duration_ms}`;
if (skip !== undefined) {
title += ` # ${typeof skip === "string" && skip.length ? skip : "SKIP"}`;
} else if (todo !== undefined) {
title += ` # ${typeof todo === "string" && todo.length ? todo : "TODO"}`;
}
const error = formatError(data.details?.error, indent);
if (hasChildren) {
// If this test has had children - it was already reported, so slightly modify the output
const err =
!error || data.details?.error?.failureType === "subtestsFailed"
? ""
: `\n${error}`;
return `${prefix}${indent}${color}${reporterUnicodeSymbolMap["arrow:right"]}${colors.white}${title}${err}`;
}
if (skip !== undefined) {
color = colors.gray;
symbol = reporterUnicodeSymbolMap["hyphen:minus"];
}
return `${prefix}${indent}${color}${symbol}${title}${colors.white}${error}`;
}
const _export___proto___ = null;
export { _export___proto___ as __proto__ };
export { reporterUnicodeSymbolMap };
export { reporterColorMap };
export { formatTestReport };
export { indent };