@amiceli/vitest-cucumber
Version:
vitest tools to use Gherkin feature in unit tests
73 lines (72 loc) • 2.31 kB
JavaScript
import { FeatureUknowRuleError, ItemAlreadyExistsError, RuleNotCalledError, } from '../../errors/errors';
import { DefineBackground, DefineScenario, } from '.';
import { DefineRule, Rule } from './Rule';
import { ScenarioParent } from './ScenarioParent';
export class Feature extends ScenarioParent {
_rules;
withoutGherkin;
constructor(name, title = 'Feature', withoutGherkin = false) {
super(name, title);
this._rules = [];
this.withoutGherkin = withoutGherkin;
}
getRuleByName(name) {
return this._rules.find((rule) => rule.name === name);
}
getFirstRuleNotCalled(options) {
return this._rules.find((rule) => rule.isCalled === false &&
(options.includeTags.length <= 0 ||
rule.matchTags(options.includeTags) === true) &&
rule.matchTags(options.excludeTags) === false);
}
haveAlreadyCalledRule() {
return this._rules.some((rule) => rule.isCalled === true);
}
checkUncalledRule(options) {
const uncalled = this.getFirstRuleNotCalled(options);
if (uncalled) {
throw new RuleNotCalledError(uncalled);
}
return this;
}
checkIfRuleExists(ruleName) {
const foundRule = this.getRuleByName(ruleName);
if (!foundRule) {
throw new FeatureUknowRuleError(this, new Rule(ruleName));
}
return foundRule;
}
mustHaveScenarioOrRules() {
if (this._rules.length > 0) {
for (const rule of this._rules) {
rule.mustHaveScenario();
}
}
else {
this.mustHaveScenario();
}
}
addRule(newRule) {
const duplicatedRule = this._rules.find((rule) => {
return rule.getTitle() === newRule.getTitle();
});
if (duplicatedRule) {
throw new ItemAlreadyExistsError(this, newRule);
}
this._rules.push(newRule);
}
get rules() {
return this._rules;
}
}
export class DefineFeature extends Feature {
getBackground() {
return new DefineBackground();
}
getScenario(description) {
return new DefineScenario(description);
}
checkIfRuleExists(ruleName) {
return new DefineRule(ruleName);
}
}