UNPKG

@contract-case/case-core-plugin-function

Version:

ContractCase core function plugin, allowing validation of arbitrary functions

55 lines 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupMockFunctionCaller = void 0; const case_core_plugin_function_dsl_1 = require("@contract-case/case-core-plugin-function-dsl"); const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const entities_1 = require("../entities"); const validateArray = (maybeArray, context) => { if (!Array.isArray(maybeArray)) { throw new case_plugin_base_1.CaseConfigurationError('Arguments did not resolve to an array', context); } return maybeArray; }; const setupMockFunctionCaller = ({ request: expectedArguments, response: returnValue, functionName: functionHandle, }, context) => Promise.resolve().then(() => { const callerArguments = validateArray(context.descendAndStrip(expectedArguments, (0, case_plugin_base_1.addLocation)('callerArguments', context)), context); return { config: { '_case:mock:type': case_core_plugin_function_dsl_1.MOCK_FUNCTION_CALLER, stateVariables: context['_case:currentRun:context:variables'], functions: {}, mock: { functionHandle }, }, assertableData: () => Promise.resolve().then(async () => { try { context.logger.debug(`Invoking function by handle '${functionHandle}', with arguments`, callerArguments); if (!functionHandle) { throw new case_plugin_base_1.CaseConfigurationError('There was no functionName set to use as a handle to call this function. Please check the contract definition for this test.', context); } const result = await context.invokeFunctionByHandle(functionHandle, callerArguments); context.logger.maintainerDebug(`Function '${functionHandle}' returned`, result); if ((0, entities_1.isObject)(result)) { if ('success' in result) { context.logger.debug(`Function '${functionHandle}' returned`, result['success']); } if ('errorClassName' in result) { context.logger.debug(`Function '${functionHandle}' threw error of kind '${result['errorClassName']}'${'message' in result ? ` with message: ${result['message']}` : ''}`); } } return { actual: result, context, expected: returnValue, }; } catch (e) { if (e instanceof case_plugin_base_1.CaseConfigurationError) { throw e; } context.logger.error(`Function '${functionHandle}' threw an error`, e.message); throw new case_plugin_base_1.CaseConfigurationError(`The provided function threw an error during execution:\n ${e.message}`, context); } }), }; }); exports.setupMockFunctionCaller = setupMockFunctionCaller; //# sourceMappingURL=mockFunctionCaller.js.map