@case-contract-testing/case
Version:
Next-generation contract testing suite
42 lines (41 loc) • 3.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateVariables = void 0;
const entities_1 = require("../../../../../entities");
const results_1 = require("../../../../../entities/results");
const types_1 = require("../../../../../entities/types");
const validateVariables = async (state, context, stateHandler) => stateHandler(state).then(async (variables) => {
if (state['case:state:type'] === types_1.SETUP_VARIABLE_STATE) {
if (typeof variables === 'undefined') {
throw new entities_1.CaseConfigurationError(`The state setup for '${state.stateName}' did not recieve any variables. Did you forget to return an object in the state setup function? (Expected an object with keys ${Object.keys(state.variables)})`);
}
// Validate state setup variables
const missingVariables = Object.keys(state.variables).filter((variableName) => variables[variableName] === undefined);
if (missingVariables.length > 0) {
const message = `The state setup for '${state.stateName}' was expected to return an object with the variables [${Object.keys(state.variables)}], but the following were missing: [${missingVariables}]`;
context.logger.error(message);
throw new entities_1.CaseConfigurationError(message);
}
const extraVariables = Object.keys(variables).filter((variableName) => state.variables[variableName] === undefined);
if (extraVariables.length > 0) {
const message = `The state setup for '${state.stateName}' recieved extra variables [${extraVariables}], but it was only expecting [${Object.keys(state.variables)}]. This is probably a misconfigured state handler, and might cause failures later.`;
context.logger.warn(message);
}
const matchResult = await context.descendAndCheck(state.variables, context, variables);
if ((0, results_1.hasErrors)(matchResult)) {
context.logger.error(`The state setup for '${state.stateName}' returned a value that didn't match the expected matcher:`);
matchResult.forEach((e) => {
context.resultPrinter.printError(e, context);
});
throw new entities_1.CaseConfigurationError(`The state setup for '${state.stateName}' did not return the expected variables`);
}
context.logger.debug(`The state setup for '${state.stateName}' returned successfully; with the following variables:`, variables);
return variables;
}
if (typeof variables !== 'undefined') {
context.logger.warn(`The state '${state.stateName}' was not configured to return any variables, but still recieved variables from the state handler. Remove the return statement from the state handler or correct the state definition in the consumer contract to fix this error.`);
}
context.logger.debug(`The state setup for '${state.stateName}' returned successfully; with no variables`);
return {};
});
exports.validateVariables = validateVariables;