UNPKG

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

Version:

ContractCase core function plugin, allowing validation of arbitrary functions

45 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupMockFunctionExecution = 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 setupMockFunctionExecution = ({ request: expectedArguments, response: returnValue, functionName, }, parentContext) => Promise.resolve((0, case_plugin_base_1.addLocation)(`mockFunction[${functionName}]`, parentContext)).then((context) => { // create mock function let data = null; const f = (...stringArgs) => { data = { actualArguments: stringArgs.map((s, index) => { try { return JSON.parse(s); } catch (e) { throw new case_plugin_base_1.CaseConfigurationError(`Parsing error in argument number ${index} in function '${functionName}': ${e.message}\n The argument was: ${s}`); } }), }; context.logger.debug('Mock function was invoked with arguments:', data.actualArguments); const strippedReturnValue = context.descendAndStrip(returnValue, (0, case_plugin_base_1.addLocation)('returnValue', context)); context.logger.debug('Returning', strippedReturnValue); return JSON.stringify(strippedReturnValue); }; return { config: { '_case:mock:type': case_core_plugin_function_dsl_1.MOCK_FUNCTION_EXECUTION, stateVariables: context['_case:currentRun:context:variables'], functions: { [functionName]: f }, mock: { functionHandle: functionName }, }, assertableData: () => Promise.resolve(data).then((result) => { if (data == null) { context.logger.debug('The mock function appears not to have been invoked'); } return { actual: result ? result.actualArguments : null, context: (0, case_plugin_base_1.addLocation)('arguments', context), expected: expectedArguments, }; }), }; }); exports.setupMockFunctionExecution = setupMockFunctionExecution; //# sourceMappingURL=mockFunctionExecution.js.map