What is gherkin?
The gherkin npm package is a parser for the Gherkin language, which is used to write structured tests in a business-readable, domain-specific language. It is commonly used in Behavior-Driven Development (BDD) frameworks like Cucumber.
What are gherkin's main functionalities?
Parsing Gherkin Syntax
This feature allows you to parse Gherkin syntax into a structured format. The code sample demonstrates how to parse a simple Gherkin feature file and output the resulting JSON structure.
const { Parser } = require('gherkin');
const parser = new Parser();
const gherkinDocument = parser.parse(`
Feature: Example feature
Scenario: Example scenario
Given a precondition
When an action is performed
Then an outcome is expected
`);
console.log(JSON.stringify(gherkinDocument, null, 2));
Tokenizing Gherkin Syntax
This feature allows you to tokenize Gherkin syntax, breaking it down into individual tokens. The code sample demonstrates how to read and print tokens from a Gherkin feature file.
const { TokenScanner, TokenMatcher, GherkinLine, Token } = require('gherkin');
const scanner = new TokenScanner(`
Feature: Example feature
Scenario: Example scenario
Given a precondition
When an action is performed
Then an outcome is expected
`);
const matcher = new TokenMatcher();
let token;
while ((token = scanner.read()) && token.isEOF === false) {
console.log(token);
}
Generating Pickles
This feature allows you to generate 'pickles' from Gherkin documents. Pickles are a simplified, executable representation of Gherkin scenarios. The code sample demonstrates how to parse a Gherkin document and compile it into pickles.
const { Parser, Compiler } = require('gherkin');
const parser = new Parser();
const compiler = new Compiler();
const gherkinDocument = parser.parse(`
Feature: Example feature
Scenario: Example scenario
Given a precondition
When an action is performed
Then an outcome is expected
`);
const pickles = compiler.compile(gherkinDocument);
console.log(JSON.stringify(pickles, null, 2));
Other packages similar to gherkin
cucumber
Cucumber is a tool for running automated tests written in plain language. It supports the Gherkin syntax and integrates with various programming languages. Compared to gherkin, Cucumber provides a full-fledged testing framework, including test execution and reporting.