Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

gherkin-ast

1.1.0 • Public • Published

gherkin-ast

Build Status dependency Status devDependency Status Coverage Status

JS model for Gherkin feature files

AST

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

'use strict';
const AST = require('gherkin-ast');
console.log(Object.keys(AST));
// Background,...,Feature,GherkinDocument,...,Tag
'use strict';
const {Feature, Scenario} = require('gherkin-ast');
const feature = new Feature('Feature', 'Displaying documents');
feature.elements.push(new Scenario('Scenario', 'Opening a document'));
// ...

GherkinDocument

Model of a complete Gherkin document, i.e. feature file.

Fields

  • {Feature} feature - The feature which this document contains.

Methods

  • new GherkinDocument() : GherkinDocument - Creates a new instance.
  • {GherkinDocument}.toString({FormatConfig} [options]) : string - Converts the document to string, i.e. formats it.
  • {GherkinDocument}.clone() : GherkinDocument - Clones the document.
  • GherkinDocumnet.parse({Object} object) : GherkinDocument - Parses the given GherkinDocument object to a GherkinDocument.

Feature

Model of a Gherkin feature.

Feature: Hello world
  As a smo
  I want to do smth
  So that I am smth

Fields

  • {string} keyword - The keyword of the feature, e.g. "Feature".
  • {string} name - The name of the feature, e.g. "Hello world".
  • {string} description - The description of the feature, e.g. "As a smo\nI want to do smth\nSo that I am smth".
  • {string} language - Tne of the supported Gherkin language, default: "en".
  • {Array<Tag>} tags - Tags of the feature.
  • {Array<Background|Scenario|ScenarioOutline>} elements - The elements of the feature, i.e. scenario, background or scenario outline.

Methods

  • new Feature(keyword, name, description, language) : Feature - Creates a new Feature object, with the given values.
  • {Feature}.toString({FormatConfig}) : string - Converts the feature to string, i.e. formats it.
  • {Feature}.clone() : Feature - Clones the feature.
  • Feature.parse({Object} object) : Feature - Parses the given Feature object to a Feature.

Background

Model of a Gherkin Background scenario.

Background: Some background steps
  Given this is a given step
  And this is a given step too
  When this is a when step
  And this is a when step too
  Then it should be a then step
  And it should be a then step too

Fields

  • {string} keyword - The keyword of the background, e.g. "Background".
  • {string} name - The name of the background, e.g. "Some background steps".
  • {string} description - The description of the background.
  • {Array<Step>} steps - The steps of the background.

Methods

  • new Background(keyword, name, description) : Background - Creates a new Background object, with the given values.
  • {Background}.useNormalStepKeywords() - Sets the keywords of all step to normal keywords, i.e. Given, When, Then.
  • {Background}.useReadableStepKeywords() - Sets the keywords of steps to more readable ones, if applicable, i.e. replaces multiple normal keywords with And keyword.
  • {Background}.toString({FormatConfig}) : string - Converts the background to string, i.e. formats it.
  • {Background}.clone() : Background - Clones the background.
  • Background.parse({Object} object) : Background - Parses the given Background object to a Background.

Scenario

Model of a Gherkin scenario.

@tag2 @tag3
Scenario: Name of scenario
Description of the scenario
 
  Given this is a given step
  And this is a given step too

Fields

  • {string} keyword - The keyword of the scenario, e.g. "Scenario".
  • {string} name - The name of the scenario, e.g. "Name of scenario".
  • {string} description - The description of the scenario, e.g. "Description of the scenario".
  • {Array<Step>} steps - The steps of the scenario.
  • {Array<Tag>} tags - Tags of the scenario.

