Skip to content

vitest-cucumber plugin fails when parsing multiple feature files due to GherkinParser state pollution #18

@deepracticexs

Description

@deepracticexs

Problem

When the vitest-cucumber plugin is integrated via @deepracticex/configurer and tries to parse multiple .feature files, it fails with:

Error: Failed to transform features/basic.feature: Failed to parse Gherkin: Cannot read properties of undefined (reading 'reset')

Root Cause

The plugin creates a single FeatureTransformer instance that reuses the same GherkinParser instance for all feature files. The Gherkin parser/builder maintains internal state that gets corrupted when parsing multiple files sequentially.

Current Architecture

Plugin (plugin.ts) 
  → Single FeatureTransformer instance (line 11)
    → Single FeatureParser instance
      → Single GherkinParser instance with stateful builder

When Vite calls transform() for each .feature file, it uses the same parser instance, causing state pollution.

Why Tests Didn't Catch This

Our E2E tests use two different approaches:

  1. Old tests (basic.test.ts, advanced.test.ts) - Call generateCucumberTests() API only once
  2. New tests (data-table.test.ts, etc) - Use runCucumberFeature() which runs Cucumber CLI in separate process

Neither approach simulates the real scenario where Vite's transform hook is called multiple times with the same plugin instance.

Solution Implemented

Modified GherkinParser.parse() to create fresh parser/builder instances for each parse:

public parse(content: string): Messages.GherkinDocument {
  // Create fresh instances for each parse to avoid state pollution
  const uuidFn = Messages.IdGenerator.uuid();
  const builder = new Gherkin.AstBuilder(uuidFn);
  const parser = new Gherkin.Parser(builder);
  
  const gherkinDocument = parser.parse(content);
  return gherkinDocument;
}

Additional Issue

The @deepracticex/configurer package bundles @deepracticex/vitest-cucumber into its dist, so updates to vitest-cucumber don't propagate until configurer is rebuilt. Need to add vitest-cucumber to external dependencies in configurer's tsup.config.ts.

Steps to Reproduce

  1. Install @deepracticex/configurer with vitest-cucumber plugin integrated
  2. Create multiple .feature files in a project
  3. Run vitest run
  4. Second and subsequent feature files will fail to parse

Expected Behavior

All feature files should parse successfully regardless of order or quantity.

Environment

  • vitest-cucumber: 0.1.0
  • @deepracticex/configurer: 0.1.0
  • Node: 22.14.0
  • pnpm: 10.15.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions