UNPKG

playwright-bdd

Version:
67 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArityChecker = void 0; const helpers_1 = require("../../gherkin/helpers"); const utils_1 = require("../../utils"); const paths_1 = require("../../utils/paths"); class ArityChecker { constructor(featureUri, config) { this.featureUri = featureUri; this.config = config; this.errors = []; } checkStepDefinitionArity(matchedDefinition, pickleStep, gherkinStep) { if (!this.shouldCheckArity(matchedDefinition)) return; const stepFnArgs = matchedDefinition.definition.arity; const expectedArgs = this.getExpectedArgs(matchedDefinition, pickleStep); if (stepFnArgs >= expectedArgs.min && stepFnArgs <= expectedArgs.max) return; this.errors.push(this.formatError(matchedDefinition, { stepText: pickleStep.text, keyword: gherkinStep.keyword, location: gherkinStep.location, }, stepFnArgs, expectedArgs)); } getExpectedArgs(matchedDefinition, pickleStep) { let expectedArgs = matchedDefinition.result.length; if (pickleStep.argument?.dataTable || pickleStep.argument?.docString) { expectedArgs += 1; } if (isPlaywrightStyle(matchedDefinition)) { // For Playwright-style steps the fixtures object is always passed first by the runtime, // but when the step has no captures and no doc string / data table, // the user may omit args entirely. return expectedArgs === 0 ? { min: 0, max: 1 } : { min: expectedArgs + 1, max: expectedArgs + 1 }; } else { return { min: expectedArgs, max: expectedArgs }; } } shouldCheckArity(matchedDefinition) { return matchedDefinition.definition.providedOptions?.arityCheck ?? this.config.arityCheck; } // eslint-disable-next-line max-params formatError(matchedDefinition, step, stepFnArgs, expectedArgs) { const stepTextWithKeyword = (0, helpers_1.getStepTextWithKeyword)(step.keyword, step.stepText); const stepLocation = `${this.featureUri}:${(0, utils_1.stringifyLocation)(step.location)}`; const { definition } = matchedDefinition; const definitionLocation = `${(0, paths_1.relativeToCwd)(definition.uri)}:${definition.line}`; const expectedStr = expectedArgs.min === expectedArgs.max ? String(expectedArgs.min) : `${expectedArgs.min}-${expectedArgs.max}`; const actualArgsStr = stepFnArgs === 1 ? '1 argument' : `${stepFnArgs} arguments`; return [ `Step: ${stepTextWithKeyword} # ${stepLocation}`, `Pattern: ${definition.patternString} # ${definitionLocation}`, `Function has ${actualArgsStr}, but expected ${expectedStr}.`, ].join('\n'); } } exports.ArityChecker = ArityChecker; function isPlaywrightStyle({ definition }) { return !definition.isCucumberStyle() && !definition.isDecorator(); } //# sourceMappingURL=arity-checker.js.map