Methods

  • new Scenario(keyword, name, description) : Scenario - Creates a new Scenario object, with the given values.
  • {Scenario}.useNormalStepKeywords() - Sets the keywords of all step to normal keywords, i.e. Given, When, Then.
  • {Scenario}.useReadableStepKeywords() - Sets the keywords of steps to more readable ones, if applicable, i.e. replaces multiple normal keywords with And keyword.
  • {Scenario}.replace(key, value) - Replaces the given string (key) with the given value in the name, description and steps of the scenario.
  • {Scenario}.toString({FormatConfig}) : string - Converts the scenario to string, i.e. formats it.
  • {Scenario}.clone() : Scenario - Clones the scenario.
  • Scenario.parse({Object} object) : Scenario - Parses the given Scenario object to a Scenario.

ScenarioOutline

Model of a Gherkin Scenario outline.

@tag2 @tag(3)
Scenario Outline: Name of outline <key>
  Given this is a given step
  And this is a given step too
  When this is a when step <key>
  And this is a when step too
  Then it should be a then step
  And it should be a then step too
 
@tagE1
  Examples: First examples
    | key    |
    | value1 |

Fields

  • {string} keyword - The keyword of the scenario outline, e.g. "Scenario Outline".
  • {string} name - The name of the scenario outline, e.g. "Name of outline <key>".
  • {string} description - The description of the scenario outline.
  • {Array<Step>} steps - The steps of the scenario outline.
  • {Array<Tag>} tags - Tags of the scenario outline.
  • {Array<Examples>} examples - Examples of the scenario outline.

Methods

  • new ScenarioOutline(keyword, name, description) : ScenarioOutline - Creates a new ScenarioOutline object, with the given values.
  • {ScenarioOutline}.useNormalStepKeywords() - Sets the keywords of all step to normal keywords, i.e. Given, When, Then.
  • {ScenarioOutline}.useReadableStepKeywords() - Sets the keywords of steps to more readable ones, if applicable, i.e. replaces multiple normal keywords with And keyword.
  • {ScenarioOutline}.toScenario({number} columnToAddAsTag) : Array<Scenario> - Converts the given ScenaioOutline to Scenarios, as it would be expanded in run-time. It also adds a tag to the scenarios, which format is @<nameOfColumn>(<valueOfColumn>), where the columnToAddAsTagth column of each examples used.
  • {ScenarioOutline}.toString({FormatConfig}) : string - Converts the scenario outline to string, i.e. formats it.
  • {ScenarioOutline}.clone() : ScenarioOutline - Clones the scenario outline.
  • ScenarioOutline.parse({Object} object) : ScenarioOutline - Parses the given ScenarioOutline object to a ScenarioOutline.

Examples

Model of a Gherkin Scenario outline Examples table.

@tagE1
  Examples: First examples
    | key    |
    | value1 |

Fields

  • {string} keyword - The keyword of the examples table, e.g. "Examples".
  • {string} name - The name of the examples table, e.g. "First examples".
  • {Array<Tag>} tags - Tags of the examples table.
  • {TableRow} header - The header row of the examples table, with column name(s).
  • {Array<TableRow>} body - The data rows of the examples table.

Methods

  • new Examples(keyword, name, description) : Examples - Creates a new Examples object, with the given values.
  • {Examples}.toString({FormatConfig}) : string - Converts the examples table to string, i.e. formats it.
  • {Examples}.clone() : Examples - Clones the examples table.
  • Examples.parse({Object} object) : Examples - Parses the given Examples object to an Examples.

Step

Model of a Gherkin step.

Given this is a given step
And this is a given step too

Fields

  • {string} keyword - The keyword of the step, e.g. "Given".
  • {string} text - The text of the step, e.g. "this is a given step".
  • {DocString|DataTable} argument - The argument of the step if there is any. It could be a DocString or a DataTable.

Methods

  • new Step(keyword, text) : Step - Creates a new Step object, with the given values.
  • {Step}.replace(key, value) - Replaces the given string (key) with the given value in the text of the step.
  • {Step}.toString({FormatConfig}) : string - Converts the step to string, i.e. formats it.
  • {Step}.clone() : Step - Clones the step.
  • Step.parse({Object} object) : Step - Parses the given Step object to a Step.

Tag

Model of a Gherkin tag (annotation).

@tag(1)

