2

I have this error, cy. is not a function:

enter image description here

But I already updated the e2e.ts with this:

import './commands';

Here is my commands.ts:

    declare namespace Cypress {
  interface Chainable<Subject = any> {
    login(email: string, password: string): typeof login;
  }
}
//
function login(email: string, password: string): void {
  cy.visit('/login');
  cy.url().should('includes', 'login');
  cy.contains('Please sign in');
  cy.get('[name=email]').type(email);
  cy.get('[name=password]').type(password);
  cy.get('button').click();
}
//
// NOTE: You can use it like so:
Cypress.Commands.add('login', login);

What Am I missing here?

Thank you

Fody
  • 23,754
  • 3
  • 20
  • 37
Asdf1234567
  • 476
  • 1
  • 10
  • 25

1 Answers1

1

I think you need to add declare global { around the type definition.

declare global {
  declare namespace Cypress {
    interface Chainable<Subject = any> {
      login(email: string, password: string): typeof login;
    }
  }
}

See Set Up TypeScript on Cypress in 4 Steps Easily for a reasonably straight-forward discussion.


See also How to declare types for Cypress custom commands for an alternative.

Fody
  • 23,754
  • 3
  • 20
  • 37