为什么 运行 Cypress on a React/GraphQL app return 正常浏览时不会出现网络错误?

Why does running Cypress on a React/GraphQL app return network errors when normal browsing doesn't?

我有一个 React/apollo 客户端和一个基于 GRANDStack 的 apollo/neo4j 后端应用程序。 我的 React 应用程序在 localhost:3000 上运行,而我的 GraphQL 在 localhost:4001/graphql 上运行,它们之间的通信没有失败。一切在应用程序中运行良好(启用 CORS),但我想使用 Cypress 进行测试。

我是否应该期望 Cypress 能够无误地观察 React 和 GraphQL 之间的流程?或者这超出了它的能力范围?

我试过的: 我设置了 Cypress 和 运行 以下测试:

it("Opens myPlan.", function() {
    cy.visit("localhost:3000/myPlan");
    cy.wait(6000);
});

在第一次设置 cypress 时,我的网站被加载了。该应用程序首先要做的事情之一是 graphql 查询一些值,并根据这些值创建一个下拉框。虽然这个和所有其他 graphql 请求在浏览器中工作正常,但我在 cypress 中收到相同代码的 "{"graphQLErrors":[],"networkError":{"name":"ServerParseError","response":{},"statusCode":404,"bodyText":""},"message":"Network error: Unexpected end of JSON input"}" 错误。

据推测,问题是因为有 2 个端点,而 cy.visit 只允许一个。我尝试禁用 ChromeWebSecurity 并尝试使用 "Access-Control-Allow-Origin-master" 插件。

编辑:我找到了一个了解 Cypress 的人,他们建议添加:

"proxy": "http://localhost:4001/",

到我的 React 客户端配置。这避免了多端口问题,并且 Cypress 可以正常工作。

编辑:我找到了一个了解 Cypress 的人,他们建议添加:

"proxy": "http://localhost:4001/",

到我的 React 客户端配置。这避免了多端口问题,并且 Cypress 可以正常工作。