如何使用cypress存根对graphql的调用?

如何使用cypress存根对graphql的调用?,graphql,cypress,Graphql,Cypress,我正在编写一个Vue应用程序,它使用Vue apollo与graphql交互。我想知道是否可以存根graphql请求。我想这应该行得通: it('should access a story', function() { cy.server(); cy.route('http://localhost:3002/graphql', { data: { Story: { id: 2, title: 'story title', content: 'sto

我正在编写一个Vue应用程序,它使用Vue apollo与graphql交互。我想知道是否可以存根graphql请求。我想这应该行得通:

  it('should access a story', function() {
    cy.server();
    cy.route('http://localhost:3002/graphql', {
      data: {
        Story: { id: 2, title: 'story title', content: 'story content' }
      }
    });

    cy.visit('/stories/2');
  });

不幸的是,我从graphql中得到一个错误,抱怨
id
Int
而不是
ObjectId
。我遗漏了什么吗?

问题是Cypress中还没有实现存根
获取
请求(这就是Vue Apollo所使用的)。我最后按照指示做了:

  • 安装
  • 将此添加到
    cypress/support/index.js


现在它工作了

我在这里找到了这个软件包:

  • npm i@iam4x/cypress graphql mock

  • 将此行添加到“support/commands.js”

  • 导入“@iam4x/cypress graphql mock”

  • 转到graphiql游乐场并下载您的模式

  • 将任务命令添加到“plugins/index.js”中(记住更改您先前下载的模式文件的路径)

  • 使用加载的模式编写测试
  • 访问原始回购协议以获得进一步解释,因为我发现它不那么令人困惑。您安装的软件包只是此软件包的一个工作分支:
  • Cypress.on('window:before:load', win => {
      win.fetch = null;
      win.Blob = null;
    });
    
    module.exports = (on, config) => {
      on("task", {
        getSchema() {
          return fs.readFileSync(
            path.resolve(__dirname, "../../../schema.graphql"),
            "utf8"
          );
        }
      });
    };
    
    beforeEach(() => {
      cy.server();
      cy.task("getSchema").then(schema => {
        cy.mockGraphql({
          schema
        });
      });
    });`
    
    describe("Login Form", () => {
      it("should redirect after login", () => {
        cy.mockGraphqlOps({
          operations: {
            Login: {
              login: {
                jwt: "some-token",
                user: {
                  id: "5d5a8e1e635a8b6694dd7cb0"
                }
              }
            }
          }
        });
        cy.visit("/login");
        cy.getTestEl("email-input").type("Max Mustermann");
        cy.getTestEl("password-input").type("passwort");
        cy.getTestEl("submit").click();
        cy.getTestEl("toolbar-title").should("exist");
      });
    })