Following Cypress guidelines, I managed to register my Cypress custom commands into my IDE through Typescript. The only thing I'm having trouble with is the default values for my commands.
Basically:
- I create a command with default values
- I register the command in the
index.d.ts
- I mention the default values in the JSDoc
- When using this command, if I don't specify some arguments that have default values, my IDE is unhappy
Snippet example:
// commands.js
Cypress.Commands.add("rsChoose", {
prevSubject: true
}, (subject, text="", num=1, all=false) => {
// Open the select
cy.wrap(subject)
.click();
// Display all options
if (all === true) {
cy.wrap(subject)
.find(rsShowMoreButton)
.click();
}
// Alias the option list
cy.wrap(subject)
.find(rsOptions)
.as("options");
// Filter our options with our text
if (text.length > 0) {
cy.get("@options")
.contains(text)
.as("options");
}
// Choose the first matching option
cy.get("@options")
.eq(num - 1)
.click();
return cy.wrap(subject)
});
// index.d.ts
declare namespace Cypress {
interface JQuery<HTMLElement> {
/**
* Allows you to select an option from a FieldBox React select based on a given text
* If no text is given, will simply pick the first option
* Most select only display a limited amount of options. We can display them using the "all" param
* @prevSubject subject
* @param {String} [text=""] - The text our option must contain
* @param {Number} [num=1] - The option number we want to get (starts at 1)
* @param {Boolean} [all=false] - Forces the select to display all the available options
* @returns {Cypress} - The original subject (Cypress-wrapped select wrapper)
*/
rsChoose(text, num, all): JQuery<HTMLElement>
}
}
The error in my IDE:
I'm not sure what to change to fix this issue :(