Setting Up Cucumber
Setting Up Cucumber
Install Cucumber
There is no need to install cucumber explicitly, it will be installed as a dependency when you install Step Forge.
Cucumber Setup and Configuration
Create a cucumber.mjs file
There are lots of options here which you can read about in the Cucumber configuration docs. But here is a basic sample setup:
const defaultProfile = { format: [ process.env.CI || !process.stdout.isTTY ? "progress" : "progress-bar", ], parallel: 1, requireModule: ["ts-node/register"], require: ["./features/steps/**/*.ts", "./features/steps/*.ts"], strict: false,};export default defaultProfile;formatthis is the format of the output you want to see in the console. There are other formatters available, see the Cucumber formatters page for more information.parallelthis is the number of parallel processes you want to run.requireModulethis allows Cucumber to process TypeScript files.requirethis is the path to your step definitions.strictthis is whether to fail the build if there are any pending steps.
Create a ./features directory
This is where you will create your feature files and define your steps. We recommend the following structure:
-> /features -> /steps -> world.ts -> steps.ts -> (... other step files) -> sample.feature -> (... other feature files)world.tsthis is where you will define your states and do any global setup.steps.tsthis is what we call the file for our examples, but it can be named anything you want.(... other step files)this is where you will define your other steps. You can break this out into multiple files if you want to.sample.featurethis is what we call the file for our examples, but it can be named anything you want.(... other feature files)this is where you will define your other feature files.
Create a ./features/steps/world.ts file
This is where you will define your states and do any global setup. The most important part for Step Forge specifically is setting up your World object with the appropriate state.
Your World object is a class that is used to store the state of the currently executing scenario. It is what carries information between steps. Step Forge has some very specific requirements for how scenario state is setup in your World object. Here is the basic setup we’ll use for our examples:
import { BasicWorld } from "@step-forge/step-forge";
export interface GivenState { valid: boolean;}
export interface WhenState { valid: boolean;}
export interface ThenState {}
export class World extends BasicWorld<GivenState, WhenState, ThenState> {}
setWorldConstructor(World);-
BasicWorldis a class that is provided by Step Forge that divides your state into three categories:Given,When, andThen. -
GivenStateis the state for theGivensteps. You can add any properties you want with any types that are relevant to your tests.GivenStatecan be read by all steps, but can only be written to byGivensteps. -
WhenStateis the state for theWhensteps. You can add any properties you want with any types that are relevant to your tests.WhenStatecan be read byWhenandThensteps, but can only be written to byWhensteps. -
ThenStateis the state for theThensteps. You can add any properties you want with any types that are relevant to your tests.ThenStateread or written only byThensteps.Note:
ThenStateis used mostly for assertions on complex state and is rarely used. -
class Worldthis is your custom World class that you can
Note: GivenState, WhenState, and ThenState represent all values that any step can use across all of your tests. Step Forge handles narrowing this down for you in individual steps.
Want to learn more?
If you want to dive deeper into the many configuration options available for Cucumber, check out the Cucumber JS docs on GitHub.