Home > Blockchain >  How to implement BeforeAll Cucumber hook in Cypress tests?
How to implement BeforeAll Cucumber hook in Cypress tests?

Time:05-24

We are using:

"cypress": "^9.6.1",
"cypress-cucumber-preprocessor": "^4.3.1",

with TypeScript:

"ts-node": "^10.8.0",
"tsify": "^5.0.4",
"typescript": "^4.6.4"

As I know cypress-cucumber-preprocessor doesn't have any before hook which will go before the whole test suite. Is there any way to implement such hook ?

CodePudding user response:

I worked around this by adding background to each feature file. This will execute before each scenario in your feature file.

Example:

Feature: Doing some this

Background: 
    Given This will run before each scenario
Scenario: 
    Given Will run after the background process
Scenario: 
    This Will also run after the background run

CodePudding user response:

I'm no expert in cypress-cucumber=preprocessor, but looking at cypress-cucumber-preprocessor/features/hooks_ordering.feature there seems to be normal usage of Mocha before() and beforeEach() hooks.

Cucumber Before() is a different hook, because of the capital "B".

    And a file named "cypress/support/step_definitions/steps.js" with:
      """
      const {
        Given,
        Before,
        After
      } = require("@badeball/cypress-cucumber-preprocessor")
      let counter;
      before(function() {
        counter = 0;
      })
      beforeEach(function() {
        expect(counter  , "Expected beforeEach() to be called after before()").to.equal(0)
      })
      Before(function() {
        expect(counter  , "Expected Before() to be called after beforeEach()").to.equal(1)
      })
  • Related