├── .gitignore ├── README.md ├── features ├── calculator.feature ├── step_definitions │ ├── README.md │ └── calculator.js └── support │ ├── README.md │ ├── hooks.js │ └── world.js ├── lib └── calculator.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #nodejs-cucumber-sample 2 | 3 | Minimal project to show how to setup 4 | [cucumber](http://cukes.info/) for nodejs environment 5 | 6 | 7 | ## Prerequesites 8 | 9 | * [Node.js](http://nodejs.org) 10 | * [NPM](http://npmjs.org) 11 | * [cucumber-js](https://github.com/cucumber/cucumber-js) 12 | 13 | ##How to use 14 | 15 | * Install cucumber-js globally with: 16 | ``` shell 17 | $ npm install -g cucumber 18 | ``` 19 | * Clone this repository 20 | * Then install all required dependencies with `npm install --dev` 21 | * Finally run calculator sample test `npm test` 22 | 23 | After that you may use this project as a base of your development 24 | 25 | ## Credits 26 | All credit goes to [cucumber team](http://cukes.info), [Node.js community](http://nodejs.org) and 27 | [cucumber-js](https://github.com/cucumber/cucumber-js) authors 28 | 29 | ##Licence 30 | MIT 31 | -------------------------------------------------------------------------------- /features/calculator.feature: -------------------------------------------------------------------------------- 1 | Feature: Calculate 2 | 3 | Scenario: Add two numbers 4 | Given the input "2+2" 5 | When the calculator is run 6 | Then the output should be "4" 7 | 8 | Scenario: Subtract two numbers 9 | Given the input "9-4" 10 | When the calculator is run 11 | Then the output should be "5" -------------------------------------------------------------------------------- /features/step_definitions/README.md: -------------------------------------------------------------------------------- 1 | #step_definitions directory 2 | 3 | Put your step definitions in this directory 4 | 5 | If you dont understand what step are please 6 | visit the guide [cucumber-js](https://github.com/cucumber/cucumber-js#step-definitions) 7 | 8 | -------------------------------------------------------------------------------- /features/step_definitions/calculator.js: -------------------------------------------------------------------------------- 1 | /** 2 | * calculate step 3 | */ 4 | 5 | module.exports = function() { 6 | var self = this; 7 | 8 | this.Given(/^the input "([^"]*)"$/, function(input, callback) { 9 | self.expression = input; 10 | callback(); 11 | }); 12 | 13 | this.When(/^the calculator is run$/, function(callback) { 14 | self.result = self.calculator.run(self.expression); 15 | callback(); 16 | }); 17 | 18 | this.Then(/^the output should be "([^"]*)"$/, function(output, callback) { 19 | self.expect(Number(self.result)).to.equal(Number(output)); 20 | callback(); 21 | }); 22 | } -------------------------------------------------------------------------------- /features/support/README.md: -------------------------------------------------------------------------------- 1 | #support directory 2 | 3 | Contains support files that let you setup the environment in which steps will be run, and define step definitions. 4 | 5 | If you dont understand what to put in support directory please visit [cucumber-js](https://github.com/cucumber/cucumber-js#support-files) -------------------------------------------------------------------------------- /features/support/hooks.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @function 3 | * 4 | * Hooks can be used to prepare and clean 5 | * the environment before and after 6 | * each scenario is executed 7 | */ 8 | module.exports = function() { 9 | /** 10 | * To run something before every scenario, 11 | * use before hooks 12 | * 13 | * @param {Function} callback a done callback from cucumber.js 14 | */ 15 | this.Before(function(callback) { 16 | 17 | //don't forget to tell cucumber when your done 18 | callback(); 19 | }); 20 | 21 | 22 | /** 23 | * To run something after every scenario, 24 | * use after hooks 25 | * 26 | * @param {Function} callback a done callback from cucumber.js 27 | */ 28 | this.After(function(callback) { 29 | 30 | //don't forget to tell cucumber when your done 31 | callback(); 32 | }); 33 | } -------------------------------------------------------------------------------- /features/support/world.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @function 3 | * 4 | * world is a constructor function 5 | * with utility properties, 6 | * destined to be used in step definitions 7 | */ 8 | var cwd = process.cwd(); 9 | var path = require('path'); 10 | 11 | var Calculator = require(path.join(cwd, 'lib', 'calculator')); 12 | 13 | module.exports = function() { 14 | this.calculator = new Calculator(); 15 | this.expect = require('chai').expect; 16 | } -------------------------------------------------------------------------------- /lib/calculator.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @function 3 | * 4 | * simple calculator 5 | */ 6 | var calculator = module.exports = function() {}; 7 | /** 8 | * simple calculate implementation 9 | * @param String expression an input expression to evaluate 10 | * @return String result of expression evaluation 11 | */ 12 | calculator.prototype.run = function(expression) { 13 | return eval(expression); 14 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-cucumber-sample", 3 | "version": "0.0.1", 4 | "description": "Minimal project to show how to setup [cucumber](http: //cukes.info/) for nodejs environment", 5 | "scripts": { 6 | "test": "cucumber.js" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/lykmapipo/nodejs-cucumber-sample.git" 11 | }, 12 | "bugs": { 13 | "url": "https://github.com/lykmapipo/nodejs-cucumber-sample/issues" 14 | }, 15 | "homepage": "https://github.com/lykmapipo/nodejs-cucumber-sample", 16 | "keywords": [ 17 | "cucumberjs", 18 | "bdd", 19 | "setup", 20 | "bootstrap", 21 | "tdd", 22 | "test", 23 | "testing", 24 | "gherkin", 25 | "cucumber" 26 | ], 27 | "author": { 28 | "name": "Lally Elias", 29 | "email": "lallyelias87@gmail.com" 30 | }, 31 | "license": "MIT", 32 | "devDependencies": { 33 | "chai": "^1.10.0" 34 | }, 35 | "engines": { 36 | "node": ">=0.10.0" 37 | } 38 | } --------------------------------------------------------------------------------