UNPKG

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

Version:

ContractCase core function plugin, allowing validation of arbitrary functions

46 lines 2.53 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 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.debug(`Function '${functionHandle}' returned`, result); return { actual: result, context: (0, case_plugin_base_1.addLocation)('returnValue', 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