Defining Steps
Defining Steps
Let’s implement steps for our simple feature file and break down the code.
import { givenBuilder, whenBuilder, thenBuilder } from "@step-forge/step-forge";import { GivenState, WhenState, ThenState } from "./world";
givenBuilder<GivenState>() .statement("a valid step") .step(() => { return { valid: true, }; });
whenBuilder<GivenState, WhenState>() .statement("I check for valid input") .step(() => { return { valid: true, }; });
thenBuilder<GivenState, WhenState, ThenState>() .statement("the input is valid") .dependencies({ when: { valid: "required" } }) .step(({ when: { valid } }) => { expect(valid).toBe(true); });Breaking Down the Code
Given Builder
givenBuilderis used for defining given steps
Note: In standard Cucumber the Given, When, and Then clauses are interchangeable but in Step Forge they are not. This is because Step Forge has rules and restrictions for each section.
statementthis is the wording that will match the step language in your feature file. It can have variables, see the references for more information.stepthis is the function that will be called when the step is matched. You can execute any code you want here, and what you return will be loaded into yourGivenState. All builder types have access to reading fromGivenState, but onlygivenBuilderhas access to writing to it.
When Builder
whenBuilderis used for defining when stepsstatementthis is the wording that will match the step language in your feature file. It can have variables, see the references for more information.stepthis is the function that will be called when the step is matched. You can execute any code you want here, and what you return will be loaded into yourWhenState.WhenStatecan only be set fromwhenBuilderbut can be read by otherwhensteps orthensteps.
Then Builder
thenBuilderis used for defining then stepsstatementthis is the wording that will match the step language in your feature file. It can have variables, see the references for more information.dependenciesto use state from other steps you have to declare the pieces you need in thedependenciesobject.stepthis is the function that will be called when the step is matched. You can execute any code you want here, and what you return will be loaded into yourThenState.ThenStatecan only be set fromthenBuilderbut can be read by otherthensteps.
How It Works
When we run our tests using Cucumber it will walk through each step of the feature file sequentially. It will then match that step language to one of the steps we’ve defined in our step files.
For example, let’s say we just started running our tests from the feature file we created in the previous section and we’re on the very first step:
Feature: A simple feature
Scenario: A simple scenario -> Given a valid step When I check for valid input Then the input is validCucumber will match the Given a valid step to the givenBuilder we created in our step file.
givenBuilder<GivenState>() .statement("a valid step") .step(() => { return { valid: true, }; });After it executes the step we’ll have the following GivenState:
const world: World = { given: { valid: true, },};Cucumber will then continue to the next step in the feature file and execute the WhenBuilder step.
whenBuilder<GivenState, WhenState>() .statement("I check for valid input") .step(() => { return { valid: true, }; });After it executes the step we’ll have the following scenario state:
const world: World = { given: { valid: true, }, when: { valid: true, },};Cucumber will then continue to the next step in the feature file and execute the ThenBuilder step.
thenBuilder<GivenState, WhenState, ThenState>() .statement("the input is valid") .dependencies({ when: { valid: "required" } }) .step(({ when: { valid } }) => { expect(valid).toBe(true); });There are two new things happening here:
- We’re using the
dependenciesobject to tell Step Forge that theThenBuilderstep depends on theWhenBuilderstep.
In order to access scenario state from a step function you must declare the pieces you need in the dependencies object.
- We’re using the
stepfunction to read thevalidproperty from theWhenState.
If you try to read the valid property without declaring it in the dependencies object you’ll get an error.
Since our Then step doesn’t return anything, scenario state does not change, and as long as valid is indeed true, or false, our test will pass.
Next Steps
Now that we’ve the basics of how Step Forge works, dive deeper into the Concepts section or directly to the References to learn more about the different builder types and how to use them.