@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
53 lines • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleResult = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const CaseFailedAssertionError_1 = require("../CaseFailedAssertionError");
const handleResult = (example, exampleIndex, context) => {
context.logger.deepMaintainerDebug('Handling a result of', example);
if (example.result === 'FAILED') {
context.logger.deepMaintainerDebug(`Printing a failure title for`, example, exampleIndex, context);
// The result formatter here returns what it logs, so
// we can keep the error message consistent
context.resultPrinter.printFailureTitle(example, exampleIndex, context);
const errors = example.errors
.map((e) => context.resultPrinter.printError(e, context))
.reduce((acc, curr) => `${acc}\n${curr}`, '');
const errorMessage = `${errors}`;
// Warning: **ALL** error types must be checked in this function
// TODO: This should be refactored so that it's not possible to reach the end
// without checking all types
const triggerError = example.errors.find((i) => i.type === case_plugin_base_1.ERROR_TYPE_TRIGGER);
if (triggerError) {
throw new case_plugin_base_1.CaseTriggerError(`The provided trigger function failed during execution:\n\n${errorMessage}`, {
...context,
// Need to override the location in the context with the location of
// the trigger error, because it happened at a different place
'_case:currentRun:context:location': triggerError.location,
}, triggerError.userFacingStackTrace);
}
if (example.errors.some((i) => i.type === case_plugin_base_1.ERROR_TYPE_MATCHING || i.type === case_plugin_base_1.ERROR_TYPE_RAW_MATCH)) {
context.logger.debug(`Matching errors present`);
if (context['_case:currentRun:context:throwOnFail']) {
context.logger.maintainerDebug(`Throwing a CaseFailedAssertionError with`, example.errors);
throw new CaseFailedAssertionError_1.CaseFailedAssertionError(errorMessage, example.errors);
}
else {
context.logger.maintainerDebug(`Not erroring because throwOnFail is false`);
}
}
const verificationError = example.errors.find((i) => i.type === case_plugin_base_1.ERROR_TYPE_TEST_RESPONSE);
if (verificationError && context['_case:currentRun:context:throwOnFail']) {
throw verificationError.error;
}
const configurationError = example.errors.find((i) => i.type === case_plugin_base_1.ERROR_TYPE_CONFIGURATION);
if (configurationError) {
throw new case_plugin_base_1.CaseConfigurationError(errorMessage, 'DONT_ADD_LOCATION', configurationError.code ?? 'UNDOCUMENTED');
}
}
else {
context.resultPrinter.printSuccessTitle(example, exampleIndex, context);
}
};
exports.handleResult = handleResult;
//# sourceMappingURL=handlers.js.map