Jest Cucumber Fusion
Write 'pure' cucumber test in Jest without syntax clutter
![npm downloads](https://img.shields.io/npm/dm/jest-cucumber-fusion.svg?style=flat-square)
Overview
Build on top of Jest-cucumber, Jest-Cucumber-Fusion handle the writing of the corresponding Jest test steps using an uncluttered cucumber style.
Instead of using describe
and it
blocks, you instead write a Jest test for each scenario, and then define Given
, When
, and Then
step definitions inside of your Jest tests.
jest-cucumber-fusion then allows you to link these Cucumber tests to your javascript Cucumber feature steps.
Adding Fusion
, jest-cucumber-fusion then make the links between and build the necessary scaffolding for jest-cucumber to do its job.
Now allowed to use jest naturally in your project like you would use native Cucumber.
Motivation
Jest-cucumber is an amazing project but forces you to write a lot of repetitive scaffolding code to setup the link betwen Jest and Cucumber.
With Jest-Cucumber-Fusion, it really takes only the minimal code possible:
- a Cucumber Feature file with gherkin sentences
- a Cucumber Feature step file with your javascript validation code, ended with the
Fusion
function to link the two
Getting Started
Install Jest Cucumber Fusion:
npm install jest-cucumber-fusion --save-dev
Add a Feature file:
###filename: rocket-launching.feature
Feature: Rocket Launching
Scenario: Launching a SpaceX rocket
Given I am Elon Musk attempting to launch a rocket into space
When I launch the rocket
Then the rocket should end up in space
And the booster(s) should land back on the launch pad
And nobody should doubt me ever again
Add the following to your package.json configuration:
"jest": { "testMatch": [ "**/*.steps.js" ] }
Add a your Cucumber feature step definition file and load Fusion
const { Given, When, Then, And, But, Fusion } = require( 'jest-cucumber-fusion' )
Load any dependency you need to do your test
const { Given, When, Then, And, But, Fusion } = require( 'jest-cucumber-fusion' )
const { Rocket } = require( '../../src/rocket' )
let rocket
Add step definitions to your scenario Jest tests:
const { Given, When, Then, And, But, Fusion } = require( 'jest-cucumber-fusion' )
const { Rocket } = require( '../../src/rocket' )
let rocket
Given( 'I am Elon Musk attempting to launch a rocket into space', () => {
rocket = new Rocket()
} )
When( 'I launch the rocket', () => {
rocket.launch()
} )
Then( 'the rocket should end up in space', () => {
expect(rocket.isInSpace).toBe(true)
} )
And( /^the booster\(s\) should land back on the launch pad$/, () => {
expect(rocket.boostersLanded).toBe(true)
} )
But( 'nobody should doubt me ever again', () => {
expect('people').not.toBe('haters')
} )
Adding the Fusion() call at the end of your Cucumber feature step
You have to match it with your Cucumber Feature definition file:
const { Given, When, Then, And, But, Fusion } = require( 'jest-cucumber-fusion' )
const { Rocket } = require( '../../src/rocket' )
let rocket
Given( 'I am Elon Musk attempting to launch a rocket into space', () => {
rocket = new Rocket()
} )
When( 'I launch the rocket', () => {
rocket.launch()
} )
Then( 'the rocket should end up in space', () => {
expect(rocket.isInSpace).toBe(true)
} )
And( /^the booster\(s\) should land back on the launch pad$/, () => {
expect(rocket.boostersLanded).toBe(true)
} )
But( 'nobody should doubt me ever again', () => {
expect('people').not.toBe('haters')
} )
Fusion( 'rocket-launching.feature' )
Adding coverage
Since we're using jest, it is very easy to generate the code coverage of your Cucumber test:
"jest": {
"testMatch": [
"**/*.steps.js"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/"
],
"coverageDirectory": "./coverage/",
"collectCoverage": true
}
Additional Documentation