Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News BDD and JavaScript Using CucumberJS

BDD and JavaScript Using CucumberJS

Lire ce contenu en français

CucumberJS is a new tool that when added to the TDD workflow for JavaScript-based projects embraces the ideas of Behaviour Driven Development, BDD, allowing a developer to follow the TDD principles while developing from the outside in; running automated tests that fail until code that supports a feature is implemented, Todd Anderson reveals in a recent blog post.

Todd, an application developer and co-author of three books, describes CucumberJS as a port of the BDD tool Cucumber to JavaScript running both on NodeJS and modern web browsers. It allows definitions of feature specifications to be written in Gherkin and these specifications to be run using a command line tool which will report pass or fail of the steps and scenarios building up a specification. He notes though that Cucumber is only the testing framework, it's up to a developer to include an assertion library that will make these steps pass or fail.

Todd uses an earlier grocery list application as an example of how to adapt to BDD using Cucumber, giving one example of a feature:

Feature: Shopper can add an item to their Grocery List
Scenario: Item added to grocery list
Given I have an empty grocery list
When I add an item to the list
Then the grocery list contains a single item

The feature defines a business value, while the scenario defines the steps that provide that value. Each scenario has a list of steps defined by the keywords Given, When, Then; steps that CucumberJS will execute after having read the feature specification.
Using CucumberJS together with NodeJS, Todd walks through some examples of implementing and running the grocery list application while moving from all tests failing to all pass.

Todd concludes with a belief that there is a reward in having code under a test harness when it comes to refactoring and bug fixing, and with an ambition to follow up with new posts addressing how he uses CucumberJS to run tests integrating with a browser.

BDD was developed around 2006 by Dan North.
Cucumber is an open source tool for Behaviour Driven development, BDD, currently for nine programming languages, including JavaScript.

Rate this Article