参数类型字符串不可分配给参数类型 keyof Chainable ... 在赛普拉斯

Argument type string is not assignable to parameter type keyof Chainable... in Cypress

在 Cypress 中更新 9.0.0 后出现以下错误

参数类型字符串不可分配给参数类型keyof Chainable...类型字符串不可分配给类型“and” | “作为” | “模糊” | “检查” | “children” | “清楚” | “清除Cookie” | “清空饼干” | “清除本地存储” | “点击” | “时钟” | ...类型字符串不可分配给类型“拦截”,这会影响我所有的自定义命令

有人可以帮助我吗? My custom command

从版本 9.0.0 开始,您现在必须声明您的自定义命令。 参见 changelog for 9.0.0 (6th bullet point under breaking changes) and see the specific information about custom commands now being typed based on the declared custom chainable here.

此外,请参阅此recipe了解如何添加自定义命令并正确声明它们。

对于您的自定义命令,使用以下代码添加此文件 cypress/support/index.d.ts

/// <reference types="cypress" />

declare namespace Cypress {
    interface Chainable<Subject = any> {
        /**
         * Custom command to ... add your description here
         * @example cy.clickOnMyJourneyInCandidateCabinet()
         */
        clickOnMyJourneyInCandidateCabinet(): Chainable<null>;
    }
}

在 support/index.d.ts

declare namespace Cypress {
  interface Chainable<Subject = string> {
    preventSubmit(form: string): Chainable<Element>;
  }
}

在support/commnad.js

Cypress.Commands.add("preventSubmit", (form) => {
  cy.get(form).then((form$) => {
    form$.on("submit", (e) => {
      e.preventDefault();
    });
  });
  cy.log(`prevent default submit to '${form}'`);
});

在specs/test.js

describe("MyTest", () => {
  ...
  it("Test 1", () => {
    ...
    cy.preventSubmit("form");
    cy.get("form").submit();
  }
}