@case-contract-testing/case
Version:
Next-generation contract testing suite
80 lines (79 loc) • 4.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resultPrinter = void 0;
const chalk_1 = __importDefault(require("chalk"));
const node_console_1 = require("node:console");
const contract_1 = require("../../entities/contract");
const results_1 = require("../../entities/results");
const types_1 = require("../../entities/types");
const context_1 = require("../../entities/context");
const stdout = new node_console_1.Console({ stdout: process.stdout });
const locationString = (context) => `${(0, context_1.locationString)(context)}`;
const spaces = (size, str) => {
const space = new Array(size).fill(' ').join('');
return `${space}${str.replace(/\n/g, `\n${space}`)}`;
};
const locationTitleLine = (location, context) => Array.isArray(location) && location.length > 3
? ` ${locationString({
...context,
'case:currentRun:context:location': location.slice(2),
})}: `
: '';
const errorTitleLine = (title, error, context) => spaces(6, `${chalk_1.default.bgRed.white(` ${title} `)} ${chalk_1.default.whiteBright(locationTitleLine(error.location, context))} ${chalk_1.default.whiteBright(error.message)}`);
const secondMatchErrorLine = (error) => spaces(9, `Expected something like:\n${spaces(3, chalk_1.default.green((0, results_1.actualToString)(error.expected, 10)))}`);
const thirdMatchErrorLine = (error) => spaces(9, `Actual:\n${spaces(3, chalk_1.default.red((0, results_1.actualToString)(error.actual, 10)))}`);
const camelToCapital = (camel) => camel.replace(/([a-z])([A-Z])/g, '$1 $2').toLocaleUpperCase();
const printError = (error, context) => {
const locationTagLine = () => spaces(12, `${chalk_1.default.gray(` - ${locationString({
...context,
'case:currentRun:context:location': Array.isArray(error.location)
? error.location
: [],
})} [${'matcher' in error ? error.matcher['case:matcher:type'] : error.code}]`)}`);
if (context['case:currentRun:context:printResults']) {
switch (error.type) {
// This is done as one line to prevent it splitting when multiple tests are running
case types_1.ERROR_TYPE_MATCHING:
stdout.log(`${errorTitleLine('MATCHING ERROR', error, context)}\n${secondMatchErrorLine(error)}\n${thirdMatchErrorLine(error)}\n\n${locationTagLine()}\n\n`);
break;
case types_1.ERROR_TYPE_RAW_MATCH:
stdout.log(`${errorTitleLine('MATCHING ERROR', error, context)}\n${secondMatchErrorLine(error)}\n${thirdMatchErrorLine(error)}\n\n${locationTagLine()}\n\n`);
break;
case types_1.ERROR_TYPE_EXECUTION:
stdout.log(`${errorTitleLine(camelToCapital(error.code), error, context)}\n\n${locationTagLine()}\n\n`);
break;
case types_1.ERROR_TYPE_TEST_RESPONSE:
stdout.log(`${errorTitleLine('ERROR VERIFYING RETURNED OBJECT', error, context)}\n\n${locationTagLine()}\n\n`);
break;
default:
stdout.log(`${errorTitleLine('ERROR', error, context)}\n\n${locationTagLine()}\n\n`);
}
}
};
const printFailureTitle = (example, index, context) => {
if (context['case:currentRun:context:printResults']) {
// This is done as one line to prevent it splitting when multiple tests are running
stdout.log(spaces(3, `\n${chalk_1.default.red(`✘`)} ${chalk_1.default.whiteBright((0, contract_1.exampleToNames)(example, index).mockName)}\n Error details follow:\n`));
}
};
const printSuccessTitle = (example, index, context) => {
if (context['case:currentRun:context:printResults']) {
// This is done as one line to prevent it splitting when multiple tests are running
stdout.log(spaces(3, `\n${chalk_1.default.greenBright(`✅`)} ${chalk_1.default.whiteBright((0, contract_1.exampleToNames)(example, index).mockName)}\n`));
}
};
const printDownloadedContract = (filename, context) => {
if (context['case:currentRun:context:printResults']) {
// This is done as one line to prevent it splitting when multiple tests are running
stdout.log(spaces(3, `\n${chalk_1.default.greenBright(`📥`)} ${chalk_1.default.whiteBright(`Downloaded contract ${filename}`)}\n`));
}
};
exports.resultPrinter = {
printError,
printFailureTitle,
printSuccessTitle,
printDownloadedContract,
};