我有一个代码块,在异步请求后检查响应。
graphql.ts snippet:
19 return cy.request({...}).then((response) => {
20 expect(response.status, 'Invalid response status').to.equal(200)
21 return cy.wrap(response.body.data)
22 })
我的测试失败,出现错误:
CypressError: `cy.should()` failed because you invoked a command inside the callback. `cy.should()` retries the inner function, which would result in commands being added to the queue multiple times. Use `cy.then()` instead of `cy.should()`, or move any commands outside the callback function.
实际上,没有使用should()
。但是堆栈跟踪显示,cy.get()
从then()
块内的expect(response.status, 'Invalid response status').to.equal(200)
行调用。
> `cy.get()`
at $Cypress.commandEnqueued (http://172.18.0.20/__cypress/runner/cypress_runner.js:135214:76)
at $Cypress.listener (http://172.18.0.20/__cypress/runner/cypress_runner.js:86503:17)
...
From Your Spec Code:
at Context.eval (infra/api/graphql.ts:20:10)
有人能解释一下这种行为吗?
UPD:我的调用堆栈看起来像:
Then('updated pipeline is available via API', function (table) {
cy
.getUserInfo()
.then(usr => {
retryWithTimeout(retry => {
requestGraphQL(pipelineQuery, usr.api_key, vars)
.should(res => {
cy.get(`@${Alias}`).then(alias => {
if (somecondition) retry('Msg')
....
})
})
}, { timeout: 120, delay: 20 })
})
})
const requestGraphQL = (body: string, authKey: string, vars: any = {}, url= graphqlUrlPath): Chainable => cyRequest(body, authKey, vars, url)
const cyRequest = (queryStr: string, authKey: string, vars: any, url: string): Chainable<any> => {
return cy.request({...}).then((response) => {
expect(response.status, 'Invalid response status').to.equal(200)
return cy.wrap(response.body.data)
})
}
1条答案
按热度按时间r55awzrz1#
.should(callback)
命令有内置的重试。如果回调函数抛出,它将重复到设置的超时。这意味着
cy.get('@${Alias}')...
可能被添加到队列中数百次,这显然不是您想要的。Cypress有一些内置的检查功能,可以查找像这样的命令溢出,但它不知道您在
.should()
中到底做了什么,只知道命令队列被修改了。很难判断测试的目标是什么,但解决这个问题的简短答案是在调用
.should()
之前访问别名。大概是这样的
你的特殊功能的细节将需要给予你一个完整的工作样本。