Search code examples
testingcucumbercypress

Unable to use cy.server() with cypress-cucumber-preprocessor


When I try to run cy.server() in order to mock http requests that are executed as part of the tests, I get the following error:

Uncaught CypressError: Cannot call "cy.server()" outside a running test.....

I don't know how to get this to work. This is my code:

import { Given, Then } from 'cypress-cucumber-preprocessor/steps'

beforeEach(() => {
  cy.server()

  cy.route({
    method: 'GET',
    url: '/',
    response: []
  })
})

const url = 'http://localhost:8080'

Given('I click the big button', () => {
  cy.visit(url)
  cy.get('.btn').click()
})

Then('I can get the MOTD', (title) => {
  cy.title().should('include', title)
})

Solution

  • You are missing the 'it()' context for Cypress. Try this:

    import { Given, Then } from 'cypress-cucumber-preprocessor/steps'
    
      beforeEach(() => {
        cy.server()
    
        cy.route({
          method: 'GET',
          url: '/',
          response: []
        })
      })
    
      it('description of the it', function () {
        const url = 'http://localhost:8080'
    
        Given('I click the big button', () => {
          cy.visit(url)
          cy.get('.btn').click()
        })
    
        Then('I can get the MOTD', (title) => {
          cy.title().should('include', title)
        })
      })