UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

84 lines (83 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verificationError = exports.executionError = exports.failedExpectationError = exports.matchingError = exports.errorWhen = void 0; const context_1 = require("../../entities/context"); const types_1 = require("./types"); const errorWhen = (test, err) => (test ? [err].flat() : []); exports.errorWhen = errorWhen; /** * This represents a mismatched matcher * * @param matcher - The matcher that generated this error * @param message - The message that describes this error * @param actual - The actual value that was recieved * @param context - The match context this occurred in * @param expected - An optional expected value (might be a description of what was expected) * @returns CaseError */ const matchingError = (matcher, message, actual, context, expected) => ({ type: types_1.ERROR_TYPE_MATCHING, matcher, message, expected: expected || ('case:matcher:example' in matcher ? matcher['case:matcher:example'] : context.descendAndStrip(matcher, context)), actual, location: context['case:currentRun:context:location'], toString: () => `${(0, context_1.locationString)(context)}: ${message} (${matcher['case:matcher:type']})`, }); exports.matchingError = matchingError; /** * This represents a mismatch during a case execution that isn't covered by a * matcher (usually this is a misconfiguration that is not well described by a * {@link ../../entities/errors#CaseConfigurationError | CaseConfigurationError}) * * @param message - The message that describes this error * @param actual - The actual value that was recieved * @param code - A code that can be looked up in the documentation * @param context - The match context this occurred in * @param expected - An optional expected value (might be a description of what was expected) * @returns CaseError */ const failedExpectationError = (message, actual, code, context, expected) => ({ type: types_1.ERROR_TYPE_RAW_MATCH, message, expected, actual, code, location: context['case:currentRun:context:location'], toString: () => `${(0, context_1.locationString)(context)}: ${message}`, }); exports.failedExpectationError = failedExpectationError; /** * This represents an error thrown by user code during execution of an example * (eg, when a user's trigger is called) * * @param error - The error thrown * @param context - the context that the error was in * @returns ExecutionError */ const executionError = (error, context) => ({ type: types_1.ERROR_TYPE_EXECUTION, message: error.message, code: error.name, location: context['case:currentRun:context:location'], }); exports.executionError = executionError; /** * This represents an error thrown during verification using the provided * verification function * * @param error - The VerifyTriggerReturnObjectError from the verification * @param context - the context that the error was in * @returns VerificationError */ const verificationError = (error, context) => ({ type: types_1.ERROR_TYPE_TEST_RESPONSE, message: error.message, code: error.name, error, location: context['case:currentRun:context:location'], }); exports.verificationError = verificationError;