cucumber-js doesn't find step definitions

Created on 11 Jan 2017  Â·  48Comments  Â·  Source: cucumber/cucumber-js

Hi, I'm a newbie who's been trying for hours to get cucumber-js to find my step definitions. I've come up with this simple example.

In features/foo.feature:

Feature: Foo
  Scenario: Bar
    Given FooBar

In features/step_definitions/step_defs.js:

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({Given}) {
    Given('FooBar', function () {
      return 'pending';
    });    
});

I get this result:

Feature: Foo

  Scenario: Bar
  ? Given FooBar

Warnings:

1) Scenario: Bar - features\foo.feature:2
   Step: Given FooBar - features\foo.feature:3
   Message:
     Undefined. Implement with the following snippet:

       Given('FooBar', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

1 scenario (1 undefined)
1 step (1 undefined)
0m00.000s
help wanted

Most helpful comment

Alright. I believe I figured this out.

When using a global install of cucumber to run the feature tests, you are still defining support code by requiring the local install of cucumber. The fact that these are two different instances results in no step definitions being loaded.

Is there any reason you can't use the local install for running the command? I do this on mac by adding "./node_modules/.bin" to my path.

I don't like the idea of trying to making this work by having the global install try to require the local install in order to get the step definitions. I also dislike the global install since it makes you have to specify things like compile modules by their absolute path.

If this is possible on windows, I'd prefer to just update the documentation to mention that this cannot be used as a global install.

All 48 comments

I know that cucumber-js is finding the step definitions file, because when I inject a syntax error into it, cucumber-js reports the error correctly.

What version of cucumber-js are you using?

It says 2.0.0-rc.6

Also a newbie and have the same issue running the same version of cucumber.js
I was following another tutorial but got stuck at this point.

/features/add-item.feature

Feature: Shopper can add an item to their Grocery List
  As a grocery shopper
  I want to add an item to my grocery list
  So that I can remember to buy that item at the grocery store

  Scenario: Item added to grocery list
    Given I have an empty grocery list
    When I add an item to the list
    Then The grocery list contains a single item

  Scenario: Item accessible from grocery list
    Given I have an empty grocery list
    When I add an item to the list
    Then I can access that item from the grocery list`

/features/step_definitions/add-item.steps.js
'use strict';

module.exports = function() {

    Given('I have an empty grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });


    When('I add an item to the list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

    Then('The grocery list contains a single item', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

    Then('I can access that item from the grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

}

$ node_modules/.bin/cucumber-js returns

Feature: Shopper can add an item to their Grocery List

    As a grocery shopper
    I want to add an item to my grocery list
    So that I can remember to buy that item at the grocery store

  Scenario: Item added to grocery list
  ? Given I have an empty grocery list
  ? When I add an item to the list
  ? Then The grocery list contains a single item

  Scenario: Item accessible from grocery list
  ? Given I have an empty grocery list
  ? When I add an item to the list
  ? Then I can access that item from the grocery list

Warnings:

1) Scenario: Item added to grocery list - features/add-item.feature:6
   Step: Given I have an empty grocery list - features/add-item.feature:7
   Message:
     Undefined. Implement with the following snippet:

       Given('I have an empty grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

2) Scenario: Item added to grocery list - features/add-item.feature:6
   Step: When I add an item to the list - features/add-item.feature:8
   Message:
     Undefined. Implement with the following snippet:

       When('I add an item to the list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

3) Scenario: Item added to grocery list - features/add-item.feature:6
   Step: Then The grocery list contains a single item - features/add-item.feature:9
   Message:
     Undefined. Implement with the following snippet:

       Then('The grocery list contains a single item', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

4) Scenario: Item accessible from grocery list - features/add-item.feature:11
   Step: Given I have an empty grocery list - features/add-item.feature:12
   Message:
     Undefined. Implement with the following snippet:

       Given('I have an empty grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

5) Scenario: Item accessible from grocery list - features/add-item.feature:11
   Step: When I add an item to the list - features/add-item.feature:13
   Message:
     Undefined. Implement with the following snippet:

       When('I add an item to the list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

6) Scenario: Item accessible from grocery list - features/add-item.feature:11
   Step: Then I can access that item from the grocery list - features/add-item.feature:14
   Message:
     Undefined. Implement with the following snippet:

       Then('I can access that item from the grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

2 scenarios (2 undefined)
6 steps (6 undefined)
0m00.000s

A world.js file has not be included at this point.

@dunlop-ben please see the changelog about the difference between 1.0 and the 2.0 release candidates. The way of defining steps changed, and your example appears to be using the 1.0 syntax.

@jeffmath nothing looks wrong to me. We have feature tests that verify something like your simple example works. Reproducing on a mac, it works as expected for me. What version of node are you running? And from the paths it appears you are on windows machine, is that correct?

@charlierudolph - thanks. I have looked into this and tried a few different examples with a similar result. I have also tried using feature and step definition here: http://cucumber.github.io/cucumber-js/

I feel as though i must be missing a step and/or files. If there is anywhere you can direct me to that would be great. I haven't had much luck reading through the documentation on Github.

@charlierudolph Node 6.4.0 on Windows 7

@charlierudolph I have similar problem. I exactly follow https://github.com/cucumber/cucumber-js/blob/master/docs/nodejs_example.md and issue still occur.

My setup is

  • NODE 7.4.0
  • OS: Windows 10
  • Cucumber 2.0.0 -rc.6

@jeffmath @TheAdamW sorry but I don't have a windows machine and thus can't really track this down. We are using appveyor to run our tests on windows machine and I don't know what the difference would be. Could one of you please try to dig into this? I'm happy to provide any assistance I can

For me it works if I'm writting ./node_modules/.bin/cucumberjs but not if I write just the globally installed cucumberjs (same version 2.0.0-rc.6).

Same error here: there is no way for cucumber to find the step definitions. Having the same folder structure works in linux.

  • NODE. 4.4.0 & 4.6
  • OS: Windows 10
  • Cucumber 2.0.0 -rc.6

Is there any way to force the step definitions path / folder / pattern?
BTW @MartinDelille ´s trick didn't work for me.

I am facing the same issue all of a sudden, the cucumber was fine executing the scripts until today morning. I have no idea what is wrong with it.

I run the feature by rightclick on ruby mine and from command line using the command cucumber -r features/chatstep.feature I get the same result.

Can anyone please help

Given(/^I launch "([^"]*)"$/) do |arg1|
pending # Write code here that turns the phrase above into concrete actions
end

I had to downgrade to 1.3.1 to make it work.
It does not even work in centos.

On 25 Jan 2017 21:32, "Mrityunjeyan S" notifications@github.com wrote:

I am facing the same issue all of a sudden, the cucumber was fine
executing the scripts until today morning. I have no idea what is wrong
with it.

I run the feature by rightclick on ruby mine and from command line using
the command cucumber -r features/chatstep.feature I get the same result.

Can anyone please help

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/cucumber/cucumber-js/issues/718#issuecomment-275224435,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALjDKMNdggk1Gh-x_q3KRuvlpN5f_0cOks5rV7E9gaJpZM4LhJZO
.

@jbgraug: what did u downgrade? What was Ur previous version before downgrade

I tried with RC 2.0.6 & RC 2.0.0. I could not make it work in linux or
windows as cucumber was unable to find the step definitions, not even with
the -r option.

On Thu, Jan 26, 2017 at 6:18 AM, Mrityunjeyan S notifications@github.com
wrote:

@jbgraug https://github.com/jbgraug: what did u downgrade? What was Ur
previous version before downgrade

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/cucumber/cucumber-js/issues/718#issuecomment-275310070,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALjDKGbyYXvsjlLv-dlz6bnHMfPK7t05ks5rWCypgaJpZM4LhJZO
.

@jbgraug I guess the cucumber has deprecated the gherkin bundled with it from version 2.0? I am currently installing 1.3.14, and it says fetching gherkin which i never noticed while installing versions above 2.0

@reach2jeyan based on your command line and test snippets I believe you are talking about cucumber-ruby. While these two projects are related in trying to implement the cucumber framework for their respective languages, they have a number of differences.

@charlierudolph Thanks for stepping in. My bad, I had actually not followed the step definition procedures earlier, plus, the cucumber-ruby versions were all messed up. I had to then reset everything and start all over again.

It works now by the way!

@charlierudolph exactly what @MartinDelille said, works fine via npm -S with ./node_modules/.bin/cucumber.js, but not via npm -g with /usr/local/bin/cucumber.js, it finds and executes the steps, but somehow forgets them afterwards.

macOS + Node.js 7.5.0 + Cucumber.js 2.0.0-rc.7
windows specific label is misleading

@kozhevnikov thanks for the update. All the reports thus far had been on a windows (and I had unable to recreate on my local mac with the local install) Hard to imagine the global install is somehow different but at least now I can investigate.

i get the same issues on windows using command prompt through cygwin maybe thats the issue? i even copied the code directly from the example.

Feature: Simple maths
In order to do maths
As a developer
I want to increment variables

Scenario: easy maths
Given a variable set to 1
When I increment the variable by 1
Then the variable should contain 2

Scenario Outline: much more complex stuff
Given a variable set to
When I increment the variable by
Then the variable should contain

Examples:
  | var | increment | result |
  | 100 |         5 |    105 |
  |  99 |      1234 |   1333 |
  |  12 |         5 |     18 |

Cucumber.defineSupportCode(function(context) {
var setWorldConstructor = context.setWorldConstructor;
var Given = context.Given
var When = context.When
var Then = context.Then

///// Your World /////
//
// Call 'setWorldConstructor' with to your custom world (optional)
//

var CustomWorld = function() {};

CustomWorld.prototype.variable = 0;

CustomWorld.prototype.setTo = function(number) {
this.variable = parseInt(number);
};

CustomWorld.prototype.incrementBy = function(number) {
this.variable += parseInt(number);
};

setWorldConstructor(CustomWorld);

///// Your step definitions /////
//
// use 'Given', 'When' and 'Then' to declare step definitions
//

Given(/^a variable set to (\d+)$/, function(number) {
this.setTo(number);
});

When(/^I increment the variable by (\d+)$/, function(number) {
this.incrementBy(number);
});

Then(/^the variable should contain (\d+)$/, function(number) {
if (this.variable != parseInt(number))
throw new Error('Variable should contain ' + number +
' but it contains ' + this.variable + '.');
});
})

i get this error "ReferenceError: Given is not defined"
i run cucumber.js and ~/node_modules/.bin/cucumber.js with the specific file name and it always throws an error. I can run cucumber with ruby no problems but i can't get cucumberjs to work

Alright. I believe I figured this out.

When using a global install of cucumber to run the feature tests, you are still defining support code by requiring the local install of cucumber. The fact that these are two different instances results in no step definitions being loaded.

Is there any reason you can't use the local install for running the command? I do this on mac by adding "./node_modules/.bin" to my path.

I don't like the idea of trying to making this work by having the global install try to require the local install in order to get the step definitions. I also dislike the global install since it makes you have to specify things like compile modules by their absolute path.

If this is possible on windows, I'd prefer to just update the documentation to mention that this cannot be used as a global install.

Two minor points about removing global install:

  1. All docs would need updating, and not just the official ones but a lot of other popular frameworks built on top. I was following along when I ran into this and it was frustrating to debug for someone who only just started with JavaScript/Node.js/Cucumber.js

  2. A lot of IDEs and plugins are hardcoded to point to global. Also adding an environment variable outside of bash profile (i.e. launching WebStorm through Spotlight) on macOS is convoluted and changes from version to version.

Hmm. We can add a CLI option which allows the user specify where the local cucumber instance is? We can have the default be process.cwd() + /node_modules/cucumber which should cover most cases.

@charlierudolph After originally reporting the error, I realized I needed to be using Cucumber-JVM, anyway, since the code I was needing to test was written in Scala.js. But when I next need to test Javascript, your workaround sounds easy to implement. Thank you for figuring out the issue.

It'll be tricky, from what I understand Node has lots of edge cases, so at a minimum it would have to traverse parents till it finds the local package or hits drive root, and I don't know how popular using global folders is in the Node community.

@jeffmath @kozhevnikov here are my updated thoughts on this

  1. I'm fine with cli option that allows the user to specify the path to the local cucumber. If not provided it uses the executed instance.
  2. Use of this option would do version validation to ensure the local / global install are the exact same version as otherwise things may not work as expected. If not, it will throw an error telling the user to please align the versions.
  3. This option would be immediately deprecated and come with a warning to stop using the global install and instead use solely the local install.
  4. This option will be removed in the next major version.

I have found a similar problem while trying to build a library of common tests. My idea is to reuse tests across different projects.

I have built a common tests package called _roi-functional-tests_ that I would like to use in other projects. So far, I have linked the package with _npm link_ and then try to point cucumber to the features in _roi-functional-tests_, something like this:

./node_modules/.bin/cucumberjs ./node_modules/roi-functional-tests/features

But I get this:

Warnings:

1) Scenario: Curl request - node_modules/roi-functional-tests/features/home.feature:3
   Step: Given I make curl request to "/" - node_modules/roi-functional-tests/features/home.feature:4
   Message:
     Undefined. Implement with the following snippet:

       Given('I make curl request to {arg1:stringInDoubleQuotes}', function (arg1, callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

Can cucumber import definitions from outside of the working directory?

@jramcast yes. But you would need to use the --require cli flag.

Already tried using the --require flag:

./node_modules/.bin/cucumberjs --require ./node_modules/roi-functional-tests/features ./node_modules/roi-functional-tests/features

But I get the same result.

It is possible that npm linkmight be causing cucumber to not find the definitions?

So I did the same test without npm link. I published and installed the package and everything is working fine by just calling:

./node_modules/.bin/cucumberjs ./node_modules/roi-functional-tests/features

My guess is that, when linking the package, require loads the cucumber installed inside the linked package directory (node_modules/roi-functional-tests/node_modules/cucumber). However, if _roi-functional-tests_ is installed, npm flattens the dependency tree and uses the cucumber instance installed right under the project's node_modules (node_modules/cucumber)

Have the same problem.

Cucumber version: "cucumber": "^2.0.0-rc.9"
Windows version: 10
Node version: 6.10.3 LTS & 7.10

Does not work in IDE or global instance of cucumber, never finds the steps even though they are implemented correctly.

If I call the cucumber.js from within the project's node_modules dir then it works.

Anyone have any suggestions on how to fix this issue?

This is also happening on MacOS Sierra 10.12.4
Node version 7.10

Went back to Cucumber version 1.3.0, works fine now calling it via cmd using global instance AND works in IDE.

Hi @charlierudolph ,
I am in the next step. (Moved forward from issue 60 to here)
I run with local instance only but my step definitions are not recognised while running the feature file
image
The feature file and Step Definition files are in the right place (Screen-shot attached)
Why are my step def not identified?

I had Cucumber reference to 1.X versions in package-lock.json
"wdio-cucumber-framework":

    "cucumber": {
      "version": "1.3.3",
      "resolved": "https://registry.npmjs.org/cucumber/-/cucumber-1.3.3.tgz",
      "integrity": "sha1-Za+2Xy+T9y2teN8qterPFGCf7C8=",
      "dev": true
    },

"webdriverio-cucumber-js":  

   "cucumber": {
      "version": "1.2.2",
      "resolved": "https://registry.npmjs.org/cucumber/-/cucumber-1.2.2.tgz",
      "integrity": "sha1-KEZJEf8arfwOZjTXY2q69mHGaJI="
    },

I changed those two versions to 2,3,1
My node version is 8.0.0
But still step definition is not recognised
Any idea?

@vvedachalam

If you're using webdriverIO with its test runner as opposed to in standalone mode, you will NEED to use cucumber-js version 1.x, preferably 1.3.3 as opposed to 2.x as the test runner does NOT support the new version and new way the step definitions are implemented.

I have gone back to the old [email protected] as I am using the test runner which is required for page object model and ensuring all commands are run synchronously.

@GeeChao
Thanks for the reply. I have reverted and ran the test still Step definition is not recognised

When I tried the command with -r , I get syntex error in the feature file...
C:/webdriverio-test/node_modules/.bin/cucumber-js -r features/customer-validation.feature

C:\webdriverio-testfeatures\customer-validation.feature:2
As an owner
^^

SyntaxError: Unexpected identifier
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at C:\webdriverio-test\node_modules\cucumberlib\cliindex.js:135:16
at Array.forEach (native)
at Cli.getSupportCodeLibrary (C:\webdriverio-test\node_modules\cucumberlib\cliindex.js:134:24)
at Cli. (C:\webdriverio-test\node_modules\cucumberlib\cliindex.js:144:39)
at Generator.next ()
at Generator.tryCatcher (C:\webdriverio-test\node_modules\bluebird\js\release\util.js:16:23)
at PromiseSpawn._promiseFulfilled (C:\webdriverio-test\node_modules\bluebird\js\release\generators.js:97:49)

@vvedachalam Could you please create and push this to your repo so I can replicate it?

@GeeChao
Please have a look
https://github.com/vvedachalam/Ruffer-Test

@vvedachalam

Move your step_definitions folder within your features folder and it will start working and recognize the step definitions. Only issue that's thrown after is your txtdetails.txt file location is incorrect.

@GeeChao
ahh,, sry I didnt push that change.
If you see my above screenshot, the step definition is inside features folder only.
Hope content will be:
Content = fs.readFileSync('../../txndetails.txt', 'utf8');

Then why I am unable to glue the step definition to feature file in my local ?
Is my set-up not correct?

@vvedachalam

It works for me.

The only thing I did was move step_definitions to under the features directory

also i did a npm install [email protected] --save-dev

then ran node_modules/cucumber/bin/cucumber.js features/

@GeeChao All worked fine. I made a For loop inside the Then statement which created the issue. Is it possible to use For loop inside step definition?
I dont want to use Example as I have to run the Then step based on a Text file data (data driven test)
I know this Question is not related to this topic but just thought whether I can get some help.

@GeeChao Ok, again its my silly mistake. Looping variable was not set properly (forgot to compare the length). Thanks again for your support

For me, as a newbie, the error was caused by me passing a string rather than a regex into the steps;

Then('I log in as (.*)', ...)

rather than

Then(/I log in as (.*)', ...)

Simple mistake, but hopefully useful to someone else just starting out.

@vvedachalam Did u sort it out? Could you please help

@sancy2017 I moved to TestCafe tool from WDIO. So I may not be able to tell you the solution immediately. Let me know what exactly the problem that you are facing

The simple solution for this error is npm install [email protected] --save-dev
then run your test ..npm test

I ran into same problem "Cucumber Step definitions were not recognised". Changing the directory name from "steps" to "step_definitions" worked for me.

I am seeing mostly people have step_definitions under features directory, i believe a better practice is to have the steps implementation in a separate directory i.e. src/step_definitions instead of keeping the steps under features directory.

Was this page helpful?
0 / 5 - 0 ratings