无法将 cy.server() 与 cypress-cucumber-preprocessor 一起使用

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

当我尝试 运行 cy.server() 以模拟作为测试的一部分执行的 http 请求时,出现以下错误:

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

我不知道如何让它工作。这是我的代码:

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)
})

您缺少 Cypress 的 'it()' 上下文。试试这个:

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)
    })
  })