What is @cucumber/cucumber-expressions?
@cucumber/cucumber-expressions is a package that allows you to define and match text patterns in a way that is useful for writing and running Cucumber tests. It provides a way to define patterns that can be used to match steps in Gherkin scenarios, making it easier to write readable and maintainable test cases.
What are @cucumber/cucumber-expressions's main functionalities?
Defining Cucumber Expressions
This feature allows you to define Cucumber expressions that can be used to match text patterns in Gherkin steps. In this example, the expression 'I have {int} cukes' is defined and matched against the text 'I have 42 cukes', extracting the integer value 42.
const { CucumberExpression, ParameterTypeRegistry } = require('@cucumber/cucumber-expressions');
const parameterTypeRegistry = new ParameterTypeRegistry();
const expression = new CucumberExpression('I have {int} cukes', parameterTypeRegistry);
const value = expression.match('I have 42 cukes')[0].getValue();
console.log(value); // 42
Custom Parameter Types
This feature allows you to define custom parameter types that can be used in Cucumber expressions. In this example, a custom parameter type 'word' is defined to match any word, and it is used in the expression 'I have a {word}', extracting the word 'cucumber'.
const { ParameterType, ParameterTypeRegistry, CucumberExpression } = require('@cucumber/cucumber-expressions');
const parameterTypeRegistry = new ParameterTypeRegistry();
parameterTypeRegistry.defineParameterType(new ParameterType('word', /\w+/, String, s => s));
const expression = new CucumberExpression('I have a {word}', parameterTypeRegistry);
const value = expression.match('I have a cucumber')[0].getValue();
console.log(value); // 'cucumber'
Regular Expressions
This feature allows you to use regular expressions to match text patterns in Gherkin steps. In this example, the regular expression /I have (\d+) cukes/ is used to match the text 'I have 42 cukes', extracting the integer value 42.
const { RegularExpression, ParameterTypeRegistry } = require('@cucumber/cucumber-expressions');
const parameterTypeRegistry = new ParameterTypeRegistry();
const expression = new RegularExpression(/I have (\d+) cukes/, parameterTypeRegistry);
const value = expression.match('I have 42 cukes')[0].getValue();
console.log(value); // 42
Other packages similar to @cucumber/cucumber-expressions
cucumber
The 'cucumber' package is the main package for running Cucumber tests in JavaScript. It includes support for defining and running Gherkin scenarios, but it does not provide the same level of flexibility for defining custom expressions and parameter types as @cucumber/cucumber-expressions.
gherkin
The 'gherkin' package is responsible for parsing Gherkin syntax, which is used to define Cucumber scenarios. While it focuses on parsing the Gherkin language, it does not provide the same capabilities for defining and matching custom expressions as @cucumber/cucumber-expressions.
jest-cucumber
The 'jest-cucumber' package allows you to write Cucumber-style tests using the Jest testing framework. It provides a way to define Gherkin scenarios and match them to step definitions, but it does not offer the same level of customization for expressions and parameter types as @cucumber/cucumber-expressions.