UNPKG

skutter

Version:

Skutter: Opinionated BDD Browser based test framework

101 lines (100 loc) 4.73 kB
"use strict"; const Yadda = require("yadda"); const box_wrapper_1 = require("../framework/services/box-wrapper"); const new_guid_1 = require("../framework/services/new-guid"); const executor_1 = require("../framework/executor/executor"); const execution_marks_1 = require("../framework/executor/execution-marks"); let YaddaInterpreter = Yadda.Interpreter; let className = "StepActions"; class StepActions { static run(executionContext, ...steps) { let methodName = "run"; checkSpecified(className, methodName, "steps", steps); checkSpecified(className, methodName, "executionContext", executionContext); if (!executionContext.skutter_internal.libraryProvider) { throw new Error("No libraries are specified for delegation."); } let step = steps.shift(); let promiseChain = StepActions.internal_run(executionContext, step); step = steps.shift(); while (step) { promiseChain.then(() => { return StepActions.internal_run(executionContext, step); }); step = steps.shift(); } return promiseChain; } static internal_run(executionContext, stepText) { let methodName = "internal_run"; if (stepText === "") { throw new Error(`[${className}].[${methodName}] stepText has not been specified.`); } let stepTextKeyword = stepText.substring(0, stepText.indexOf(" ")); switch (stepTextKeyword) { case "Given": case "When": case "Then": case "And": break; default: throw new Error(`[${className}].[${methodName}] stepText "${stepText}" does not start with a supported keyword [Given,When,Then,And].`); } let [top, text, bottom] = box_wrapper_1.BoxWrapper.wrap(stepText); console.log(`>> ${top}`); console.log(`>> ${text}`); console.log(`>> ${bottom}`); let promise = new Promise((resolve, reject) => { let interpreter = new YaddaInterpreter(executionContext.skutter_internal.libraryProvider.get()); let broadcaster = executionContext.skutter_internal.broadcaster; let logCapture = executionContext.skutter_internal.logCapture; let subStepId = new_guid_1.newGuid(); let meta = executionContext.context.meta; let stepDepth = 1; let ancestorStepId = meta.stepId; let parentStepId = meta.subStepId || meta.stepId; logCapture.mark(execution_marks_1.ExecutionMarks.StepStart, subStepId); broadcaster.subStepStarted(meta.featureId, meta.scenarioId, ancestorStepId, parentStepId, subStepId, stepText, stepDepth) .then(() => { executionContext.context.meta.subStepId = subStepId; executionContext.context.meta.step; try { interpreter.interpret_step(stepText, executionContext, (err) => { logCapture.mark(execution_marks_1.ExecutionMarks.StepEnd); let broadcastPromise = null; if (err) { if (executor_1.Executor.isFailure(err)) { broadcastPromise = broadcaster.subStepFail(meta.featureId, meta.scenarioId, subStepId, logCapture.getStepLogs(subStepId), err); } else { broadcastPromise = broadcaster.subStepError(meta.featureId, meta.scenarioId, subStepId, logCapture.getStepLogs(subStepId), err); } } else { broadcastPromise = broadcaster.subStepPass(meta.featureId, meta.scenarioId, subStepId, logCapture.getStepLogs(subStepId)); } broadcastPromise.then(() => { meta.subStepId = undefined; if (err) { reject(err); } else { resolve(); } }); }); } catch (yaddaException) { reject(yaddaException); } }); }); return promise; } } exports.StepActions = StepActions; function checkSpecified(className, functionName, parameterName, parameterValue) { if (!parameterValue) { throw new Error(`[${className}].[${functionName}] ${parameterName} is not specified.`); } }