Skip to content

Commit

Permalink
chore: adds & uses a better minimal reporter for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Aug 6, 2023
1 parent 27788e6 commit b47308b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"branches": 100,
"functions": 100,
"lines": 100,
"exclude": ["{src/**/*.test.ts}"],
"exclude": ["{src/**/*.test.ts,tools/**/*}"],
"reporter": ["text-summary", "html", "json-summary"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"format": "prettier --log-level warn --write \"**/*.{md,ts,json,yml}\"",
"prepare": "husky install",
"scm:stage": "git add .",
"test": "c8 node --no-warnings --loader 'ts-node/esm' --test-reporter dot --test src/*.test.ts",
"test": "c8 node --no-warnings --loader 'ts-node/esm' --test-reporter ./tools/dot-with-summary.reporter.js --test src/*.test.ts",
"update-dependencies": "npm run upem:update && npm run upem:install && npm run check",
"upem-outdated": "npm outdated --json --long | upem --dry-run",
"upem:install": "npm install",
Expand Down
93 changes: 93 additions & 0 deletions tools/dot-with-summary.reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable no-use-before-define */
import { EOL } from "node:os";

// eslint-disable-next-line no-undefined
const LOCALE = undefined;
const gTimeFormat = new Intl.NumberFormat(LOCALE, {
style: "unit",
unit: "millisecond",
unitDisplay: "narrow",
maximumFractionDigits: 0,
}).format;

// eslint-disable-next-line max-lines-per-function, complexity
export default async function* dotWithSummaryReporter(pSource) {
let lFailStack = [];
let lDiagnosticStack = [];

for await (const lEvent of pSource) {
switch (lEvent.type) {
case "test:pass":
if (lEvent.data.details.type === "suite") {
yield "";
} else if (lEvent.data.skip || lEvent.data.todo) {
yield ",";
} else {
yield ".";
}
break;
case "test:fail":
lFailStack.push(lEvent.data);
if (lEvent.data.details.type === "suite") {
yield "";
} else {
yield "!";
}
break;
case "test:diagnostic":
lDiagnosticStack.push(lEvent);
break;
// // uncomment these lines if you're interested in any stdout/stderr
// case "test:stdout":
// yield lEvent.data.message;
// break;
// case "test:stderr":
// yield lEvent.data.message;
// break;
// // test:coverage apparently exists, but not seen in any runs so far
// case "test:coverage":
// yield "C";
// break;
default:
break;
}
}

const lDiagnostics = lDiagnosticStack
.map(diagnosticToObject)
.reduce((pAll, pDiagnostic) => ({ ...pAll, ...pDiagnostic }), {});

yield `${
EOL + lFailStack.map(summarizeFailsToText).filter(Boolean).join(EOL)
}${EOL}${lDiagnostics.pass} passing (${gTimeFormat(
lDiagnostics.duration_ms,
)})${EOL}` +
`${lDiagnostics.fail > 0 ? `${lDiagnostics.fail} failing${EOL}` : ""}` +
`${
lDiagnostics.skipped > 0 ? `${lDiagnostics.skipped} skipped${EOL}` : ""
}` +
`${lDiagnostics.todo > 0 ? `${lDiagnostics.todo} todo${EOL}` : ""}${EOL}`;
}

function summarizeFailsToText(pFailEvent) {
if (pFailEvent.details.error.failureType === "testCodeFailure") {
return `✘ ${pFailEvent.name}${EOL} ${formatError(pFailEvent)}${EOL}${EOL}`;
}
return "";
}

function diagnosticToObject(pDiagnosticEvent) {
const lReturnValue = {};
const [key, value] = pDiagnosticEvent.data.message.split(" ");
// eslint-disable-next-line security/detect-object-injection
lReturnValue[key] = value;
return lReturnValue;
}

function formatError(pTestResult) {
return (
pTestResult.details.error.cause.stack ||
pTestResult.details.error.cause.message ||
pTestResult.details.error.cause.code
);
}

0 comments on commit b47308b

Please sign in to comment.