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

Package detail

gherkin-ast

gherking128.8kMIT3.4.2TypeScript support: included

JS model for Gherkin feature files

Gherkin, Feature, Scenario, Scenario, Outline, Background, Step, Examples

readme

gherkin-ast

Downloads Version@npm Version@git CI Docs

Models for Gherkin feature files

AST (Abstract Syntax Tree)

The API provides types to handle different parts of Gherkin feature files.

In TypeScript:

import {Feature, Scenario /*, Background, ... */} from "gherkin-ast";

OR in JavaScript:

const {Feature, Scenario /*, Background, ... */} = require("gherkin-ast");
const feature = new Feature("Feature", "Displaying documents");
feature.elements.push(new Scenario("Scenario", "Opening a document"));
// ...

Components

The following components are available to work with the Gherkin feature files in code:

Comments

Comments are only permitted at the start of a new line, anywhere in the feature file. They begin with zero or more spaces, followed by a hash sign (#) and some text. (https://cucumber.io/docs/gherkin/reference/)

Although comments can be written in basically any place in Gherkin, AST only supports semantic comments in the feature files because of technical limitations and integration in the whole GherKing flow (parsing, AST, processing, formatting).

The following semantic comments are supported:

Name Description Empty lines supported
Before tags comment For objects with tags, the comment right before the tags. No
Preceding comment For objects with a keyword, the comment right before the line of the keyword until the previous element. Yes
Description comment For objects with a possible description, the comment between the description and the first element (not including before tag and preceding comment of the first element). Yes
Tag comment For tags, the comment right above the tag; note, the first comment cannot have a tag, it is considered a before tags comment. No
Row comment For data table and example rows, the comment between the current and previous row (or element). Yes
DocString comment For docstrings, the comment between the docstring and the step. Yes
Step comment For steps, the comment between the current and the previous step (or its parameters). Yes
Start comment All the comment, at the start of the feature file, before the tag or the tag comments for the feature. Yes
End comment All the comment, at the end of the feature file, after the last step or example row. Yes

Note, that for comments, where empty lines are not supported, one comment is fron the subject object, backwards until the first empty line.

The following example should explain the semantic comments better:

# Start comment(s) of the Document

# Before tags comment of the Feature
@tag1 @tag2
# Tag comment of @tag3 Tag
@tag3
# Preceding comment of the Feature
Feature: Name

  This is a multiline
  Feature description

  # Description comment of the Feature

  # Before tags comment of the Rule
  @tag1 @tag2
  # Preceding comment of the Rule
  Rule: Name

    This is a multiline
    Rule description

    # Description comment of the Rule

    # Preceding comment of the Background
    Background: Name

      This is a multiline
      Background description

      # Description comment of the Background

      # Comment of the given step
      Given step
      # Comment of the when step
      When step
      # Comment of the then step
      Then step

    # Before tags comment of the Scenario
    @tag1 @tag2
    # Preceding comment of the Scenario
    Scenario: Name

      This is a multiline
      Scenario description

      # Description comment of the Scenario

      # Comment of the given step
      Given step
        # Comment of the docstirng
        """
        docstring
        other docstring
        """
      # Comment of the when step
      When step
        # Comment of the data table row
        | data  | table |
        # Comment of the data table row
        | value | value |
        # Comment of the data table row
        | value | value |
      # Comment of the then step
      Then step
        """markdown
        title
        =====
        docstring with content type
        """
      And step
        ```markdown
        docstring with backtick and content type
# Before tags comment of the ScenarioOutline
@tag1 @tag2
# Preceding comment of the ScenarioOutline
Scenario Outline: Name

  This is a multiline
  ScenarioOutline description

  # Description comment of the ScenarioOutline

  Given step <v>
  When step <w>

  # Before tags comment of the Examples
  @tag1 @tag2
  # Preceding comment of the Examples
  Examples: Name
    # Comment of the examples table row
    | v | w |
    # Comment of the examples table row
    | 1 | 2 |
    # Comment of the examples table row
    | 2 | 3 |

End comment(s) of the Document


### Meta information

All components support parsing them from a Gherkin Document. On top of that, certain of the components, also support parsing meta information, e.g. tags in Gherkin are simple strings, but during practical usage tags can be parametrized, thus `Tag` support having both name and value, and it is parsed.

The following components have meta information parsed:

#### Tags

As mentioned in the example, tags in Gherkin are simple strings. However, in practical usage, we often used parametrized tags, e.g., `@suite(sanity)` where the name of the tag is `suite` and the value is `sanity`.

Tag meta information parsing supports the following parametrized tag formats:

1. **Functional**: `@name(value)` (default), the name is `name`, the value is `value`
2. **Assignment**: `@name=value`, the name is `name`, the value is `value`
3. **Underscore**: `@name_value`, the name is `name`, the value is `value`
4. **Parameterless**: this basically means that no value will be parsed, the tag will be handled as a simple string, so in any case the name will be the whole tag, e.g., for `@name(value)` the name of the tag will be `name(value)`, the value is undefined

To set the which format should be used, use the `config(...)` function:

```javascript
const {config, TagFormat, Tag} = require('gherkin-ast');

config({
  tagFormat: TagFormat.ASSIGNMENT,
});
const tag = Tag.parseString("@name=value");
console.log(tag.name); // name
console.log(tag.value); // value
console.log(tag.toString()); // @name=value

Other

This package uses debug for logging, use gherkin-ast :

DEBUG=gherkin-ast* gherking ...

For detailed documentation see the TypeDocs documentation.

changelog

Changelog

3.4.2 - 2024-01-22

Fixed

  • Fixed issue with empty DocString (PR#46)

3.4.1 - 2023-11-24

Fixed

  • Fixed issue with undefined and null values (#87)

3.4.0 - 2023-11-09

Added

  • Added support for configuring the parser (#84):
    • Support for configuring what parametrized tag format should be used during parsing
    • Supporting functional, assignment, underscore, and parameterless tags

3.3.2 - 2023-02-15

Fixed

  • Fixed issue with $$ (#39)

3.3.1 - 2022-10-15

Fixed

  • Fixed comments with missing #

3.3.0 - 2022-03-04

Added

  • Added support for Gherkin Comments
  • Added support for Media Type in DocStrings
  • Added support for filename/location in Gherkin Document (for further use in GherKing)
  • Added debug

3.2.1 - 2021-11-30

Added

  • Published pruneID utility

3.2.0 - 2021-11-13

Added

  • Added unique ID to all AST objects (_id)

3.1.0 - 2021-09-20

Added

  • Added support for tags in Rules (#22)

3.0.3 - 2021-09-17

Fixed

  • Fixed empty cell issue (#29)

3.0.2 - 2021-02-10

Fixed

  • Fixed Scenario parsing issue (#19)

3.0.1 - 2021-01-29

Added

  • Integration with Github Actions

3.0.0 - 2021-01-19

Breaking changed

Added support for normal and parametirized tags:

  • Normal tag, e.g. @tag same as new Tag("tag")
  • Parametirized tag, e.g. @suite(smoke) same as new Tag("suite", "smoke")

All methods of a tag work with both type of tags. Also added Tag.parseString(string) to be able to parse a tag string to any of the above type of tags.

2.1.0 - 2019-09-10

Added

  • Added Rule
  • Added keywords for certain types

2.0.0 - 2019-06-26

BREAKING CHANGES

Refactored API and models:

  • All formatting of the model is removed and moved to gherkin-formatter

  • Most additional logic is removed from models, only replace and clone remain, and ScenarioOutline 's function to expand it to Scenario s

  • All model available through the root of the package, e.g. import {Feature} from "gherkin-ast" or const {Feature} = require("gherkin-ast")

Added

  • Added TS documentation to GitHub Pages.

Changed

  • Using TypeScript, so that .d.ts is available for better IDE and TypeScript support.

1.1.0 - 2018-04-12

Added

  • Added replace method to Step, Scenario, TableCell, TableRow, DocString and DataTable.
  • Added toScenario method to ScenarioOutline.

1.0.1 - 2018-03-23

Fixed

  • Fixed documentation

1.0.0 - 2018-03-23

Added

  • Moved AST from gherkin-assembler to gherkin-ast