Cypress不会拦截GraphQL API调用

问题描述 投票:0回答:1

我正在尝试拦截graphql突变以获取其响应。赛普拉斯没有注意到该请求,并且cy.wait()超时。

test

describe('/profile', () => {
  beforeEach(() => {
    cy.server()
    cy.route({
      method: 'POST',
      url: Cypress.config().baseUrl + '/graphql',
    }).as('graphql')
    cy.fastLogin()
    cy.visit('/profile')
  })

  it('is possible to change password', () => {
    cy.get('[data-cy=change-password-btn]').click()

    cy.get('[name=oldPassword]').type(Cypress.config().password)
    cy.get('[name=newPassword]').type('password1')
    cy.get('[name=confirmNewPassword]').type('password1')
    cy.get('[data-cy=change-pw-form-btn]').click()

    cy.wait('@graphql').then(xhr => {
      cy.log(JSON.stringify(xhr.response.body))
    })

    cy.get('[data-cy=notification-container]').should(
      'contain',
      'Your password has been changed'
    )
  })

我发现许多人遇到的一个普遍问题是,他们在cy.server()之后叫cy.route()cy.visit()。这不是我的情况,但是cy.wait()仍然失败。

如果有任何区别,我有一个NodeJS后端和ReactJS前端。

有什么想法吗?

graphql cypress
1个回答
0
投票

cypress没看到我的graphql查询,因为所有查询都是通过fetch进行的。我用这个[要点] [1]将fetch转换为xhr。现在,我可以在graphql UI中看到POST cypress调用,但是cy.wait()仍然超时

[1]: https://gist.github.com/yagudaev/2ad1ef4a21a2d1cfe0e7d96afc7170bc
© www.soinside.com 2019 - 2024. All rights reserved.