Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@cucumber/gherkin-utils

cucumber4.9mMIT9.0.1TypeScript support: included

Various Gherkin utilities

cucumber

readme

Cucumber logo
Gherkin Utils

Utilities for working with Gherkin documents and AST

npm maven-central build backers sponsors

Features

  • ✨ Formatting
  • 👉 Translation of .feature files to .feature.md
  • 🚶‍♂️ Document walker
  • 📁 Document handler

Install

Gherkin Utils is available on npm for JavaScript:

npm install @cucumber/gherkin-utils

Gherkin Utils is available on Maven Central for Java, by adding the dependency to your pom.xml:

<dependencies>
  <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>gherkin-utils</artifactId>
    <version>9.0.0</version>
  </dependency>
</dependencies>

Usage

Command line

To run Gherkin Utils as a formatter, try any of the following:

# Format `file.feature`
> npx @cucumber/gherkin-utils format features/file.feature
# Format `file.feature` and `other.feature`
> npx @cucumber/gherkin-utils format features/file.feature features/other.feature
# Format feature files directly within `features/`
> npx @cucumber/gherkin-utils format features/*.feature
# Format feature files ending with `_test.feature` in `features`
> npx @cucumber/gherkin-utils format features/*_test.feature
# Format feature files within immediate subdirectories of `features/`
> npx @cucumber/gherkin-utils format features/**/*.feature

To convert gherkin feature files to Markdown with Gherkin - or the other way around - while formatting, try the following:

# Format `file.feature` to gherkin markdown `file.feature.md`
npx @cucumber/gherkin-utils format --to-syntax=markdown features/file.feature
# Format `file.feature.md` to gherkin `file.feature`
npx @cucumber/gherkin-utils format --to-syntax=gherkin features/file.feature.md

For more details on usage, see the help menu.

npx @cucumber/gherkin-utils --help

Library

This module can also be used as a library. It provides two main utilities, pretty and gherkinDocumentWalker.

pretty(gherkinDocument: messages.GherkinDocument, syntax: 'gherkin' | 'markdown')

This function takes a GherkinDocument as input and returns a pretty-printed representation in Gherkin or Markdown.

import { AstBuilder, GherkinClassicTokenMatcher, Parser } from '@cucumber/gherkin'
import { pretty } from '@cucumber/gherkin-utils'
import { IdGenerator } from '@cucumber/messages'

const uuidFn = IdGenerator.uuid()

const builder = new AstBuilder(uuidFn)
const matcher = new GherkinClassicTokenMatcher()
const parser = new Parser(builder, matcher)

const feature = `Feature:
Scenario:
Given step text`

const gherkinDocument = parser.parse(feature)

const formattedGherkinFeature = pretty(gherkinDocument)
/*
Feature:

  Scenario:
    Given step text

*/
const formattedGherkinMarkdownFeature = pretty(gherkinDocument, 'markdown')
/*
# Feature:

## Scenario:
* Given step text

*/

GherkinDocumentWalker class

The GherkinDocumentWalker is a class for walking and filtering the AST produced by Gherkin after parsing a feature file. When running walkGherkinDocument on a GherkinDocument, it will produce a deep copy of the object.

It takes two arguments upon creation:

  • filters: set of functions used to know if the walked elements are kept in the result. By default, all elements are kept.
  • handlers: set of function that can be used to alter the produced elements.

Filtering keeps the meaning of the original GherkinDocument, which means:

  • if a Background was present, it will always be in the Feature (or Rule)
  • the kept scenarios will have the same steps and examples than the original

By default, all elements are accepted, which means that if you want to do filtering you should reject all other elements. To ease this, we also provide the rejectAllFilters.

Here's an example:

import { GherkinDocumentWalker, rejectAllFilters } from '@cucumber/gherkin-utils';

// Only keeps scenarios which name include 'magic'
const filter = new GherkinDocumentWalker({
  ...rejectAllFilters,
  ...{ acceptScenario: (scenario) => scenario.name.includes('magic') },
})

// Makes a list with all the scenario names
const allScenarioNames: string[] = []
const scenarioNameFinder = new GherkinDocumentWalker({}, {
  handleScenario: (scenario) => allScenarioNames.push(scenario.name),
})

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

9.0.1 - 2025-01-22

Fixed

  • [JavaScript] Amend parse bug report URL in error message

9.0.0 - 2024-03-26

Changed

  • [JavaScript] Fix background parameter name in GherkinDocumentHandlers (#68)

Fixed

  • [JavaScript] Correct repo URL in package.json

8.0.6 - 2024-02-23

Fixed

  • [JavaScript] Preserve trailing comments at end of file when prettying in JavaScript (#41).
  • [JavaScript] Prevent the introduction of trailing whitespace after headings (#34)
  • [JavaScript] Fix GherkinDocumentWalker filtering of Rules (cucumber/react-components#136)
  • [JavaScript] Fix test execution in Windows - Closes (cucumber/gherkin-utils#2)
  • [Java] Fix docstring pretty formatting (#58)
  • Table formatting for full width characters (#53)
  • [Java] Fix urls in project metadata

8.0.5 - 2023-06-02

Changed

  • Upgrade to @cucumber/messages 22.0.0

Fixed

  • Trim trailing spaces

8.0.4 - 2023-05-25

Changed

  • [Java] Improve prettyPrint of comments, courtesy of @blacelle

8.0.3 - 2023-05-11

Fixed

  • Walk comments in walkGherkinDocument
  • Pretty print comments in pretty

8.0.2 - 2022-11-21

Added

Changed

  • [Java] Enabled reproducible builds
  • Use GitHub Actions for releases

Deprecated

Fixed

Removed

8.0.1 - 2022-11-01

Fixed

  • Declared required dependencies correctly (#2072)
  • Update @cucumber/gherkin-utils package.json to use correct bin option.

8.0.0 - 2022-05-31

Changed

  • Upgrade to @cucumber/gherkin 24.0.0

7.0.0 - 2021-09-01

Added

  • Added new command-line tool for formatting Gherkin documents and converting to/from Gherkin/Markdown. Run npx @cucumber/gherkin-utils --help for details about usage.

Changed

  • Upgrade to @cucumber/messages 17.1.0
  • Upgrade to @cucumber/gherkin 21.0.0

Deprecated

6.0.0 - 2021-07-08

Changed

  • Bump @cucumber/messages version to 17.0.0

5.1.0 - 2021-05-26

Added

  • Added getSource, getFeature, getBackground, getRule, getScenario and getExamples methods

5.0.2 - 2021-05-17

Changed

  • Upgrade to @cucumber/message-streams 2.0.0

5.0.1 - 2021-05-17

Fixed

  • Use ^x.y.z version for @cucumber/* dependencies, allowing minor and patch releases to be picked up.

5.0.0 - 2021-05-15

Added

  • Added Query#getSources(): readonly messages.ISource[]

Changed

  • Upgrade to messages 16.0.0

4.0.0 - 2021-03-29

Changed

  • Upgrade to messages 15.0.0

3.0.0 - 2021-02-07

Changed

  • Upgrade to messages 14.0.0

2.1.1 - 2020-12-13

Fixed

  • [JavaScript] Remove unneded dependency on @cucumber/gherkin

2.1.0 - 2020-11-03

Added

  • [JavaScript] The Query class has been added to this library, moved from @cucumber/gherkin

2.0.1 - 2020-09-02

Fixed

2.0.0 - 2020-08-07

Changed

  • Update messages to 13.0.1

1.0.1 - 2020-06-29

Fixed

  • [JavaScript] Upgrade Gherkin

1.0.0 - 2020-06-25

Added

  • First release