@case-contract-testing/case
Version:
Next-generation contract testing suite
47 lines (46 loc) • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeExample = void 0;
const triggers_1 = require("./triggers");
const setup_1 = require("./setup");
const entities_1 = require("../../entities");
const contract_1 = require("../../entities/contract");
const results_1 = require("../../entities/results");
const toResultingExample = (assertable, example, context) => assertable.assert().then((matchResult) => {
if ((0, results_1.hasErrors)(matchResult)) {
context.logger.debug(`This example failed the assertions`);
return (0, contract_1.makeFailedExample)(example, matchResult);
}
context.logger.debug(`This example passed all assertions`);
return (0, contract_1.makeSuccessExample)(example);
}, (error) => {
context.logger.debug(`This example failed while trying to run the assertion`);
if (error instanceof entities_1.CaseConfigurationError) {
return (0, contract_1.makeFailedExample)(example, (0, results_1.makeResults)((0, results_1.executionError)(error, context)));
}
if (error instanceof entities_1.CaseFailedAssertionError) {
return (0, contract_1.makeFailedExample)(example, error.matchResult);
}
return (0, contract_1.makeFailedExample)(example, (0, results_1.makeResults)((0, results_1.executionError)(new entities_1.CaseConfigurationError('Failed during case assertion'), context), (0, results_1.executionError)(error, context)));
});
const executeExample = (example, { stateHandlers = {}, trigger, triggers, names, testErrorResponse, testResponse, }, contract, context) => (0, setup_1.setupExample)(example, stateHandlers, context)
.then((assertable) => {
context.logger.debug(`Invoking trigger with`, assertable.config, context['case:currentRun:context:location']);
return (0, triggers_1.findAndCallTrigger)(example.mock, { trigger, triggers, names, testErrorResponse, testResponse }, assertable, context).then(() => {
context.logger.maintainerDebug(`Asserting result`);
return toResultingExample(assertable, example, context);
}, async (error) => {
context.logger.debug(`This example failed while trying to invoke the trigger function`);
// We still need to drain the assertable
context.logger.maintainerDebug(`Draining assertable and ignoring the result`);
await assertable.assert().catch();
if (error instanceof entities_1.VerifyTriggerReturnObjectError) {
return (0, contract_1.makeFailedExample)(example, (0, results_1.makeResults)((0, results_1.verificationError)(error, context)));
}
return (0, contract_1.makeFailedExample)(example, (0, results_1.makeResults)((0, results_1.executionError)(new entities_1.CaseConfigurationError('Failed during trigger function'), context), (0, results_1.executionError)(error, context)));
});
}, (error) => (0, contract_1.makeFailedExample)(example, (0, results_1.makeResults)((0, results_1.executionError)(error, context))))
.then((resultingExample) => {
(0, results_1.handleResult)(contract.recordExample(resultingExample, context), context['case:currentRun:context:testName'], context);
});
exports.executeExample = executeExample;