UNPKG

@contract-case/case-core

Version:

Core functionality for the ContractCase contract testing suite

46 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeStateTeardown = void 0; const case_entities_internal_1 = require("@contract-case/case-entities-internal"); const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const stateTeardownHandler = (stateSetups, state, parentContext) => Promise.resolve((0, case_plugin_base_1.addLocation)(`[${state.stateName}]`, parentContext)).then((context) => Promise.resolve() .then(async () => { const stateFn = stateSetups[state.stateName]; if (stateFn !== undefined && !(0, case_entities_internal_1.isSetupFunction)(stateFn)) { context.logger.maintainerDebug(`Calling state teardown for '${state.stateName}'`); await stateFn.teardown(); } else { context.logger.maintainerDebug(`No state teardown exists for '${state.stateName}'`); } }) .catch((e) => { context.logger.error(`State teardown for '${state.stateName}' failed with: ${e.message}`, e); context.logger.error(`Please check the implementation of the '${state.stateName}' state teardown function`); throw new case_plugin_base_1.CaseConfigurationError(`State teardown '${state.stateName}' failed: ${e.message}. Please check the implementation of your state teardown handler`, context, 'UNDOCUMENTED'); })); const executeStateTeardown = (example, stateSetups, parentContext) => Promise.resolve((0, case_plugin_base_1.addLocation)(':stateTeardown', parentContext)).then((context) => { const variableSource = example.mock['_case:run:context:setup'][context['_case:currentRun:context:contractMode']].stateVariables; if (variableSource === 'default') { context.logger.maintainerDebug(`Not executing state teardown handlers, since run mode is '${context['_case:currentRun:context:contractMode']}'. Variables obtained from ${variableSource}`); return Promise.resolve(); } context.logger.maintainerDebug(`Executing state teardown handlers in '${context['_case:currentRun:context:contractMode']}' mode: Variables obtained from ${variableSource}`); return Promise.resolve() .then(async () => { // 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) { // eslint-disable-next-line no-await-in-loop await stateTeardownHandler(stateSetups, state, context); } }) .catch((e) => { context.logger.error(`Test may have passed, but at least one state teardown failed`, e.message); throw new case_plugin_base_1.CaseConfigurationError(`State teardown function threw an error: ${e.message}`, context, 'UNDOCUMENTED'); }); }); exports.executeStateTeardown = executeStateTeardown; //# sourceMappingURL=executeStateTeardown.js.map