Search code examples
javascriptcucumberbddcucumberjs

cucumber-js cannot find the step definition when a variable is needed


I have a feature

// test.feature
    Feature: Test1

      Scenario: To test variables
         Given When no variable succeed
         When When value blah and value blah3


    // test_steps.js
    const { Before, Given, When, Then } = require('cucumber');
const assert = require('assert');


Given(/^When no variable succeed$/, function () {
    assert.equal(1,1)
  });

When(/^When value {string} and value {string}$/, function (val1, val2) {
    return 'pending';
});

what the results are

> Scenario: To test variables #
> tests/bdd/features_cucumber/test.feature:3    ✔ Given When no variable
> succeed # tests/bdd/features_cucumber/step_definitions/test_steps.js:5
> ? When When value blah and value blah3
>        Undefined. Implement with the following snippet:
> 
>          When('When value blah and value blah3', function () {
>            // Write code here that turns the phrase above into concrete actions
>            return 'pending';
>          });

Solution

  • You have to add quotes around the variables in the feature file When When value "blah" and value "blah3"

    And you don't need the regular expressing in the step definition. When('When value {string} and value {string}', function (val1, val2) { should do

    If you use regular expression in your step definition, then you have to use capturing groups for your values. e.g: When(/^When value "([^"]*)" and value "([^"]*)"$/, function (val1, val2) {