Fields

  • {string} value - The tag itself, e.g. "@tag(1)".

Methods

  • new Tag(value) : Tag - Creates a new Tag object, with the given values. The value will be processed and white spaces will be replaced.
  • {Tag}.toString() : string - Converts the tag to string, i.e. formats it.
  • {Tag}.clone() : Tag - Clones the tag.
  • Tag.parse({Object} object) : Tag - Parses the given Tag object to a Tag.

DocString

Model of a Gherkin DocString step argument.

And this is a when step with doc string
  """
  Hello world
  Hello World
  hello World
  hello world
  """

Fields

  • {string} content - The content of the docString, e.g. "Hello world\nHello World...".

Methods

  • new DocString(value) : DocString - Creates a new DocString object, with the given values.
  • {DocString}.replace(key, value) - Replaces the given string (key) with the given value in the content of the DocString.
  • {DocString}.toString({FormatConfig}) : string - Converts the docString to string, i.e. formats it.
  • {DocString}.clone() : DocString - Clones the docString.
  • DocString.parse({Object} object) : DocString - Parses the given DocString object to a DocString.

DataTable

Model of a Gherkin DataTable step argument.

And this is a when step with data table too
  | col1 | col2 |
  | val1 | val2 |
  | val3 | val4 |

Fields

  • {Array<TableRow>} rows - The rows of the data table.

Methods

  • new DataTable(rows) : DataTable - Creates a new DataTable object, with the given values.
  • {DataTable}.replace(key, value) - Replaces the given string (key) with the given value in the cells of the data table.
  • {DataTable}.toString({FormatConfig}) : string - Converts the data table to string, i.e. formats it.
  • {DataTable}.clone() : DataTable - Clones the data table.
  • DataTable.parse({Object} object) : DataTable - Parses the given DataTable object to a DataTable.

TableRow

Model of a row in Gherkin DataTable or Examples.

And this is a when step with data table too
  | col1 | col2 |
  | val1 | val2 |
  | val3 | val4 |

Fields

  • {Array<TableCell>} cells - The cells of the table row.

Methods

  • new TableRow(cells) : TableRow - Creates a new TableRow object, with the given values.
  • {TableRow}.replace(key, value) - Replaces the given string (key) with the given value in the cells of the table row.
  • {TableRow}.toString({FormatConfig}) : string - Converts the table row to string, i.e. formats it.
  • {TableRow}.clone() : TableRow - Clones the table row.
  • TableRow.parse({Object} object) : TableRow - Parses the given TableRow object to a TableRow.

TableCell

Model of a cell in a Gherkin TableRow.

And this is a when step with data table too
  | col1 | col2 |
  | val1 | val2 |
  | val3 | val4 |

Fields

  • {string} value - The value of the cell, e.g. "col1".

Methods

  • new TableCell(value) : TableCell - Creates a new TableCell object, with the given values.
  • {TableCell}.replace(key, value) - Replaces the given string (key) with the given value in the value of the table cell.
  • {TableCell}.toString() : string - Converts the table cell to string, i.e. formats it.
  • {TableCell}.clone() : TableCell - Clones the table cell.
  • TableCell.parse({Object} object) : TableCell - Parses the given TableCell object to a TableCell.

FormatConfig

By passing an FormatConfig object to format method (or other Ast type methods where it's applicable) it can be set, how feature file text is rendered.

Option Description Default
oneTagPerLine Should tags rendered separately, one by line? false, i.e. all tag of a scenario, feature, etc will be rendered in the same line
separateStepGroups Should step groups (when-then) be separated? false
compact Should empty lines be skipped, removed from the result? false, i.e. there will be empty lines in appropriate places
lineBreak The line break character(s). \n, i.e. it uses Unix line break, to set Windows style, set \r\n
indentation The indentation character(s). ' ', i.e. it uses two space character to add indentation where it's appropriate

install

npm i gherkin-ast

Downloadsweekly downloads

1,738

version

1.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
  • avatar
Report a vulnerability