Joseph Wilk

Joseph Wilk

Programming bits and bobs

Testing Javascript With Cucumber in Javascript

I recently created an adapter in Cucumber which provides support for writing step definitions in Javascript. So as a Javascript programmer you can test your code with Cucumber without having to write any Ruby.

It does this through TheRubyRacer (http://github.com/cowboyd/therubyracer written by Charles Lowell) which provides Ruby bindings to V8 (http://code.google.com/p/v8/).

Lets look at an example:

The Feature

Feature: Fibonacci
  In order to calculate super fast fibonacci series
  As a Javascriptist
  I want to use Javascript for that

  @fibonacci
  Scenario Outline: Series
    When I ask Javascript to calculate fibonacci up to <n>
    Then it should give me <series>

    Examples:
      | n   | series                                 |
      | 1   | []                                     |
      | 2   | [1, 1]                                 |
      | 3   | [1, 1, 2]                              |
      | 4   | [1, 1, 2, 3]                           |
      | 6   | [1, 1, 2, 3, 5]                        |
      | 9   | [1, 1, 2, 3, 5, 8]                     |
      | 100 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |

The Step Definitions

1
2
3
4
5
6
7
8
9
10
11
12
Before(['@fibonacci'], function(){
  fibResult = 0;
});

When(/^I ask Javascript to calculate fibonacci up to (\d+)$/, function(n){
  assertEqual(0, fibResult)
  fibResult = fibonacciSeries(n);
});

Then(/^it should give me (\[.*\])$/, function(expectedResult){
  assertEqual(expectedResult, fibResult)
});

The Cucumber Javascript API

The best reference for the Javascript Api is the examples within the Cucumber source: http://github.com/aslakhellesoy/cucumber/tree/master/examples/javascript/features/

I have tried to make the Javascript Api as close to the Cucumber Ruby Api as possible. However it currently does not support a couple of things the Ruby version is capable of: Calling step definitions from within step definitions with multiline arguments and giving line reporting on step definitions.

Loading your Javascript code into the World

The most important difference to take note of in the Javascript Api compared with the Ruby one is how we load code into the World so it is in scope within the step definitions.

Given the following folder structure:

my_js_project/lib/code_lives_here.js
my_js_project/features/support/env.js
my_js_project/features/my_feature.feature

Within our features/support/env.js setup file we would have:

1
2
//Cucumber resolves the files relative to the folder that contains the features folder.
World(['lib/code_lives_here.js'])

The code within code_lives_here.js would be availabe in the step definitions.

Full Javascript Example

You can see the full example in the Cucumber project:

http://github.com/aslakhellesoy/cucumber/tree/master/examples/javascript/features/

Feedback

This Javascript Cucumber adapter represents an experiment to see how well we can use Cucumber through Javascript and V8. I would love to hear ideas and feedback on the Javascript Api.

Comments