Beginner Tutorial: BDD Testing with Cucumber (TypeScript)
Prerequisites

Behavior Driven Development (BDD) helps teams write tests in human-readable language.
Tools like Cucumber allow writing test scenarios using Gherkin syntax (Given–When–Then).
This tutorial will show how to:
Install Cucumber
Configure TypeScript
Write a Feature file
Implement Step Definitions
Run the test
Steps to Run Cucumber
Create a Project
Following steps to create project -
javascript1mkdir cucumber-bdd-demo2cd cucumber-bdd-demo3npm init -yInstall Dependencies
Install Cucumber.js with TypeScript support.
javascript1npm install --save-dev @cucumber/cucumber2npm install --save-dev typescript ts-node3npm install --save-dev @types/nodeConfigure TypeScript
Create
tsconfig.jsonjavascript1{2 "compilerOptions": {3 "target": "ES6",4 "module": "commonjs",5 "strict": true,6 "esModuleInterop": true,7 "outDir": "dist"8 }9}Project Structure
Project structure is given below -
javascript1cucumber-bdd-demo2│3├── features4│ ├── login.feature5│ └── step-definitions6│ └── loginSteps.ts7│8├── tsconfig.json9└── package.jsonCreate a Feature File
features/login.featurejavascript1Feature: Login functionality23 Scenario: Successful login4 Given the user is on the login page5 When the user enters valid username and password6 Then the user should see the dashboardInfoExplanation: Feature → describes system capability Scenario → test case Given / When / Then → test steps
Create Step Definitions
features/step-definitions/loginSteps.tsjavascript1import { Given, When, Then } from '@cucumber/cucumber';2import assert from 'assert';34let loggedIn = false;56Given('the user is on the login page', function () {7 console.log("User navigates to login page");8});910When('the user enters valid username and password', function () {11 loggedIn = true;12});1314Then('the user should see the dashboard', function () {15 assert.strictEqual(loggedIn, true);16 console.log("Login successful. Dashboard displayed.");17});Add Test Script
Update
package.jsonjavascript1"scripts": {2 "test": "cucumber-js --require-module ts-node/register --require features/**/*.ts"3}Run the Test
To run test -
javascript1npm testSuccessExpected Output: Feature: Login functionality Scenario: Successful login ✓ Given the user is on the login page ✓ When the user enters valid username and password ✓ Then the user should see the dashboard 1 scenario (1 passed) 3 steps (3 passed)
Advantages of BDD
Using Cucumber provides:
Collaboration between QA, developers, and business
Readable test scenarios
Behavior-focused testing
Living documentation
Example Real Use Case
BDD is widely used with automation tools like:
Playwright
Selenium
Cypress