UNPKG

@amiceli/vitest-cucumber

Version:

vitest tools to use Gherkin feature in unit tests

73 lines (72 loc) 2.52 kB
import { ItemAlreadyExistsError, StepAbleStepExpressionError, StepAbleStepsNotCalledError, StepAbleUnknowStepError, } from '../../errors/errors'; import { ExpressionStep } from '../expression/ExpressionStep'; import { Taggable } from './Taggable'; import { Step } from './step'; export class StepAble extends Taggable { isCalled; _steps; title; constructor(title) { super(); this.title = title; this.isCalled = false; this._steps = []; } stepFailedExpressionMatch = {}; findStepByTypeAndDetails(type, details) { this.stepFailedExpressionMatch[details] = 0; return this._steps.find((step) => { try { const sameType = step.type === type; const sameDetails = step.details === details; if (ExpressionStep.stepContainsRegex(details)) { const params = ExpressionStep.matchStep(step, details); return sameType && (sameDetails || params.length >= 0); } return sameType && sameDetails; } catch (e) { this.stepFailedExpressionMatch[details] += 1; return false; } }); } hasUnCalledSteps() { return this.getNoCalledStep() !== undefined; } getNoCalledStep() { return this._steps.find((s) => s.isCalled === false); } addStep(newStep) { const duplicatedStep = this._steps.find((step) => { return step.getTitle() === newStep.getTitle(); }); if (duplicatedStep) { throw new ItemAlreadyExistsError(this, newStep); } this._steps.push(newStep); } checkIfStepWasCalled() { const step = this.getNoCalledStep(); if (step) { throw new StepAbleStepsNotCalledError(this, step); } } checkIfStepExists(stepType, stepDetails) { const foundStep = this.findStepByTypeAndDetails(stepType, stepDetails); if (!foundStep) { if (this.stepFailedExpressionMatch[stepDetails] === this._steps.length) { throw new StepAbleStepExpressionError(this, new Step(stepType, stepDetails)); } throw new StepAbleUnknowStepError(this, new Step(stepType, stepDetails)); } return foundStep; } get lastStep() { return this._steps[this._steps.length - 1]; } get steps() { return this._steps; } }