Cucumber.js
Cucumber, the popular Behaviour-Driven Development tool, brought to your JavaScript stack.
It runs on both Node.js and modern web browsers.
Development status
Cucumber.js is still a work in progress. Here is its current status.
Cucumber Technology Compatibility Kit
Feature | Status |
---|---|
Core (scenarios, steps, mappings) | Done |
Background | Done1 |
Comments | Done |
Command-line interface | Done1, 2 |
Command-line options | Todo2 |
Data tables | Done |
Doc strings | Done |
Failing steps | Done |
Hooks | Done |
I18n | To do |
JSON formatter | WIP3 |
Pretty formatter | WIP2 |
Scenario outlines and examples | To do |
Stats collector | To do |
Step argument transforms | To do |
Tags | Done |
Undefined steps | Done |
Wire protocol | To do |
World | Done |
- Not certified by Cucumber TCK yet.
- Considered for removal from Cucumber TCK.
- Missing 'matches' attributes. Simple wrapper for Gherkin's
JsonFormatter
pending porting of:
- https://github.com/cucumber/gherkin/blob/master/lib/gherkin/listener/formatter_listener.rb
- https://github.com/cucumber/gherkin/blob/master/lib/gherkin/formatter/filter_formatter.rb
in Gherkin itself.
Cucumber.js-specific features
Feature | Status |
---|---|
Background | Done |
CoffeeScript support | Done |
Command-line interface | Done |
- Will be certified by Cucumber TCK.
Prerequesites
Cucumber.js is tested on:
- Node.js 0.6, 0.8 and 0.10.0 (see CI builds)
- Google Chrome
- Firefox
- Safari
- Opera
There are plans to have CI builds on browsers too.
Usage
Install
Cucumber.js is available as an npm module.
Install globally with:
$ npm install -g cucumber
OR
You may also define cucumber.js as a development dependency of your application by including it in a package.json file.
// package.json
Then install with npm install --dev
Features
Features are written with the Gherkin syntax
# features/myFeature.featureFeature: Example featureAs a user of cucumber.jsI want to have documentation on cucumberSo that I can concentrate on building awesome applicationsScenario: Reading documentationGiven I am on the Cucumber.js GitHub repositoryWhen I go to the README fileThen I should see "Usage" as the page title
Support Files
Support files let you setup the environment in which steps will be run, and define step definitions. Both JavaScript (.js
) and CoffeeScript (.coffee
) source files are supported.
World
World is a constructor function with utility properties, destined to be used in step definitions:
// features/support/world.jsvar zombie = ;var {thisbrowser = ; // this.browser will be available in step definitionsthis {thisbrowser;};; // tell Cucumber we're finished and to use 'this' as the world instance};exportsWorld = World;
It is possible to tell Cucumber to use another object instance than the constructor:
// features/support/world.jsvar zombie = ;var {thisbrowser = ; // this.browser will be available in step definitionsvar world ={thisbrowser;};; // tell Cucumber we're finished and to use our world object instead of 'this'};exportsWorld = WorldConstructor;
Step Definitions
Step definitions are the glue between features written in Gherkin and the actual SUT (system under test). They are written in JavaScript.
All step definitions will run with this
set to what is known as the World in Cucumber. It's an object exposing useful methods, helpers and variables to your step definitions. A new instance of World
is created before each scenario.
Step definitions are contained within one or more wrapper functions.
Those wrappers are run before executing the feature suite. this
is an object holding important properties like the Given()
, When()
and Then()
functions. Another notable property is World
; it contains a default World
constructor that can be either extended or replaced.
Step definitions are run when steps match their name. this
is an instance of World
.
// features/step_definitions/myStepDefinitions.jsvar {thisWorld = World; // overwrite default World constructorthis;this;this;};moduleexports = myStepDefinitionsWrapper;
It is also possible to use simple strings instead of regexps as step definition patterns:
this;
'I have $count "$string"'
would translate to /^I have (.*) "([^"]*)"$/
.
Hooks
Hooks can be used to prepare and clean the environment before and after each scenario is executed.
Before hooks
To run something before every scenario, use before hooks:
// features/support/hooks.js (this path is just a suggestion)var {this;};moduleexports = myHooks;
After hooks
The before hook counterpart is the after hook. It's similar in shape but is executed, well, after every scenario:
// features/support/after_hooks.js var { this;}; moduleexports = myAfterHooks;
Around hooks
It's also possible to combine both before and after hooks in one single definition with the help of around hooks:
// features/support/advanced_hooks.js { this;}; moduleexports = myAroundHooks;
Tagged hooks
Hooks can be conditionally elected for execution based on the tags of the scenario.
// features/support/hooks.js (this path is just a suggestion)var {this;};moduleexports = myHooks;
Run cucumber
Cucumber.js includes a binary file to execute the features.
If you installed cucumber.js globally, you may run it with:
$ cucumber.js
You may specify the features to run:
$ cucumber.js features/my_feature.feature
And require specific step definitions and support code files with the --require option:
$ cucumber.js features/my_feature.feature --require features/step_definitions/my_step_definitions.js
If you installed Cucumber locally or with npm install --dev
, you'll need to specify the path to the binary:
$ ./node_modules/.bin/cucumber.js
Note to Windows users: invoke Cucumber.js with cucumber-js
instead of cucumber.js
. The latter is causing the operating system to invoke JScript instead of Node.js, because of the so-called file extension.
Examples
A few example apps are available for you to browse:
Contribute
See CONTRIBUTE.
Help & support
- Twitter: @cucumber_js
- IRC: #cucumber on Freenode
- Google Groups: cukes
- cukes.info