You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress 7中无法覆盖Intercept拦截器的问题排查

Cypress 7版本中无法覆盖Intercept拦截器的问题

我正在从Cypress 5版本升级到7版本,目前卡在拦截器覆盖的问题上——尝试覆盖beforeEach中定义的Intercept拦截器始终失败。

查资料时发现信息矛盾:有的内容提到设置同路径但不同别名的拦截器时,只有第一个会触发;但另一些内容表示,相似路径不同别名的拦截器都会触发。

我的代码如下:

...
describe("Loading Inventory", () => {
   beforeEach(() => {
      cy.intercept("GET", "/ui_connect/inventory/**", {
         fixture: "operator/file.json",
         statusCode: 200,
     }).as("checkInventoryRequest");
   });

   it("My test", () => {
      cy.intercept("GET", "/ui_connect/inventory/**", {
         body: {},
         statusCode: 500,
      }).as("checkInventoryRequestWithError");

      cy.get("[data-test=inventory-button]").click();  // 触发请求
       
      cy.wait("@checkInventoryRequestWithError").then(() => {
       
         cy.get(".e-alert-dialog")
            .contains("Inventory Generation Error")
            .should("be.visible");
         });
      });
   });
});

我想要覆盖beforeEach里定义的拦截器,但请求始终返回200状态码和fixture中的内容,哪怕把两个拦截器设为相同别名也没用。

根据Cypress官方文档,请求阶段应该采用最后定义的拦截器并终止处理,但实际情况并非如此。请问我的代码哪里有问题?

内容的提问来源于stack exchange,提问作者Ricardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 19:12:26