Coder Social home page Coder Social logo

mocha-gherkin's Introduction

mocha-gherkin

A little tool to generate Mocha BDD specs from Gherkin features

Installation

npm i mklabs/mocha-gherkin --save-dev

Description

You might now cucumber. There is now a robust JavaScript based cucumber implementation \o/ with cucumber-js. This tool is not a replacement or even trying to compete with cucumber-js. If you want a real and valid cucumber implementation, search no more and head over the cucumber-js readme. A really neat project, worth watching which will get better and better.

Mocha is another tool I recently found , and I do like it. A lot. I felt like I needed and I could do a little tool to generate my mocha BDD tests stub.

It's not a test framework. It's not a cucumber-js replacement. It's not wrapping mocha to run the test, or defining any kind of report or mocha plugin.

This tool simply take a feature file, written in Gherkin syntax, as input, and output the matching Mocha BDD tests for this feature file. It has some notion of step definition (kinda) and can output you the missing step definition as well.

mocha-gherkin(1)

Usage: cat some.feature | ./node_modules/.bin/mocha-gherkin

Options:

  -v, --version     Output package version
  -h, --help        Output this help text
  -s, --step        Path to a step definition (kinda)
  -m, --missing     Will output missing step definition
                    instead of default generated test

Usage

$ cat file.feature | mocha-gherkin > test.js

with some step (like):

$ cat file.feature | mocha-gherkin -s ./steps/step-definition.js > test.js

API

The parser is a readable / writable stream, so you can pipe any stream connected to a valid feature like:

Feature: Example feature
  As a very lazy programmer
  I want to write some feature file
  So that I can quickly make my mocha

  Scenario: Reading the tests
    Given I am in the test directory
    When I read the index.js file
    Then I should see "new Parser" somewhere

to a new Parser, and pipe it to a given destination.

var Parser = require('mocha-gherkin');
fs.createReadStream('local.feature')
  .pipe(new Parser)
  .pipe(process.stdout);

You'll get matching Mocha BDD style specs:

describe("Example feature", function() {

  describe("As a very lazy programmer I want to write some feature file So that I can quickly make my mocha", function() {

    describe("Reading the tests", function() {

      it("Given I am in the test directory", function() {

      });

      it("When I read the index.js file", function() {

      });

      it("Then I should see 'new Parser' somewhere", function() {

      });

    });

  });

});

Steps definition (kinda)

You can fill in the body of it() handlers with some kind of step definitions (definitely not valid cucumber step definitions.. but kind of)

var parser = new Parser({
  step: fs.readFileSync('./steps.js', 'utf8')
});

The step.js file is a JavaScript file running in a new vm context, with Given, When, Then and And function available. Each one takes two argument, a regexp to match and a callback to read and use as a body function for mocha describe() and it().

Captured parameters are available as $1, $2 and so on, these placeholders get replaced by their relevant values from feature description.

Given(/I am in the "(.*)" directory/, function(done) {
  this.base = process.cwd();
  process.chdir(path.resolve('$1'));
  done();
});

When(/I read the "(.*)" file/, function(done) {
  this.file = fs.createReadStream('$1');
  this.file.pause();
  done();
});

And(/I pipe it through a "(.*)"/, function(done) {
  var self = this;
  this.output = '';
  this.parser = this.file.pipe($1)
    .on('data', function(c) { self.output += c })
    .on('close', done);

  this.file.resume();
});

Then(/I should see the content of "(.*)"/, function(done) {
  process.chdir(this.base);
  var output = this.output;
  fs.readFile('$1', 'utf8', function(err, body) {
    if(err) return done(err);
    assert.equal(body.trim(), output.trim());
    done();
  });
});

Step snippets

When --missing option is set, the output won't return the usual generated step but rather return you snippets for any step missing. It's handy to quicly generate the matching step file for a given feature file. Example:

$ cat example.feature | mocha-gherkin --missing

Should output:

Given(/I am in the test directory/, function() {
  // add code for your definition here, regexp captured paramaters can be used
  // in the function body with simple placeholders like $1, $2, ...
});

When(/I read the index.js file/, function() {
  // add code for your definition here, regexp captured paramaters can be used
  // in the function body with simple placeholders like $1, $2, ...
});

Then(/I should see 'new Parser' somewhere/, function() {
  // add code for your definition here, regexp captured paramaters can be used
  // in the function body with simple placeholders like $1, $2, ...
});

mocha-gherkin's People

Contributors

mklabs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mocha-gherkin's Issues

hogan dependency renamed in hogan.js

This project is not running anymore since the rename of hogan into hogan.js.
Furthermore the hogan.js reached the v3 release.

I did not check if it works straight forward by updating the name and the version.

Best Regards,
Julien

Doc: How to install

Hey,

thank you for this library. It looks very good. I'm just wondering how to install? You already did write a package.json, why didn't you published on npm?

best regards
Philipp

this.listener[event] is not a function

Hi,
After installing the same using npm, while trying to execute "cat sample.feature | ./node_modules/.bin/mocha-gherkin > test.js" getting error like

/cucumberMocha/testProj/node_modules/gherkin/lib/gherkin/lexer/en.j
s:960
this.listener[event](this.keyword, name, description, this.current_line);
^
TypeError: this.listener[event] is not a function
at Lexer.store_keyword_content (/cucumberMocha/testProj/node_mo
dules/gherkin/lib/gherkin/lexer/en.js:960:23)

Thanks in advance....

Lack of support for 'Examples' keyword

Hi!

Do You still maintain this repo? I would like to use it, but there's no support for Examples keyword ( look here ).

Is there any possibility for adding this functionality? Would You eventually accept PR?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.