Cucumber for Javascript

Notes on tutorials of Cucumber School

Adam00
3 min readNov 20, 2022

The focus on cucumber is in BDD style. It emphaizes on nontechnical documentation with code support for verification.

Code examples are from Cucumber School.

1. Use Cucumber Expression rather than Regular Expression

It is more intuitive to non-programmers.

(?:I/You) have {int} cucumber(s) in my belly/stomach \(amazing!)

# {int} - type (int, float, word, string, and {} as any
# (s) - optional
# belly/stomach - either belly or stomach
# \(amazing!) - escape match `(amazing!)` in text
# (?:I/You) - optional, and it is not capture as a parameter

Try the expression here
Check the syntax here

2. Try Custom Parameters to clean your step definition

import { defineParameterType } from '@cucumber/cucumber';

defineParameterType({
name: 'color',
regexp: /red|blue|yellow/,
transformer: s => new Color(s),
});

3. Group similar steps together

Feature: a feature
Scenario: a scenario
Given a table step
| Vegetable | Rating |
| Apricot | 5 |
| Brocolli | 2 |
| Cucumber | 10 |
import { Given } from '@cucumber/cucumber';
import * as assert from 'assert';

Given(/^a table step$/, function(table) {
const expected = [
{'Vegetable': 'Apricot', 'Rating': '5'},
{'Vegetable': 'Brocolli', 'Rating': '2'},
{'Vegetable': 'Cucumber', 'Rating': '10'},
];
assert.deepEqual(table.hashes(), expected);
});

Check here to further details

4. Function declaration Over Arrow Function

Global states can be shared and be accessed across step definition using function declaration.

When(
"{?:person} go(es) to {destination} by {vehicle}",
function (destination, vehicle) {
const vehicle = Vechicle();
this.timeTaken = vehicle.travelTime(destination);
}
);

Then(
"It spends {time}",
function(expectedTime) {
assert.equal(this.timeTaken, expectedTime);
}
);

5. Using re-run plugin for BDD to reduce additional re-run

Run the test suite with formatter rerun

npm test -- -f rerun:@rerun.txt # will export the result to rerun.txt

Say the scenario failed, the filererun.txt will be generated with the following content (using the line number filtering format)

http://features/hear_shot.feature:38

You can re-run the failed scenario by

npm test -- @rerun.txt

6. Make use of BRIEF heuristics

Think carefully whether cucumber is a testing tool or documentation tool. A lengthy steps have no benefit as a dcoumentation. If you want someone to go for it, you need to know how to write a prose in gherkin.

B is for Business Language
use plain language, avoid domain jargon

R is for Real Data
use more approachable names (like Peter, Mary)

I is for Intention Revealing
only reveal relevant detail, usually those will be observed by users

E is for Essential
further refined/removed steps after the intention revealing

F is for Focused
make it precise

7. Declarative Over Imperative

When you are imperative, you are exactly instructing in details.
Like programming, imperative style is to command the computer sequence entirely.

However, if you are going for declarative you only need to describe the actual outcome, things like the execution sequence, or how the computer achieves the outcome, you might not have a full context. It is very good for nontechnical people to know that.

8. BDD Terms to go further

Three Amigos — Tester, Developer and Product Owner
Example Mapping — Set up an example to go further (Business Rules, Real Life Examples, Questions and Assumptions, Simplify the rules)
Incidental Details
Discovery, formulation and Automation — find unknown unknown
Deliberate Discovery

Resource:

Cucumber School (JS)

--

--

Adam00

Let's go invent tomorrow instead of worrying about what happened yesterday.