UNPKG

@contract-case/case-plugin-base

Version:

Plugin framework for writing plugins for the ContractCase test framework

76 lines 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.failedExpectationError = exports.matchingError = exports.errorWhen = void 0; const context_1 = require("../../context"); const errors_types_1 = require("../errors.types"); /** * Helper function that will return a {@link CaseError} if the test condition is met, or a non-erroring {@link MatchResult} otherwise. * * @public * @param test - a boolean condition * @param err - either an error or an array of errors * @returns a {@link MatchResult} containing the errors if `test` is true, or a passing {@link MatchResult} otherwise. */ const errorWhen = (test, err) => (test ? [err].flat() : []); exports.errorWhen = errorWhen; /** * Creates a mismatched matcher expectations error * * @public * * @param matcher - The matcher that generated this error * @param message - The message that describes this error * @param actual - The actual value that was received * @param context - The match context this occurred in * @param expected - An optional expected value. If this is not provided or is `undefined`, it is calculated using `descendAndStrip` * @param annotations - Optional annotations to provide additional information for the user about the values printed * @returns CaseError */ const matchingError = (matcher, message, actual, context, expected, annotations) => ({ type: errors_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']})`, ...(annotations != null ? { annotations } : {}), }); exports.matchingError = matchingError; /** * This represents a mismatch during a case execution that isn't covered by a * matcher (usually this is an expectation failure during a MockInteraction which * means we can't run the matching engine for some reason). * * Most of the time you won't need to use this, because most matching errors * come from matchers. Errors encountered during matcher execution should use * {@link matchingError} instead. * * @public * * * @param message - The message that describes this error * @param actual - The actual value that was received * @param code - A code that can be looked up in the documentation. This should * be a unique code specific to this kind of error that users * could look up in the documentation for more information. * @param context - The match context this occurred in * @param expected - An optional expected value (might be a description of what was expected) * @param annotations - Optional annotations to provide additional information for the user about the values printed * @returns CaseError */ const failedExpectationError = (message, actual, code, context, expected, annotations) => ({ type: errors_types_1.ERROR_TYPE_RAW_MATCH, message, expected, actual, code, location: context['_case:currentRun:context:location'], toString: () => `${(0, context_1.locationString)(context)}: ${message}`, ...(annotations != null ? { annotations } : {}), }); exports.failedExpectationError = failedExpectationError; //# sourceMappingURL=errors.js.map