Back to the academy

Modern BDD with Gherkin in Vitest and Playwright (co-gherkin)

Given / When / Then scenarios the business can read, running directly in Vitest and Playwright, with Scenario Outlines and data tables.

Intermediate · Gherkin · co-gherkin · Vitest · Playwright

Given / When / Then scenarios the business can read

A Gherkin scenario describes a behaviour in the business vocabulary: the context (`Given`), the action (`When`) and the observable outcome (`Then`). Product, QA and development read the same text, and that text runs. The key is describing behaviours, not clicks: "rejects an email that is already registered" ages far better than "clicks the blue button and waits two seconds".

❌ Antipattern

Scenario: test login
  Given I open "/login"
  When I click "#btn-3"
  And I wait 2 seconds
  Then I see "div.alert"

✅ Stoic pattern

Feature: Sign up
  Scenario: Rejects an email that is already registered
    Given a user registered as "ana@example.com"
    When someone signs up with "ana@example.com"
    Then they see "This email is already in use"

Key takeaways

  • Write behaviours in business language, not sequences of clicks and selectors.
  • One scenario, one rule: if you need "and also", it is probably two scenarios.
  • Steps are reusable contracts: the same `Given` serves many scenarios.

Running Gherkin directly in Vitest

With co-gherkin, the `.feature` file is loaded from the Vitest test itself and every step is bound to a function with typed parameters such as `{int}` or `{string}`. There is no separate runner and no compilation step: scenarios run alongside the rest of the unit and integration tests, with the same speed, the same coverage and the same watch mode.

Example

import { describeFeature, loadFeature } from 'co-gherkin';

const feature = await loadFeature('./checkout.feature');

describeFeature(feature, ({ Scenario }) => {
  Scenario('Applying a discount code', ({ Given, When, Then }) => {
    Given('a cart with a {int} EUR item', (amount) => { /* ... */ });
    When('the user applies coupon {string}', (code) => { /* ... */ });
    Then('the payable total is {int} EUR', (total) => { /* ... */ });
  });
});

Key takeaways

  • The `.feature` lives next to the code it tests and runs in the same suite.
  • Step definitions are functions: an unimplemented step fails instead of silently passing.
  • No extra runner: same watch mode, same coverage, same pipeline.

Scenario Outline, data tables and E2E with Playwright

When the same rule is checked against several inputs, a `Scenario Outline` with its `Examples` table avoids copying scenarios: each row is a case. Data tables describe complex inputs, such as a cart or a portfolio, in a readable way. And the same steps can be backed by Playwright to walk through the real application in a browser when the behaviour only makes sense end to end.

❌ Antipattern

Scenario: Discount for 120
  When the order total is 120
  Then the customer pays 108

Scenario: Discount for 80
  When the order total is 80
  Then the customer pays 80

✅ Stoic pattern

Scenario Outline: 10% off orders above 100
  When the order total is <total>
  Then the customer pays <paid>

  Examples:
    | total | paid |
    | 120   | 108  |
    | 100   | 100  |
    | 80    | 80   |

Key takeaways

  • An `Examples` table turns edge cases into rows: adding one costs a single line.
  • Always include the boundary (exactly 100): that is where bugs live.
  • Keep E2E for flows that only make sense end to end; everything else, in Vitest.