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

Strongly typing the invoke function #4022

Closed
floydjdx opened this issue Apr 24, 2019 · 7 comments · Fixed by #4907
Closed

Strongly typing the invoke function #4022

floydjdx opened this issue Apr 24, 2019 · 7 comments · Fixed by #4907
Labels
good first issue Good for newcomers type: typings Issue related to Cypress types (for TypeScript)

Comments

@floydjdx
Copy link

floydjdx commented Apr 24, 2019

Current behavior:

invoke function is not strongly typed.

Desired behavior:

invoke function is strongly typed.

TypeScript has a ReturnType type that you can use

invoke<K extends keyof Subject>(functionName: K, ...args: any[]): Chainable<ReturnType<Subject[K]>>
@flotwig
Copy link
Contributor

flotwig commented Apr 24, 2019

Hey, thanks for pointing this out!

The types definition file that needs to be updated is here: https://github.com/cypress-io/cypress/blob/develop/cli/types/index.d.ts

If you'd like to, you can open a pull request with the changes that you think are necessary.

@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label Apr 24, 2019
@jennifer-shehane jennifer-shehane added the good first issue Good for newcomers label Apr 25, 2019
mayfieldiv added a commit to mayfieldiv/cypress that referenced this issue Aug 2, 2019
mayfieldiv added a commit to mayfieldiv/cypress that referenced this issue Aug 2, 2019
mayfieldiv added a commit to mayfieldiv/cypress that referenced this issue Aug 2, 2019
mayfieldiv added a commit to mayfieldiv/cypress that referenced this issue Aug 2, 2019
mayfieldiv added a commit to mayfieldiv/cypress that referenced this issue Aug 2, 2019
mayfieldiv added a commit to mayfieldiv/cypress that referenced this issue Aug 2, 2019
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope stage: work in progress labels Aug 2, 2019
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Aug 6, 2019
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Aug 6, 2019

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

bahmutov pushed a commit that referenced this issue Aug 6, 2019
* add strong return type to 'invoke' command

* refactor invoke for clarity. use dtslint tests

* add strong args types using typescript 3.0+ features

* handle invoke's jquery wrapping of its input

* move invoke tests to namespace

* add example to jsdocs of 'invoke'
bahmutov added a commit that referenced this issue Aug 6, 2019
This reverts commit 15685db.

The type for `invoke` command seems to break jQuery method
that take an argument like

```js
cy.get('.trigger-input-range')
  .invoke('val', 25)
```
@bahmutov
Copy link
Contributor

bahmutov commented Aug 6, 2019

Reverted initial work in 6930cab - we need to handle the case

cy.get('.trigger-input-range')
  .invoke('val', 25)

@bahmutov bahmutov reopened this Aug 6, 2019
@dmtrKovalenko
Copy link
Contributor

dmtrKovalenko commented Aug 7, 2019

The problem is inside function overloading. Typescript takes only the first function declaration signature. Related issues in typescript repo:
microsoft/TypeScript#29732
microsoft/TypeScript#32164

grabartley pushed a commit to grabartley/cypress that referenced this issue Oct 6, 2019
…io#4907)

* add strong return type to 'invoke' command

* refactor invoke for clarity. use dtslint tests

* add strong args types using typescript 3.0+ features

* handle invoke's jquery wrapping of its input

* move invoke tests to namespace

* add example to jsdocs of 'invoke'
grabartley pushed a commit to grabartley/cypress that referenced this issue Oct 6, 2019
…ypress-io#4907)"

This reverts commit 15685db.

The type for `invoke` command seems to break jQuery method
that take an argument like

```js
cy.get('.trigger-input-range')
  .invoke('val', 25)
```
@gabbersepp
Copy link
Contributor

I understand that this is not the best solution as overloads won't work as expected.
But they won't work currently neither.

With this fix the typings ovf invoke will be correct in most of the cases.

An example I stumbled across today:

        it.only("asd", () => {
            cy.wrap({ fn: () => ({a: 1})})
            .invoke("fn")
            .its("a")
            .should('eq', 1);
        })

image

So I wanted to know if there are any plans to merge this PR again?

@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: needs review The PR code is done & tested, needs review labels Feb 26, 2020
@jennifer-shehane jennifer-shehane added type: typings Issue related to Cypress types (for TypeScript) and removed topic: typescript labels Mar 3, 2020
@CypressJoseph
Copy link
Contributor

I think this is basically done as of #6667 (I will look again but I think we tackled most of these forms there.)

@jennifer-shehane jennifer-shehane added stage: pending release and removed stage: ready for work The issue is reproducible and in scope labels Mar 16, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Mar 16, 2020

Released in 4.2.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Mar 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers type: typings Issue related to Cypress types (for TypeScript)
Projects
None yet
7 participants