I am implementing a custom Cypress in TypeScript:
// support/commands.js
const login = () => {
console.log('Logging in...');
};
Cypress.Commands.add('login', login);
declare namespace Cypress {
interface Chainable {
login: typeof login;
}
}
I try to call it using:
describe('Login Scenario', () => {
it('should allow a user to login', () => {
cy.visit('/');
cy.login();
});
});
Yet, it seems the command is not set up:
TypeError: cy.login is not a function
If I write the command in pure JavaScript (removing the namespace declaration and updating the call to (cy as any).login();
, it works.
What am I missing?
I fixed it by adding index.d.ts file in my commands folder. In this file I added something like this:
If You don't import or export anything, just omit global namespace declaration:
Keep in mind that it won't work with Typesciprt < 2.3, becuase default generics type has to be supported.
Here is what I use and I do not have to add
/// <reference types="cypress" />
at the top of every file.I have my custom typings under
<projectroot>/cypress/support/index.d.ts
And my
<projectroot>/cypress/tsconfig.json
looks likeAnd TypeScript is finally happy