bencompton / jest-cucumber

Execute Gherkin scenarios in Jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for repeated steps

jan-musi opened this issue · comments

On this line, an error is thrown if a feature's step count doesn't match its step definition count. However, this precludes features with repeated steps.

Example:

Scenario: Walking to my friend's house
  Given I am at my house
  And I walk two blocks north
  And I walk one block east
  And I walk two blocks north
  When I look east
  Then I should see my friend's house

I currently cannot reuse one definition like this:

and("I walk two blocks north", async () => {
  ...
}

because then I would receive the error Scenario "Walking to my friend's house" has 6 step(s) in the feature file, but 5 step definition(s) defined.

Additionally, if I tried to reuse one step definition for all walking, like this:

and(/^I walk (.+?) blocks? (.+?)$/, async (blockCount: string, direction: string) => {
  ...
}

then I would receive the error Scenario "Walking to my friend's house" has 6 step(s) in the feature file, but 4 step definition(s) defined.

I imagine that others would value support for this use case, but I see that this behavior is very much unexpected/unsupported. Is this because Gherkin features are not supposed to repeat themselves?

Thank you for this great library! :)

Same issue, can you provide some feedback?

This is actually by design. In the default "Jest mode", Jest Cucumber expects your steps to match your feature files exactly, and the goal is that you keep your Jest tests in sync with your feature files. Duplicated code can be reduced by following the pattern described here. If you prefer behavior similar to Cucumber where step definitions are defined once and are matched globally to feature file steps, then you might want to look into autoBindSteps.