@case-contract-testing/case
Version:
Next-generation contract testing suite
75 lines (74 loc) • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeStateSetup = void 0;
const entities_1 = require("../../../../../entities");
const context_1 = require("../../../../../entities/context");
const types_1 = require("../../../../../entities/types");
const validateVariables_1 = require("./validateVariables");
const stateSetupHandler = (stateSetups, context) => (state) => {
const setupState = stateSetups[state.stateName];
if (setupState === undefined) {
context.logger.error(`No state handler for '${state.stateName}' was defined`);
return Promise.reject(new entities_1.CaseConfigurationError(`Missing state setup for '${state.stateName}'`));
}
return Promise.resolve()
.then(() => {
context.logger.maintainerDebug(`Calling state setup for '${state.stateName}'`);
return (0, types_1.isSetupFunction)(setupState) ? setupState() : setupState.setup();
})
.catch((e) => {
context.logger.error(`State setup '${state.stateName}' failed with the following error: "${e.message}"`, e);
context.logger.error(`Please check the implementation of the '${state.stateName}' state setup handler`);
throw new entities_1.CaseConfigurationError(`State setup '${state.stateName}' failed with the following error: "${e.message}". Please check the implementation of your state setup handler`);
});
};
const executeStateSetup = (example, stateSetups, parentContext) => Promise.resolve((0, context_1.addLocation)(`:stateSetup`, parentContext))
.then((context) => {
const variableSource = example.mock['case:run:context:setup'][context['case:currentRun:context:contractMode']].stateVariables;
context.logger.maintainerDebug(`Executing state setup handlers in '${context['case:currentRun:context:contractMode']}' mode: Variables obtained from ${variableSource}`);
if (variableSource === 'default') {
return Promise.resolve(example.states
.map((state) => {
context.logger.debug(`Setting up state '${state.stateName}' with default values`);
if (state['case:state:type'] === types_1.SETUP_VARIABLE_STATE) {
return Object.entries(state.variables).map(([key, value]) => context.addDefaultVariable(key, state.stateName, value));
}
return [];
})
.flat());
}
return Promise.resolve()
.then(async () => {
const result = [];
// Usually the following code is a mistake,
// but here we want to execute each handler in order
// So we turn off the usual lint rules on purpose.
// eslint-disable-next-line no-restricted-syntax
for (const state of example.states) {
context.logger.maintainerDebug(`Setting up state '${state.stateName}' with the provided handler`);
const stateContext = (0, context_1.addLocation)(`[${state.stateName}]`, context);
// eslint-disable-next-line no-await-in-loop
const variables = await (0, validateVariables_1.validateVariables)(state, stateContext, stateSetupHandler(stateSetups, stateContext));
result.push({
stateName: state.stateName,
variables: variables || {},
});
}
return result;
})
.then((stateSetupResults) => stateSetupResults
.map((state) => Object.entries(state.variables).map(([key, value]) => context.addStateVariable(key, state.stateName, value)))
.flat())
.catch((e) => {
context.logger.error(`Failed to execute state setup before test, not running test. See the logs above for more information`);
throw e;
});
})
.then((variables) => ({
...parentContext,
'case:currentRun:context:variables': variables.reduce((acc, [name, value]) => ({
...acc,
[name]: parentContext.descendAndStrip(value, (0, context_1.addLocation)(':contextVariables', parentContext)),
}), {}),
}));
exports.executeStateSetup = executeStateSetup;