MCPcopy Index your code
hub / github.com/bencompton/jest-cucumber

github.com/bencompton/jest-cucumber @v4.5.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v4.5.0 ↗ · + Follow
281 symbols 591 edges 92 files 0 documented · 0% 1 cross-repo links updated 3mo agov4.5.0 · 2024-07-25★ 67921 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Jest Cucumber

Execute Gherkin scenarios in Jest

Build npm downloads

Cucumber Jest Demo

Overview

jest-cucumber is an alternative to Cucumber.js that runs on top on Jest. 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 then allows you to link these Jest tests to your feature files and ensure that they always stay in sync.

Motivation

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.

Getting Started

Install Jest Cucumber:

npm install jest jest-cucumber --save-dev

Add a Feature file:

Feature: Logging in

Scenario: Entering a correct password
    Given I have previously created a password
    When I enter my password correctly
    Then I should be granted access

Add the following to your Jest configuration:

  "testMatch": [
    "**/*.steps.js"
  ],

Add a step definition file that links to your feature file:

// logging-in.steps.js

import { defineFeature, loadFeature } from 'jest-cucumber';

const feature = loadFeature('features/LoggingIn.feature');

Add a Jest test for each scenario into your step definition file:

// logging-in.steps.js

import { defineFeature, loadFeature } from 'jest-cucumber';

const feature = loadFeature('features/LoggingIn.feature');

defineFeature(feature, test => {
  test('Entering a correct password', ({ given, when, then }) => {

  });
});

Add step definitions to your scenario Jest tests:

// logging-in.steps.js

import { loadFeature, defineFeature } from 'jest-cucumber';
import { PasswordValidator } from 'src/password-validator';

const feature = loadFeature('specs/features/basic-scenarios.feature');

defineFeature(feature, (test) => {
  let passwordValidator = new PasswordValidator();
  let accessGranted = false;

  beforeEach(() => {
    passwordValidator = new PasswordValidator();
  });

  test('Entering a correct password', ({ given, when, then }) => {
    given('I have previously created a password', () => {
      passwordValidator.setPassword('1234');
    });

    when('I enter my password correctly', () => {
      accessGranted = passwordValidator.validatePassword('1234');
    });

    then('I should be granted access', () => {
      expect(accessGranted).toBe(true);
    });
  });
});

Additional Documentation

FAQ

Why doesn't this library work exactly like Cucumber and how do I avoid duplicated step code?

If you prefer an experience more like Cucumber with global step matching and the ability to define steps exactly once that can be matched to multiple steps across multiple feature files, then Jest Cucumber does accommodate this preference with autoBindSteps.

However, the default mode in Jest Cucumber can be thought of as Cucumber reimagined for Jest, and is designed for writing Jest tests that are kept in sync with Gherkin feature files. The goal is that your Jest tests (i.e., step definitions) are perfectly readable by themselves without jumping back and forth between step definitions and feature files. Another goal is to avoid global step matching, which many people find problematic and difficult to maintain as a codebase grows. By default, Jest Cucumber expects that your step definitions and feature files match exactly, and will report errors / generate suggested code when they are out of sync. To avoid duplicated step code, you can use the techniques described here.

Extension points exported contracts — how you extend this code

IJestLike (Interface)
(no doc) [1 implementers]
src/feature-definition-creation.ts
ITodo (Interface)
(no doc)
examples/typescript/src/todo-list.ts
ITodo (Interface)
(no doc)
examples/vitest/src/todo-list.ts
FrameworkTestCall (Interface)
(no doc)
src/feature-definition-creation.ts

Core symbols most depended-on inside this repo

loadFeature
called by 39
src/parsed-feature-loading.ts
whenIRunMyJestCucumberTests
called by 15
specs/step-definitions/validation-and-code-generation.steps.ts
createDefineStepFunction
called by 6
src/feature-definition-creation.ts
generateStepCode
called by 6
src/code-generation/step-generation.ts
whenIExecuteThatScenarioInJestCucumber
called by 6
specs/step-definitions/step-execution.steps.ts
add
called by 5
examples/typescript/src/todo-list.ts
add
called by 5
examples/vitest/src/todo-list.ts
loadFeatures
called by 4
src/parsed-feature-loading.ts

Shape

Function 139
Method 75
Class 62
Interface 4
Enum 1

Languages

TypeScript100%

Modules by API surface

src/parsed-feature-loading.ts23 symbols
src/feature-definition-creation.ts13 symbols
src/code-generation/step-generation.ts11 symbols
specs/step-definitions/validation-and-code-generation.steps.ts11 symbols
specs/utils/mock-test-runner/mock-test-runner.ts9 symbols
specs/test-data/validation-and-code-generation.ts7 symbols
specs/test-data/step-execution.ts6 symbols
examples/vitest/src/calculator.ts6 symbols
examples/typescript/src/calculator.ts6 symbols
examples/ecmascript/src/vending-machine.js6 symbols
examples/ecmascript/src/calculator.js6 symbols
examples/vitest/src/vending-machine.ts5 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add jest-cucumber \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page