5

I've setup custom commands for my Cypress tests :

// cypress/support/commands.ts
/// <reference types="cypress"/>

Cypress.Commands.add('getByTestId', (target: [string], timeout: number = 0) => {
    target[0] = `[data-testid="${target[0]}"]`;
    return cy.get(target.join('>'), { timeout });
});



// cypress/support/index.ts
/// <reference types="cypress" />
declare global {
namespace Cypress {
    interface Chainable {
        getByTestId(id: string): Chainable<Element>;
    }
  }
}

these are my files where I setup my commands.

When writing test I have this error even if I had this on the top of my file /// <reference types="cypress" /> :

cypress/integration/filename.test.e2e.ts

sample.test.e2e.ts

2 Answers 2

3

I also have Cypress TypeScript support and ran into some issues at the beginning. The following setup worked for me:

// cypress/plugins/index.ts
/// <reference types="cypress" />

// cypress/support/commands.ts
declare namespace Cypress {
  interface Chainable<Subject> {
    getByTestId(selector: string, ...options: any): Chainable<JQuery<HTMLElement>>;
  }
}

// cypress/support/commands.ts
Cypress.Commands.add('getByTestId', (selector, ...options) => {
  return cy.get(`[data-test=${selector}]`, ...options);
});

So I have the type reference in the plugins/index.ts file and the namespace declaration as well as custom command definitions in support/commands.ts. I am using the currently newest Cypress version 9.3.1.

Additionally, another thing I've also noticed is when you try to import stuff into commands.ts, you will also get the error you described.

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

1 Comment

This answer helped me resolve a similar issue with custom commands in version 11.2.0 There is no plugins/index.ts file after V10 so I added the namespace declaration to the
1

This worked for me :

// tsconfig.json

"include": [
    "cypress/support/*.ts"
]

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.