Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress.Commands.add type doesn't take into account prevSubject to define the type of the custom command #18940

Closed
IlCallo opened this issue Nov 16, 2021 · 7 comments · Fixed by #19003

Comments

@IlCallo
Copy link
Contributor

IlCallo commented Nov 16, 2021

Current behavior

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Custom command to select DOM element by data-cy attribute.
       * @example cy.dataCy('greeting')
       */
      dataCy<E extends Node = HTMLElement>(value: string): Chainable<JQuery<E>>;
    }
  }
}

Cypress.Commands.add(
    'dataCy',
    { prevSubject: 'optional' },
    (subject, value) => {
      return cy.get(`[data-cy=${value}]`, {
        withinSubject: subject,
      });
    },
  );

subject and value are inferred as any as prevSubject isn't taken into account and TS expect a single param
If value is removed, the first param becomes string which is wrong
The same problem applies to child commands

Desired behavior

Types should take into consideration prevSubject and use it into a conditional to provide correct typings
There may be other edge cases with element | document | window

Test code to reproduce

https://github.com/cypress-io/cypress/pull/17496/files#diff-facdf706a5238380b5e97c040bc1a5f5cb89e3116f878754dd7fb4e693787715R77

This test is wrong, it should be

Cypress.Commands.add('newCommand', { prevSubject: true }, (subject, arg) => {
    // $ExpectType string
    arg
    return
  })

Cypress Version

9.0.0

Other

No response

@IlCallo IlCallo changed the title Cypress.Commands.add type doesn't take into account prevSubject to define the type of the function Cypress.Commands.add type doesn't take into account prevSubject to define the type of the custom command Nov 16, 2021
@villanuevaSEAT
Copy link

We have the same issue 😢

@Nodonisko
Copy link

same issue here, problem is both for dual and child custom commands

@radan-iml
Copy link

Same here, introduced with this PR, like OP said wrong tests.

@Nodonisko
Copy link

Nodonisko commented Nov 18, 2021

Also this test for optional subject should be added:

Cypress.Commands.add('newCommand', { prevSubject: 'optional' }, (subject, arg) => {
    // $ExpectType string
    arg
    return
  })

@keeema
Copy link

keeema commented Nov 19, 2021

Same issue:

This was working in Cypress < 9.0.0

declare namespace Cypress {
    interface Chainable<Subject> {
        myCommand(options?: Partial<Cypress.Loggable>): Cypress.Chainable<Window>;
    }
}
Cypress.Commands.add(
    "myCommand",
    {
        prevSubject: ["optional", "window"],
    },
    (prevSubject?: Window, options?: Partial<Cypress.Loggable>): Cypress.Chainable<Window> => {
       ....some code....
    }
);

It is not working in Cypress 9.0.0.
I have looked to given example mentioned in PR.
But cypress-image-snapshot is JS plugin with external d.ts. Not a TS plugin.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: work in progress and removed stage: needs review The PR code is done & tested, needs review labels Nov 19, 2021
@cypress-bot cypress-bot bot added stage: waiting stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Dec 3, 2021
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Dec 13, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 13, 2021

The code for this is done in cypress-io/cypress#19003, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 21, 2021

Released in 9.2.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v9.2.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants