Doctor Who
Doctor Who

Reputation: 1669

Cypress: Typescript custom commands not working

Update: https://github.com/cypress-io/cypress/issues/1065#issuecomment-351769720 Removing an import from my commands.ts fixed it. Thanks

I'm trying to convert my cypress project to use TypeScript. I'm following the info on https://docs.cypress.io/guides/tooling/typescript-support#Types-for-custom-commands

I've fixed all the other compilation errors I was getting but I'm still unable to get any of my custom commands to work for example:

commands.ts:

declare namespace Cypress {
  interface Chainable {
    clickByLinkText(link: string): Chainable<Element>;
  }
}

// Finds and clicks a given link by it's link text
Cypress.Commands.add("clickByLinkText", (link: string) => {
  cy.get("a").contains(link).click();
});

When I try to call the function in my test I get:

TS2339: Property 'clickByLinkText' does not exist on type 'cy & EventEmitter'.

I have import "./commands"; in my support/index.ts

Upvotes: 1

Views: 5001

Answers (1)

Alapan Das
Alapan Das

Reputation: 18624

You have to add your custom commands like this. You can check out the discussion here - https://github.com/cypress-io/cypress/issues/1065#issuecomment-351769720 -

function clickByLinkText(link: string) {
  cy.get("a").contains(link).click();
}
Cypress.Commands.add("clickByLinkText", clickByLinkText)

Also in your tsconfig.json file add "node_modules/cypress" under includes, considering node_modules and tsconfig.json file are at the sam level.

Upvotes: 1

Related Questions