@averagehelper/corde
Version:
A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)
162 lines (161 loc) • 6.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.reporter = void 0;
const chalk_1 = __importDefault(require("chalk"));
const messages_1 = require("../messages");
const FAIL = "FAIL";
const SPACE = " ";
const DEFAULT_SPACE_VALUE = 4;
class Reporter {
constructor() {
this._bgSuccess = chalk_1.default.bgRgb(21, 194, 19);
this._bgInfo = chalk_1.default.bgYellow;
this._bgError = chalk_1.default.bgRed;
this._bold = chalk_1.default.bold;
this._red = chalk_1.default.red;
this._black = chalk_1.default.black;
this._bgSuccessBold = this._bgSuccess.bold;
this._successCount = 0;
this._failureCount = 0;
}
/**
* Prints the output of each assertion.
* @param groups All groups of tests
* @returns Returns true if all tests has passed
*/
outPutResult(groups) {
if (!groups) {
return false;
}
const tabSpace = SPACE;
this.breakLine();
groups.forEach((group) => this.printGroup(group, tabSpace));
this.showSummary();
return this._failureCount === 0;
}
printNoTestFound() {
console.log(`${this._bgInfo(messages_1.messages.INFO)}: ${messages_1.messages.EMPTY_TEST_FILE}`);
}
breakLine() {
console.log("");
}
printGroup(group, tab) {
if (group.name) {
console.log(`${tab}${group.name}`);
}
if (group.subGroups) {
tab += SPACE;
group.subGroups.forEach((subGroup) => this.printGroup(subGroup, ""));
}
if (group.tests) {
tab += SPACE;
group.tests.forEach((test) => this.printTest(test, tab));
}
}
printTest(test, tab) {
if (test.name) {
console.log(`${tab}${test.name}`);
}
if (test.subTests) {
tab += SPACE;
test.subTests.forEach((subTest) => this.printTest(subTest, tab));
}
if (test.testsReports) {
tab += SPACE;
test.testsReports.forEach((report) => this.printAssertion(report, tab));
}
}
printAssertion(report, tab) {
if (report.hasPassed) {
this.printSuccess(tab, report);
this._successCount++;
}
else {
this._failureCount++;
this.printFailure(tab, report);
}
}
showSummary() {
this.breakLine();
if (this.doesAllTestsPassed()) {
this.printFullSuccess();
}
else if (this.doesSomeTestsPassed()) {
this.printPartialSuccess();
}
else {
this.printFullFailure();
}
}
doesAllTestsPassed() {
return this._failureCount === 0 && this._successCount > 0;
}
doesSomeTestsPassed() {
return this._failureCount > 0 && this._successCount > 0;
}
printFullSuccess() {
console.log(messages_1.messages.ALL_TESTS_PASSED);
console.log(`${this._bgSuccess(messages_1.messages.TOTAL)} ${chalk_1.default.bold(this._successCount)}`);
}
printPartialSuccess() {
console.log(messages_1.messages.TESTS_PASSED_WITH_ERRORS);
console.log(`${this._bgError(messages_1.messages.FAILURES)} ${chalk_1.default.bold(this._failureCount)}`);
console.log(`${this._bgSuccess(messages_1.messages.SUCCESS)} ${chalk_1.default.bold(this._successCount)}`);
}
printFullFailure() {
console.log(messages_1.messages.ALL_TESTS_FAIL);
console.log(`${chalk_1.default.bgRed(messages_1.messages.FAILURES)} ${chalk_1.default.bold(this._failureCount)}`);
}
printFailure(tabSpace, report) {
if (report.customReturnMessage) {
console.log(`${tabSpace} ${this._bgSuccess.bold(" PASS ")} command ${chalk_1.default.bold(report.commandName)} ${report.customReturnMessage}`);
}
else {
if (report.showExpectAndOutputValue) {
this.printFailureWithValues(tabSpace, report);
}
else {
this.printFailureOnlyWithCommandName(tabSpace, report.commandName);
}
}
}
printSuccess(tabSpace, report) {
if (report.showExpectAndOutputValue) {
this.printSuccessWithValues(tabSpace, report);
}
else {
this.printSuccessOnlyWithCommandName(tabSpace, report.commandName);
}
}
printFailureWithValues(tabSpace, report) {
const notWord = this.getNotWordIfTrue(report.isNot);
console.log(`${tabSpace} ${chalk_1.default.bgRed(` ${FAIL} `)} expected ${chalk_1.default.bold(report.commandName)} to${notWord}return '${this._bold(this.getPrintingValueByType(report.expectation))}'. Returned: '${this._red(this.getPrintingValueByType(report.output))}'`);
}
printFailureOnlyWithCommandName(tabSpace, commandName) {
console.log(`${tabSpace} ${this._bgSuccess.bgRed(" FAIL ")} command ${this._bold(commandName)} not returned what was expected`);
}
printSuccessWithValues(tabSpace, report) {
const notWord = this.getNotWordIfTrue(report.isNot);
console.log(`${tabSpace} ${this._bgSuccessBold(" PASS ")} expected ${chalk_1.default.bold(report.commandName)} to${notWord}return '${chalk_1.default.bold(this.getPrintingValueByType(report.expectation))}'. Returned: '${chalk_1.default.green(this.getPrintingValueByType(report.output))}'`);
}
printSuccessOnlyWithCommandName(tabSpace, commandName) {
console.log(`${tabSpace} ${this._bgSuccess.bold(" PASS ")} command ${chalk_1.default.bold(commandName)} returned what was expected`);
}
getNotWordIfTrue(isNot) {
if (isNot) {
return " not ";
}
return " ";
}
getPrintingValueByType(value) {
if (typeof value === "string") {
return value;
}
return `\n ${JSON.stringify(value, null, DEFAULT_SPACE_VALUE)}`;
}
}
const reporter = new Reporter();
exports.reporter = reporter;