UNPKG

@amiceli/vitest-cucumber

Version:

vitest tools to use Gherkin feature in unit tests

67 lines (66 loc) 2.7 kB
import { SyntaxKind } from 'ts-morph'; import { generateScenarii } from '../../../scripts/generateFile'; import { StepAst } from './StepAst'; import { StepableAst } from './StepableAst'; export class ScenarioAst extends StepableAst { constructor(options) { super(options); } static fromOptions(options) { return new ScenarioAst(options); } handleScenarii() { const scenariiArrow = this.getScenariiArrowFunction(); const scenariiToAdd = this.getMissingScenarri(scenariiArrow); const scenariiToRemove = this.getScenariiToRemove(scenariiArrow); for (const s of scenariiToRemove) { if (this.shouldComment) { this.commentExpression(this.stepableParentFunction, s.callExpression); } else { this.removeChildFromParent(this.stepableParentFunction, s.callExpression); } } this.stepableParentFunction.addStatements(generateScenarii(scenariiToAdd || [], this.forRule)); for (const scenario of this.stepableParent.scenarii) { const scenarioArrowFunction = this.getScenarioArrowFunction(scenario); if (scenarioArrowFunction) { StepAst.fromOptions({ ...this.options, stepParent: scenario, stepParentFunction: scenarioArrowFunction, }).handleSteps(); this.updateStepableArguments(scenario, scenarioArrowFunction); } } } getScenarioArrowFunction(scenario) { const list = this.getScenariiArrowFunction(); const scenarioFunction = list.find((s) => s.name === scenario.description); if (scenarioFunction) { return scenarioFunction.callExpression .getArguments() .find((arg) => arg.isKind(SyntaxKind.ArrowFunction)); } } getScenariiToRemove(parentScenarii) { return parentScenarii.filter((scenario) => { return (scenario.name && this.stepableParent.scenarii .map((s) => s.description) .includes(scenario.name) === false); }); } getMissingScenarri(parentScenarii) { return this.stepableParent.scenarii.filter((scenario) => { return (parentScenarii .map((s) => s.name) .includes(scenario.description) === false); }); } getScenariiArrowFunction() { return this.callExpressionMatchRegExp(this.stepableParentFunction, this.forRule ? /\b(RuleScenario|RuleScenarioOutline)\(/ : /\b(Scenario|ScenarioOutline)\(/); } }