运行 e2e 测试时在 cypress 中模拟特定的 graphql 请求 [英] Mock specific graphql request in cypress when running e2e tests
问题描述
在使用 Cypress 运行 e2e 测试时,我的目标是模拟特定的 graphql 查询.
When running e2e tests with Cypress, my goal is to mock a specific graphql query.
目前,我可以像这样模拟所有请求:
Currently, I can mock all requests like this:
cy.server();
cy.route('POST', '/graphql', {
data: {
foo: 'bar'
},
});
问题是这会模拟 all /graphql
查询.如果我能以某种方式说:
The problem is that this mocks all /graphql
queries. It would be awesome if I somehow could say:
cy.route('POST', '/graphql', 'fooQuery', {
data: {
foo: 'bar'
},
});
在我们的应用程序中,我们使用 Apollo Graphql - 因此所有查询都被命名.
In our application, we are using Apollo Graphql - and thus all queries are named.
推荐答案
With cypress 6.0 route
和 route2
被弃用,建议使用intercept代码>.如文档中所述(https://docs.cypress.io/api/commands/intercept.html#Aliasing-individual-GraphQL-requests) 你可以通过这种方式模拟 GraphQL 请求:
With cypress 6.0 route
and route2
are deprecated, suggesting the use of intercept
. As written in the docs (https://docs.cypress.io/api/commands/intercept.html#Aliasing-individual-GraphQL-requests) you can mock the GraphQL requests in this way:
cy.intercept('POST', '/api', (req) => {
if (req.body.operationName === 'operationName') {
req.reply({ fixture: 'mockData.json'});
}
}
这篇关于运行 e2e 测试时在 cypress 中模拟特定的 graphql 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!