How to stub a call to graphql using cypress?

末鹿安然 提交于 2020-05-15 06:30:46

问题


I'm writing a Vue app that uses vue-apollo to interact with graphql. I'm wondering if it's possible to stub the graphql requests. I thought this should work:

  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');
  });

Unfortunately, I get an error from graphql complaining that id is an Int instead of an ObjectId. Am I missing something?


回答1:


The problem was that stubbing fetch requests isn't yet implemented in Cypress (which is what Vue Apollo is using). I ended up following these instructions:

  • Install github/fetch
  • Add this to cypress/support/index.js:

.

Cypress.on('window:before:load', win => {
  win.fetch = null;
  win.Blob = null;
});

Now it works!



来源:https://stackoverflow.com/questions/49079005/how-to-stub-a-call-to-graphql-using-cypress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!