在 Cypress 中移除拦截器

4

我有一个拦截器,可以提供一个类似于以下的模拟JSON响应:

    cy.intercept('GET', '**/api/v1/myroute/*', { fixture: 'myData.json' }).as('myAlias')

我是否有办法在测试进行到一半时删除这个拦截器呢?我希望删除别名,以便xhr请求根本不被拦截。谢谢!

2个回答

1

好的,我想通了。只需要这样做:

cy.intercept('GET', '**/api/v1/myroute/*', (req) => {
  req.continue()
});

你是怎么实现的?对我来说它没有改变结果:cy.intercept('/api/console/profile', (req) => { req.continue((res) => { res.send({ result: updatedPhoneNumberProfile, }); }); }) - SalahAdDin
您可以稍后通过将处理程序设置为null来拦截相同的路由,从而将其移除。 - undefined

1
你可以尝试使用RouteMatchertimes选项,就像这样:
cy.intercept({
  method: 'GET',
  pathname: '/api/v1/myroute/*'
  times: 1
}, { fixture: 'myData.json' }).as('myAlias')

因此,当第二次调用时,它不会被拦截


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接