柏树与阿波罗之间的相容性 [英] Compatibility between cypress and react-apollo

查看:127
本文介绍了柏树与阿波罗之间的相容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

但是赛普拉斯似乎无法捕获任何东西,除了XHR请求.

那我该怎么做才能解决这个问题?赛普拉斯有没有办法捕获获取"请求?有没有办法让react-apollo使用"xhr"而不是"fetch"?

解决方案

一个简单的解决方法是使用 whatwg-fetch ,您可以通过npm将其添加为依赖项,然后...

cypress/support/fetch_to_xhr.js

  function fetchToXhr(){让polyfill之前(()=> {cy.readFile('node_modules/whatwg-fetch/dist/fetch.umd.js').then((contents)=> polyfill = contents)Cypress.on('window:before:load',(win)=> {删除win.fetchwin.eval(polyfill)})})}fetchToXhr() 

cypress/support/index.js

  import"./fetch_to_xhr"; 

那次赛普拉斯将捕获graphql请求

I am using react-apollo to make GraphQL queries, and I am using Cypress for testing.

The problem is that these 2 dont seem to play well along. Apollo seems to be making all its requests through the Fetch API.

But Cypress seems like it is not able to capture anything, except XHR requests.

So what could I do to solve this problem? Is there a way for Cypress to capture "fetch" requests? Is there a way for react-apollo to use "xhr" instead of "fetch"?

解决方案

An easy workaround is to use whatwg-fetch, which you'd add as a dependency via npm and then...

cypress/support/fetch_to_xhr.js

function fetchToXhr() {
  let polyfill

  before(() => {
    cy.readFile('node_modules/whatwg-fetch/dist/fetch.umd.js')
      .then((contents) => polyfill = contents)
    Cypress.on('window:before:load', (win) => {
      delete win.fetch
      win.eval(polyfill)
    })
  })
}

fetchToXhr()

cypress/support/index.js

import "./fetch_to_xhr";

After that cypress will capture the graphql requests

这篇关于柏树与阿波罗之间的相容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