4

I am using Typescript for my Cypress tests and I'm trying to add the type definitions to custom commands in order to improve the readability and provide intellisense/autocomplete.

My custom command should only be chained off a multiselect field so it is a child command and used like shown. It can't be chained directly off cy but when I type cy. in my spec files in VS Code it is suggesting in the intellisense that I can use it, even though it is not valid.

Is there a way I can stop it being suggested as an option when typing cy.? I've specified it should be called on an element in the custom command but maybe there's something I need to change in the index.d.ts?

enter image description here

cy.get('MultiselectField1').MultiSelectOptions(true) // Correct usage
cy.MultiSelectOptions(true) // Incorrect usage

commands.js

Cypress.Commands.add('MultiSelectOptions', {
    prevSubject: 'element'
  }, (subject, selected) => {
      if(selected){
        return subject.find('.selected')
      }
      else{
        return subject.find('.unselected')
      }
  })

index.d.ts

declare namespace Cypress {

    interface Chainable {
      /**
       * Returns the selected or unselected options section for a multiselect field 
       * @param {boolean} selected true/false for selected/unselected options respectively 
       * @example cy.get('MultiselectField1').MultiSelectOptions(true)
      */
     MultiSelectOptions(selected: boolean): Chainable<Element>
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
gabs66
  • 61
  • 2
  • 1
    Not sure if this is possible, but note that the Cypress API that do require a previous subject still show up in Intellisense. For example, typing `cy.its` brings up Intellisense, even though using it this way is invalid. I suspect this is because all Cypress API and custom commands are defined on the `Cypress.Chainable` type, regardless of whether they have a previous subject. – natn2323 Feb 05 '21 at 15:57
  • Did you find out how to do it? – distante Nov 20 '21 at 16:25
  • 1
    No, as natn2323 said above, even built-in Cypress child commands are still suggested when typing 'cy.' so I don't think it's possible. Without Cypress adding something to support it at least I guess. – gabs66 Nov 29 '21 at 10:40

0 Answers0