1

I started a new project with yarn and added TypeScript and cypress as dependencies. I created an example test with a custom command. The test is in file cypress/e2e/cypress/example.cy.ts and reads:

describe('Example', () => {
        it('example', () => {
                cy.myCommand();
        })
})

The command is in cypress/e2e/cypress/support/commands.ts and reads:

Cypress.Commands.add('myCommand', () => {
        cy.visit('https://example.cypress.io')
})

When I run this in Cypress, my test fails with the following error: "cy.myCommand is not a function". How can I configure my tests correctly?

I am using Cypress 13.6.5.

3
  • Does this answer your question? How to make custom cypress commands work with typescript Commented Feb 26, 2024 at 18:31
  • Thanks for this suggestion, @agoff. The answer was helpful, but it was not exactly what I wanted. I did not like to introduce index.ts because that file was not generated by Cypress like e2e.ts. Furthermore I did not know where to put index.ts. Commented Feb 27, 2024 at 10:54
  • I have upvoted the question you are referring to and the answer it currently has. Commented Feb 27, 2024 at 11:03

1 Answer 1

2

When you add cypress and TypeScript as dependencies, you do not automatically have a correct configuration. By combining some similar answers, I found the following solution:

  • Rename cypress/e2e/cypress/support/e2e.js to cypress/e2e/cypress/support/e2e.ts. This file just imports commands.{js|ts}.

  • Update commands.ts to be as follows:

    declare namespace Cypress {
        interface Chainable<Subject = any> {
            myCommand(): Chainable<any>;
        }
    }
    
    Cypress.Commands.add('myCommand', () => {
        cy.visit('https://example.cypress.io')
    })
    
  • Rename cypress.config.js to cypress.config.ts (in the root directory of your project).

  • Add the following line to cypress.config.ts:

    supportFile: 'cypress/e2e/cypress/support/e2e.ts'
    

EDIT May 31 2024: I was also using yarn as my package manager. I had to create .yarnrc.yml with the following contents:

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.1.1.cjs

This causes yarn to create directory node_modules at the yarn project root instead of somewhere else. Now VSCode find types in node_modules needed for syntax checking.

I did so based on: Typescript cannot find type in definition file in node_modules folder

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

1 Comment

Maybe it would strictly be Chainable<undefined> since that particular command has no return value and Cypress defaults to undefined when you don't make an explicit return, but Chainable<any> is a good catch-all.

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.