paul
paul

Reputation: 4487

cypress can't find step definitions

I am trying to set up the Cypress project from scratch and I couldn't find out why feature files are not able to identify step definitions.

What am I doing wrong?

I am sharing what I have tried:

This is package.json

{
  "name": "cypressdemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@badeball/cypress-cucumber-preprocessor": "^15.1.4",
    "cucumber": "^6.0.7",
    "cypress": "^12.7.0"
  },
  "@badeball/cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": false,
    "stepDefinitions": "cypress/integration/features/stepDefs"
  }
}

This is index.js

const cucumber = require('@badeball/cypress-cucumber-preprocessor').default

module.exports = (on, config) => {
  on('file:preprocessor', cucumber())
}

This is cypress.config.js

const {defineConfig} = require("cypress");

module.exports = defineConfig({
    e2e: {
        setupNodeEvents(on, config) {
            // implement node event listeners here
            // }, specPattern: 'cypress/integration/stepDefs/*.js',
        }, specPattern: 'cypress/integration/features/*.feature',

    }

});

Upvotes: 0

Views: 1870

Answers (2)

paul
paul

Reputation: 4487

The step definitions path should be in package.json.

If you have folders inside stepdefinitions folder then use * like this

"stepDefinitions": "cypress/integration/stepDefinitions/**/",

If you are using "cypress-cucumber-preprocessor": { then it would look like this

  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": false,
    "stepDefinitions": "cypress/integration/stepDefinitions/**/",
  }

The name of the Step definitions file need not be the same as your feature file, but it is recommended.

Upvotes: 0

Fody
Fody

Reputation: 32108

You probably just need to specify an extension for step definitions,
see Step definitions

{
 "stepDefinitions": [
   "cypress/integration/[filepath]/**/*.{js,ts}",
   "cypress/integration/[filepath].{js,ts}",
   "cypress/support/step_definitions/**/*.{js,ts}",
 ]
}

so package.json

"@badeball/cypress-cucumber-preprocessor": {
  "nonGlobalStepDefinitions": false,
  "stepDefinitions": "cypress/integration/features/stepDefs/*.{js,ts}"
}

Upvotes: 4

Related Questions