1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure? That's not how it works on the example and looking at other repos I can't see them doing it like that.
Hey, I added a GitHub thread to the answer. I was facing the same issue as you, I was able to resolve it by these two changes.
Thanks, by removing the import from my commands.ts I fixed it (well I have more errors to fix but at least not around my commands)
Why you don't return? What if this is in an external npm package?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.