npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-gherkin-runner

v1.0.8

Published

node module that run tests using mocha, protractor or jasmine-node using gherkin

Downloads

11

Readme

node-gherkin-runner

Node module that transforme feature files into full javascript (using https://github.com/gregorylimoratto/gherkin-specs-api.git and run then using mocha, jasmine-node or protractor

Any feedback is appreciated !

Usage

Install

node-gherkin-runner is available as an npm module

Install locally with

npm install node-gherkin-runner --save-dev

Features

The examples are written with Gherkin language : https://github.com/cucumber/cucumber/wiki/Gherkin

Feature: Calculator 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
		Given I have entered 50 into the calculator
		And I have entered 70 into the calculator
		When I press add
		Then the result should be 120 on the screen

	@ignore
	Scenario: Add two other numbers
		Given I have entered 10 into the calculator
		And I have entered 79 into the calculator
		When I press add
		Then the result should be 89 on the screen
		And failed the test

Each Gherkin Feature will become a jasmine/mocha/protractor describe

And each Scenario will become a it

Some tags will allow you to ignore feature execution : jasmine/mocha/protractor xdescribe() / xit()

  • @ignore - ignore Feature or Scenario
  • @ignoreOthers - ignore all other feature / scenario exept those with this tag

Javascript Specs

The module will look for feature files (gherkin) in process.cwd() and all subdirectories.

The featureSteps() function host a set of steps that will be available for each feature that match the featureSteps regexp

	var gherkin = require('node-gherkin-runner');
	gherkin.api.featureSteps(/Calculator/)
		.given(/I have entered (.*) into the calculator/, function(num){
			// this step is for each feature that contains Calculator in title 
		});

	gherkin.api.featureSteps('Calculator addition')
		.when('I press add', function(){
			// this one is not shared for "Calculator substraction"
		});

given/when/then step can be string or regular expression that match a step in the feature file.

	var gherkin = require('node-gherkin-runner');
	gherkin.api.featureSteps(/Addition/)
		.given(/I have entered (.*) into the calculator/, function(num){
			this.numbers = this.numbers || [];
			this.numbers.push(parseInt(num));
		})
		.when('I press add', function(){
			this.result = this.numbers.reduce(function(a,b){ return a + b },0);
		})
		.then(/the result should be (\d+) on the screen/, function(expectedSum){
			expect(this.result).toBe(parseInt(expectedSum));
		})
		.then('failed the test', function(){
			expect(true).toBe(false);
		});

Each step is executed on an isolated scope (this) which can hold current scenario state. (reset for each scenario)

You can add test initialize and cleanup :

	var gherkin = require('node-gherkin-runner');
	gherkin.api.featureSteps('Addition')
	 	.before(function () {
			...
	    })
		.after(function(){
			...
		})
	...

To install :

npm install node-gherkin-runner --save-dev

And just include the module as a file to test in your test runner

Example gruntfile using mocha (and grunt-mocha-test) :

	mochaTest: {
      test: {
        options: {
          reporter: 'spec'
        },
        src: ['tests/**/*.js', 'node_module/node-gherkin-runner/lib/gherkin-runner.js']
      }
    }

Examples

Using protractor :

demo.feature:

Feature: angularjs homepage

Background:
	Given I browse angular website
	
Scenario: Must greet the user

	Given I insert 'Greg' in the "yourName" field
	When I look at the greeting message
	Then I see 'Hello Greg!'

Scenario: Must display a pre-set todo list
	
	Given The todo list is displayed by default with 2 elements
	When I look at the todo elements
	Then There is 2 elements in todo list
	And The text for the number 2 is 'build an angular app'

Scenario: Must add a todo when Add button is click

	Given The todo list is displayed by default with 2 elements
	When I insert 'write a protractor test using gherkin' in the input field
	And I click on Add button
	And I look at the todo elements
	Then There is 3 elements in todo list
	And The text for the number 3 is 'write a protractor test using gherkin'

demo.feature-specs.js

	var gherkin = require('node-gherkin-runner');

(function(){
	'use strict';
	gherkin.api.featureSteps(/angularjs homepage/)
		.given(/I browse angular website/, function(){
			browser.get('http://www.angularjs.org');
		})
		.given(/I insert '(.*)' in the "yourName" field/, function(name){
			element(by.model('yourName')).sendKeys(name);
		})
		.when(/I look at the greeting message/, function(){
			 this.message = element(by.binding('yourName'));
		})
		.then(/I see '(.*)'/, function(message){
			expect(this.message.getText()).toEqual(message);
		})
		.given(/The todo list is displayed by default with 2 elements/, function(){
			// nothing to do
		})
		.when(/I look at the todo elements/, function(){
			this.todos = element.all(by.repeater('todo in todoList.todos'));
		})
		.then(/There is (\d+) elements in todo list/,function(number){
			expect(this.todos.count()).toEqual(parseInt(number));
		})
		.then(/The text for the number (\d+) is '(.*)'/, function(number, text){
			expect(this.todos.get(number-1).getText()).toEqual(text);
		})
		.when(/I insert '(.*)' in the input field/, function(text){
			var addTodo = element(by.model('todoList.todoText'));
			addTodo.sendKeys(text);
		})
		.when(/I click on Add button/, function(){
			var addButton = element(by.css('[value="add"]'));
      		addButton.click();
		})
})();

e2e.config (protractor) :

var gherkinRunnerPath = require('node-gherkin-runner').runnerPath;
gherkinRunnerPath = gherkinRunnerPath.replace(__dirname+'\\', '');
console.log(gherkinRunnerPath);
exports.config = {
  framework: 'jasmine2',
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['spec/demo.feature-specs.js', gherkinRunnerPath]
}