UNPKG

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

Version:

ContractCase core function plugin, allowing validation of arbitrary functions

88 lines (86 loc) 4.67 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 validateFunctionResponse = (maybeFunctionResponse, context) => { if (maybeFunctionResponse === null || typeof maybeFunctionResponse !== 'object') { const message = `Bad function return value in interaction. This interaction had a function response that wasn't an object. Please update the function definition to be a ReturnValue or a FunctionThrows matcher. `; context.logger.error(`${message} The defined return type was:`, maybeFunctionResponse); throw new case_plugin_base_1.CaseConfigurationError(message, context, 'BAD_INTERACTION_DEFINITION'); } const data = maybeFunctionResponse; if ('success' in data) { return data; } if ('errorClassName' in data) { return data; } const message = `Bad function return value in interaction. This interaction had a function response that didn't contain a 'success' or 'errorClassName' key. Please update the function definition to be a ReturnValue or a FunctionThrows matcher.`; context.logger.error(`${message} The defined return type was:`, maybeFunctionResponse); throw new case_plugin_base_1.CaseConfigurationError(message, context, 'BAD_INTERACTION_DEFINITION'); }; const setupMockFunctionExecution = ({ request: expectedArguments, response: expectedResponse, 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 \n This happens if non-JSON arguments are passed to the mock function.\n\n The argument was: ${s}\n `, context); } }), }; context.logger.debug('Mock function was invoked with arguments:', data.actualArguments); const functionResponse = validateFunctionResponse(context.descendAndStrip(expectedResponse, context), context); context.logger.maintainerDebug('functionResponse was', functionResponse); if ('success' in functionResponse) { context.logger.debug('Returning', functionResponse.success); return JSON.stringify({ success: functionResponse.success, }); } if ('errorClassName' in functionResponse) { context.logger.debug(`Returning error of kind '${functionResponse.errorClassName}'${'message' in functionResponse ? ` with message: ${functionResponse.message}` : ''}`); return JSON.stringify({ errorClassName: functionResponse.errorClassName, ...('message' in functionResponse ? { message: functionResponse.message } : {}), }); } context.logger.error('Bad function return type description.', functionResponse); throw new case_plugin_base_1.CaseConfigurationError("This interaction had a bad return type description. It must contain either a 'success' key (indicating a successful return), or a 'kind' ", context, 'BAD_INTERACTION_DEFINITION'); }; 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