首页 > 解决方案 > 更改 QraphQL 端点的 cypress 测试中的路由数据

问题描述

我有一个使用 QraphQL 的 Angular 应用程序。我在 cypress 中编写了一个测试,如下所示:

it('should do something', () => {
  cy.server();
  cy.route('POST', '/graphql', 'fixture:data1.json');
  // data is loaded from the endpoint to populate the page
  cy.visit('http://localhost:3000/#/something/somethingElse');

  // when the button is clicked, the same endpoint is called again,
  // but now I need it to deliver some other data.
  cy.get('button')
    .click();
});

任何人都可以为我提供一种设置方法吗?提前致谢

标签: javascripttestinggraphqlintegration-testingcypress

解决方案


也许你可以使用easygraphql来解决这个挑战!!有多个包可以使用:

  1. easygraphql-now:您可以在您的脚本上创建一个脚本,该脚本package.json"easygraphql-now schema.gql --graphiql --local -p=7000"在您传递架构路由、本地和 graphiql 标志以及端口的位置运行......所以当您运行它时;它将创建传递模式的模拟服务器,因此您的 Angular 应用程序将向服务器发出请求,该服务器将响应您的查询的模拟。这是一个有用的帖子。

  2. easygraphql-mock:如果你想返回一个完整的类型的模拟,你可以使用这个包,有了这个,你不必为每种类型创建夹具。

  3. easygraphql-tester:它类似于easygraphql-mock但不同之处在于您可以返回查询的模拟,请查看文档

如果您决定使用easygraphql来解决这个问题,请随时在 repo 上创建示例,这里有一个问题使用 Cypress 创建示例


推荐阅读