Cypress Custom TypeScript Command is not a Function

后端 未结 3 1836
感动是毒
感动是毒 2021-02-19 19:13

I am implementing a custom Cypress command in TypeScript:

// support/commands.ts
const login = () => {
    console.log(\'Logging in...\');
};

Cypress.Command         


        
3条回答
  •  广开言路
    2021-02-19 19:34

    I had the same issue and all solutions I found did not work for me. Having done everything from the official Cypress documentation and other solutions here I still got the error cy.login is not a function.

    The problem was that I renamed every .js file to .ts and cypress/support/index.ts was not loaded any more, because per default Cypress only loads the JavaScript one. To fix it, you need to change it to .ts in cypress.json like that (the same with plugins file):

    {
      "supportFile": "cypress/support/index.ts",
      "pluginsFile": "cypress/plugins/index.ts"
    }
    

    You can also add the part with declare namespace Cypress {... to commands.ts instead of creating an index.d.ts file, to have the declaration and implementation in the same file

提交回复
热议问题