Gherkin Lint

4.2.4 · active · verified Tue Apr 21

Gherkin Lint is a JavaScript-based linter and validator for Gherkin feature files, inspired by ESLint. It ensures consistency and adherence to predefined style and structural rules within Behavior-Driven Development (BDD) scenarios. The current stable version is 4.2.4, with recent releases focusing on dependency updates and minor bug fixes. The package helps teams maintain high-quality, readable, and maintainable Gherkin files by catching common pitfalls such as incorrect indentation, duplicate tags, unnamed features or scenarios, and structural inconsistencies. Its primary differentiator is its comprehensive set of configurable rules and its integration capabilities within CI/CD pipelines and development environments, allowing for early detection of issues in Gherkin syntax and style.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates programmatic use of the `Linter` class to lint a single Gherkin feature file. It shows how to read file content, instantiate the linter, and process the results. It assumes the presence of a `.gherkin-lintrc` configuration file in the working directory.

import { Linter } from 'gherkin-lint';
import * as fs from 'fs';
import * as path from 'path';

async function lintFeatureFile(filePath: string) {
  const linter = new Linter();
  const featureContent = fs.readFileSync(filePath, 'utf8');

  // A .gherkin-lintrc file should be present in the project root
  // or specified via linter options for rules to be applied.
  // Example .gherkin-lintrc (JSON format with comments):
  // {
  //   "no-unnamed-scenarios": "on",
  //   "indentation": ["on", {"Feature": 0, "Background": 2, "Scenario": 2, "Step": 4, "Examples": 2, "example": 4}],
  //   "no-duplicate-tags": "on"
  // }

  const result = await linter.lint({
    featureFile: featureContent,
    fileName: path.basename(filePath)
  });

  if (result.length > 0) {
    console.error(`Linting issues in ${filePath}:`);
    result.forEach(issue => {
      console.error(`  [${issue.rule}] Line ${issue.line}: ${issue.message}`);
    });
    process.exit(1);
  } else {
    console.log(`No linting issues found in ${filePath}.`);
  }
}

// To run this, create a dummy.feature file and a .gherkin-lintrc in the same directory
// Example dummy.feature:
// Feature: My Feature
//   Scenario: My Scenario
//     Given a step
//     When another step
//     Then a final step

const featurePath = path.resolve(__dirname, 'dummy.feature');
if (!fs.existsSync(featurePath)) {
  fs.writeFileSync(featurePath, 'Feature: Example\n  Scenario: Test\n    Given a passing step');
  console.log('Created dummy.feature. Please create a .gherkin-lintrc file in the same directory.');
}

lintFeatureFile(featurePath).catch(console.error);

view raw JSON →