Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

After update 9.0.0 in Cypress I have the following error

Argument type string is not assignable to parameter type keyof Chainable... Type string is not assignable to type "and" | "as" | "blur" | "check" | "children" | "clear" | "clearCookie" | "clearCookies" | "clearLocalStorage" | "click" | "clock" | ... Type string is not assignable to type "intercept" which affect all my custom commands

Could someone help me? My custom command

like image 700
Oleh Dymych Avatar asked Nov 11 '21 12:11

Oleh Dymych


2 Answers

Beginning with version 9.0.0, You are now forced to declare your custom commands. See the 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.

Also, see this recipe on how to add custom commands and declare them properly.

For your custom command, add this file cypress/support/index.d.ts with the following code:

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

declare namespace Cypress {
    interface Chainable<Subject = any> {
        /**
         * Custom command to ... add your description here
         * @example cy.clickOnMyJourneyInCandidateCabinet()
         */
        clickOnMyJourneyInCandidateCabinet(): Chainable<null>;
    }
}
like image 161
PeaceAndQuiet Avatar answered Sep 28 '22 02:09

PeaceAndQuiet


in support/index.d.ts

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

in 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}'`);
});

in specs/test.js

describe("MyTest", () => {
  ...
  it("Test 1", () => {
    ...
    cy.preventSubmit("form");
    cy.get("form").submit();
  }
}
like image 33
PaKo Anguiano Avatar answered Sep 28 '22 01:09

PaKo Anguiano