@pipobscure/demitasse-pretty
Version:
Simple BDD Testing - Pretty Reporter
106 lines • 4.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const util_1 = require("util");
const INSPECTOPTS = { colors: true, compact: true, getters: true };
const TODO = '\u2610';
const SKIP = '\u2740';
const GOOD = '\u2611';
const FAIL = '\u2612';
class Pretty {
constructor(write) {
this.write = write;
this.caseStack = [];
this.good = 0;
this.skip = 0;
this.todo = 0;
this.fail = 0;
}
get indent() {
return new Array(this.caseStack.length).fill(' ').join('');
}
notifyInit(options, cases) { }
notifyStart(current) {
if (current.children) {
this.write(`${this.indent}${chalk_1.default.magenta.underline(current.name)}`);
}
this.caseStack.unshift(current);
}
notifyFinish(current) {
this.caseStack.shift();
if (!current.children) {
if (current.isSkipped) {
this.skip++;
this.write(`${this.indent}${chalk_1.default.cyan(`${SKIP} ${current.name}`)}`);
if (current.reason)
this.writeReason(current);
}
else if (current.isToDo) {
this.todo++;
this.write(`${this.indent}${chalk_1.default.blue(`${TODO} ${current.name}`)}`);
if (current.reason)
this.writeReason(current);
}
else if (current.isFailure) {
this.fail++;
this.write(`${this.indent}${chalk_1.default.redBright(`${FAIL} ${current.name}`)}`);
if (current.reason)
this.writeReason(current);
}
else {
this.good++;
this.write(`${this.indent}${chalk_1.default.green(`${GOOD} ${current.name}`)}`);
}
}
}
notifyError(error) { }
notifyComplete() {
this.write('');
this.write('');
this.write(chalk_1.default.green(`${pad(this.good)} ${GOOD} passed`));
this.write(chalk_1.default.redBright(`${pad(this.fail)} ${FAIL} failed`));
this.write(chalk_1.default.cyan(`${pad(this.skip)} ${SKIP} skipped`));
this.write(chalk_1.default.blue(`${pad(this.todo)} ${TODO} to-do`));
this.write('');
}
writeReason(testCase) {
if (!testCase.reason)
return;
const reason = testCase.reason;
// @ts-ignore
if (reason.code === 'ERR_SKIP') {
this.write(`${this.indent} ${chalk_1.default.cyan.dim('\u21D6' + reason.message)}`);
return;
}
if (reason.code === 'ERR_ASSERTION') {
const assertion = reason;
this.write(`${this.indent} ${chalk_1.default.red.dim(`assertion: ${assertion.message}`)}`);
let lines = util_1.inspect(assertion.expected, INSPECTOPTS).split(/\r?\n/);
this.write(`${this.indent} ${chalk_1.default.cyan.dim(`expected: ${lines.length > 1 ? '' : util_1.inspect(assertion.expected)}`)}`);
if (lines.length > 1)
lines.forEach((line, lnr) => this.write(`${this.indent} ${line}`));
lines = util_1.inspect(assertion.actual, INSPECTOPTS).split(/\r?\n/);
this.write(`${this.indent} ${chalk_1.default.blue.dim(`actual: ${lines.length > 1 ? '' : util_1.inspect(assertion.actual)}`)}`);
if (lines.length > 1)
lines.forEach((line, lnr) => this.write(`${this.indent} ${line}`));
}
else if (reason instanceof Error) {
const stack = (reason.stack || '').split(/\r?\n/);
this.write(`${this.indent} ${chalk_1.default.red.dim(`error: ${stack[0]}`)}`);
this.write(`${this.indent} ${chalk_1.default.yellow.dim(`location: ${stack[1].trim().replace(/^at\s*/, '')}`)}`);
}
else {
util_1.inspect(reason, INSPECTOPTS)
.split(/\r?\n/)
.forEach((line) => this.write(`${this.indent} ${chalk_1.default.dim(line)}`));
}
}
}
function pad(num) {
return ` ${num}`.slice(-4);
}
exports.reporter = new Pretty((line) => console.log(line));
//# sourceMappingURL=index.js.map