Execute Gherkin scenarios in Jest
jest-cucumber is an alternative to Cucumber.js that runs on top on Jest. Instead of using
it blocks, you instead write a Jest test for each scenario, and then define
Then step definitions inside of your Jest tests. jest-cucumber then allows you to link these Jest tests to your feature files and ensure that they always stay in sync.
Jest is an excellent test runner with great features like parallel test execution, mocking, snapshots, code coverage, etc. If you're using VS Code, there's also a terrific Jest extension that allows you get realtime feedback as you're writing your tests and easily debug failing tests individually. Cucumber is a popular tool for doing Acceptance Test-Driven Development and creating business-readable executable specifications. This library aims to achieve the best of both worlds, and even run your unit tests and acceptance tests in the same test runner.
Install Jest Cucumber:
npm install jest-cucumber --save-dev
Add a Feature file:
Feature: Rocket LaunchingScenario: Launching a SpaceX rocketGiven I am Elon Musk attempting to launch a rocket into spaceWhen I launch the rocketThen the rocket should end up in spaceAnd the booster(s) should land back on the launch padAnd nobody should doubt me ever again
Add the following to your Jest configuration:
Add a step definition file that links to your feature file:
//rocket-launching.steps.js;const feature = ;
Add a Jest test for each scenario into your step definition file:
//rocket-launching.steps.js;const feature = ;;
Add step definitions to your scenario Jest tests:
//rocket-launching.steps.js;;const feature = ;;