Karma framework to write tests with gherkin for jasmine
npm install karma-jasmine-featurejavascript
featureSteps(/Calculator/)
.given(/I have entered (.*) into the calculator/, function(num){
// this step is for each feature that contains Calculator in title
});
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.
`javascript
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 (.*) 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 :
`javascript
featureSteps('Addition')
.before(function () {
module('calculator'); // angular ng mock
var scope = null;
inject(function (_$injector_) {
scope = _$injector_.get('$rootScope').$new();
});
this.scope = scope;
})
.after(function(){
...
})
...
`
To install :
npm install karma-jasmine-feature --save-dev
Then, reference framework in karma.conf.js (require jasmine framework)
frameworks: ['jasmine', 'jasmine-feature'],
And just include feature files and specs in karma.conf.js
files: [
...
'features/*/.feature'
...
]
#### Examples
demo.feature:
Feature: Roman numerals
Background:
Given I have a Roman numerals calculator
Scenario Outline: The calculator should transform simple roman numeral to number
Given I enter '' in the calculator
When I convert the roman numeral
Then the displayed value is ''
Examples:
| roman | number |
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
Scenario: Should add two complex roman numerals
Given I enter 'IX' in the calculator
And I enter 'III' in the calculator
When I press add
Then the displayed value is 'XII'
@ignore
Scenario: Should be ignore
Given A scenario with no js implementation
When I include this scenario
Then Nothing happens
demo.feature-specs.js
`javascript
(function(){
'use strict';
featureSteps(/Roman numerals/)
.given(/I have a Roman numerals calculator/, function(){
this.calculator = new Calculator();
})
.given(/I enter '(.*)' in the calculator/, function(roman){
this.calculator.setInput(roman);
})
.when(/I convert the roman numeral/, function(){
this.calculator.convert();
})
.when(/I press add/, function(){
this.calculator.add();
})
.then(/the displayed value is '(.*)'/, function(num){
expect(this.calculator.getDisplayedValue()).toBe(num);
});
})();
``