What is @cucumber/tag-expressions?
@cucumber/tag-expressions is a package that allows you to evaluate tag expressions, which are commonly used in Cucumber to filter scenarios based on tags. This is useful for running specific subsets of tests based on tags.
What are @cucumber/tag-expressions's main functionalities?
Evaluate Simple Tag Expressions
This feature allows you to evaluate simple tag expressions to determine if a set of tags matches the expression. In this example, the expression '(@smoke or @regression) and not @wip' is parsed and evaluated against the tags ['@smoke'], resulting in true.
const { parse } = require('@cucumber/tag-expressions');
const expression = parse('(@smoke or @regression) and not @wip');
const matches = expression.evaluate(['@smoke']); // true
console.log(matches);
Evaluate Complex Tag Expressions
This feature allows you to evaluate more complex tag expressions. In this example, the expression '(@smoke and @fast) or (@regression and not @slow)' is parsed and evaluated against the tags ['@smoke', '@fast'], resulting in true.
const { parse } = require('@cucumber/tag-expressions');
const expression = parse('(@smoke and @fast) or (@regression and not @slow)');
const matches = expression.evaluate(['@smoke', '@fast']); // true
console.log(matches);
Evaluate Tag Expressions with Multiple Tags
This feature allows you to evaluate tag expressions against a set of multiple tags. In this example, the expression '@smoke and @fast' is parsed and evaluated against the tags ['@smoke', '@fast', '@ui'], resulting in true.
const { parse } = require('@cucumber/tag-expressions');
const expression = parse('@smoke and @fast');
const matches = expression.evaluate(['@smoke', '@fast', '@ui']); // true
console.log(matches);
Other packages similar to @cucumber/tag-expressions
cucumber
The 'cucumber' package is a comprehensive tool for running BDD tests with Cucumber. It includes support for tag expressions, but also provides a full suite of tools for writing and running feature files, step definitions, and more. It is more feature-rich compared to @cucumber/tag-expressions, which focuses solely on tag expression evaluation.
gherkin
The 'gherkin' package is used to parse Gherkin syntax, which is the language used for writing Cucumber feature files. While it does not directly evaluate tag expressions, it is often used in conjunction with tools like @cucumber/tag-expressions to filter and run specific scenarios based on tags.
jest
The 'jest' package is a JavaScript testing framework that includes support for test filtering using tags. While it is not specifically designed for Cucumber-style tag expressions, it provides similar functionality for organizing and running tests based on tags.
Cucumber Tag Expressions for JavaScript
The docs are here.
Example
import {TagExpressionParser} from '@cucumber/tag-expressions'
const parser = new TagExpressionParser()
const expressionNode = parser.parse('@tagA and @tagB')
expressionNode.evaluate(["@tagA", "@tagB"])
expressionNode.evaluate(["@tagA", "@tagC"])