Skip to content

Latest commit

 

History

History
97 lines (75 loc) · 3.41 KB

README.md

File metadata and controls

97 lines (75 loc) · 3.41 KB

mocha-gherkin

NPM Version Build Status Coverage Status Dependency Status

mocha-gherkin is a Gherkin-style interface and reporter add-on for mocha. This add-on allows you to describe your features using Feature, Scenario, Given, When, Then, And, and But. You can use this interface together with mocha's BDD interface.

Usage in ECMAScript 6

Feature('Addition',
  'In order to avoid silly mistakes,',
  'As a math idiot',
  'I want to be told the sum of two numbers', () => {
    Scenario('Add two numbers', () => {
      let number1;
      let number2;
      let sum;

      Given('I have entered 50 into the calculator', () => {
        number1 = 50;
      });
      And('I have entered 70 into the calculator', () => {
        number2 = 70;
      });
      When('I press add', () => {
        sum = number1 + number2;
      });
      Then('the result should be 120 on the screen', () => {
        expect(sum).to.equal(120);
      });
    });
  });

Usage in ECMAScript 5

Feature('Addition',
  'In order to avoid silly mistakes,',
  'As a math idiot',
  'I want to be told the sum of two numbers', function () {
    Scenario('Add two numbers', function () {
      var number1;
      var number2;
      var sum;

      Given('I have entered 50 into the calculator', function () {
        number1 = 50;
      });
      And('I have entered 70 into the calculator', function () {
        number2 = 70;
      });
      When('I press add', function () {
        sum = number1 + number2;
      });
      Then('the result should be 120 on the screen', function () {
        expect(sum).to.equal(120);
      });
    });
  });

Installing

You should first have mocha installed:

$ npm install mocha

Then install mocha-gherkin, either globally or inside your project:

$ npm install mocha-gherkin

How to Use

Actually, mocha-gherkin consists of two different add-ons: interface add-on and reporter add-on. To use mocha-gherkin, you should first write your features using terms provided by the interface add-on. Then, run mocha with the options below:

$ mocha tests/addition.feature.js --ui mocha-gherkin --reporter mocha-gherkin/build/spec

Screenshot of mocha-gherkin

What's Going on?

Feature and Scenario wrap around mocha's describe, and Given, When, Then, And, and But do the same thing for mocha's it. describe, context, it, and other mocha's BDD lingos still work as usal, so you can mix in them with the Gherkin-style terms.

Reporter Support

mocha-gherkin has currently only one reporter add-on, which extends mocha's spec with support for the Gherkin-style interface. However, you can use other modules as usual since mocha-gherkin just wraps around mocha's BDD interface.