Using Cypress with Typescript gives me a type error for "get" in the example in the docs JQuery not defined

Viewed 216

I'm trying to setup Typescript with Cypress following the docs found here: https://docs.cypress.io/guides/tooling/typescript-support#Configure-tsconfig-json

  • I already have typescript installed for my server/app.
  • I add a tsconfig.json in the cypress/ directory
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "dom"],
    "types": ["cypress"]
  },
  "include": ["**/*.ts"]
}
  • I add their code in cypress/support/index.ts:
// in cypress/support/index.ts
// load type definitions that come with Cypress module
/// <reference types="cypress" />

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Custom command to select DOM element by data-cy attribute.
       * @example cy.dataCy('greeting')
       */
      dataCy(value: string): Chainable<Element>
    }
  } 
}
  • I add THEIR example command in my commands.ts folder, instead of the `same file as above.
// cypress/support/commands.ts (instead of index.tx)
Cypress.Commands.add('dataCy', (value) => {
  return cy.get(`[data-cy=${value}]`)
})

The callback function gives me the following error:

Argument of type '(value: string) => Chainable<JQuery<HTMLElement>>' is not assignable to parameter of type '(value: string) => Chainable<Element>'.

So I update the index.ts callback function

  • From dataCy(value: string): Chainable<Element>
  • To dataCy(value: string): Chainable<JQuery<HTMLElement>>

But now my index.ts has the following error:

'JQuery' is not defined.

And I cannot find anywhere/anyway to import JQuery to remove the issue.

Thanks in advance for your help.

1 Answers

Try setting the return type to Cypress.JQuery<HTMLElement> rather than Chainable<Element>

Related