Typescript annotations for Cucumber
npm install cucumber_annotationsgherkin
# features/simple_math.feature
Feature: Simple maths
In order to do maths
As a developer
I want to increment variables
Scenario: easy maths
Given a variable set to 1
When I increment the variable by 1
Then the variable in words would read two
Scenario Outline: much more complex stuff
Given a variable set to
When I increment the variable by
Then the variable should contain
Examples:
| var | increment | result |
| 100 | 5 | 105 |
| 99 | 1234 | 1333 |
| 12 | 5 | 18 |
`
`typescript
// features/steps/simple_math_world.ts
import { Given, When, Then, Type } from "cucumber_annotations"
import { expect } from 'chai'
const NUMBERS_IN_WORDS = ['one', 'two', 'three']
const NUMBERS_IN_WORDS_PATTERN = RegExp( NUMBERS_IN_WORDS.join( '|' ) )
export class SimpleMathWorld {
private result = 0
@Given( 'a variable set to {int}' )
setTo( n: number ){ this.result = n }
@When('I increment the variable by {int}')
incrementBy(n: number) { this.result += n }
// custom types can be annotated too
@Type( 'number_in_words', NUMBERS_IN_WORDS_PATTERN )
wordToNumber( text: string ) { return NUMBERS_IN_WORDS.indexOf( text ) + 1 }
// multiple annotations are allowed
@Then('the variable should contain {int}')
@Then('the variable in words would read {number_in_words}')
shouldBe( n: number ) { expect(this.result).to.eql(n) }
}
`
/*
* The export for the SimpleMathWorld class is required, as the Typescript
* compiler otherwise would complain that it is: "declared but never used."
*/
`json
# tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true
}
}
`
* Run:
`s
> cucumber-js --require-module ts-node/register --require "features/scripts/*/.ts"
...........F
Failures:
1) Scenario: much more complex stuff # features\simple_maths.feature:20
√ Given a variable set to 12 # src\annotations\steps.ts:25
√ When I increment the variable by 5 # src\annotations\steps.ts:25
× Then the variable should contain 18 # src\annotations\steps.ts:25
AssertionError
+ expected - actual
-17
+18
at SimpleMathWorld.shouldBe (cucumber_annotations\features\scripts\simple_maths_steps.ts:24:51)
at CustomWorld.delegate_to_SimpleMathWorld_shouldBe (cucumber_annotations\src\worlds\annotated_world.ts:83:15)
4 scenarios (1 failed, 3 passed)
12 steps (1 failed, 11 passed)
0m00.003s
``