Skip to content

Fix the cy.stub type signature #4302

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

Merged
merged 3 commits into from
May 24, 2019
Merged

Fix the cy.stub type signature #4302

merged 3 commits into from
May 24, 2019

Conversation

NoriSte
Copy link
Contributor

@NoriSte NoriSte commented May 24, 2019

Hi
The cy.stub TypeScript signature is wrong. I started from your Add example to cy.stub() for stubbing window.prompt behavior but, with the same code, I get an error form TypeSctypt

Property 'returns' does not exist on type 'Agent<SinonStub<any[], any>>'.

see the screenshot of the code
ts-stub
In the above screenshot, I show you how everything works fine with the log, as and withArgs functions added by the Agent type.

The problem is that the cy.stub function returns a Agent<sinon.SinonStub> but the Agent itself doesn't extend the sinon.SinonStub type.

Since the code

cy.stub(win, "prompt").returns("yes");

works perfectly in a standard JavaScript spec file, the error is in the TypeScript definitions only.

The easiest solution I found is to rename the Agent type to SinonSpyAgent and declare a new type Agent with the following signature

type Agent<T> = SinonSpyAgent<T> & T

I made it because both the cy.stub and cy.spy functions return the Agent type.

Doing so:

  • the cy.stub and cy.spy signatures don't change
  • TypeScript no more throws an error

Let me know if I missed something and thank you for all, Cypress is simply amazing 😉

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@CLAassistant
Copy link

CLAassistant commented May 24, 2019

CLA assistant check
All committers have signed the CLA.

@jennifer-shehane
Copy link
Member

There are some failures in our TS linter. @NoriSte Can you look into these?

Error: /root/cypress/cli/types/index.d.ts:1811:1
ERROR: 1811:1   no-trailing-whitespace  trailing whitespace
ERROR: 1812:33  expect                  TypeScript@next compile error: 
Type 'T' does not satisfy the constraint 'SinonSpy'.

/root/cypress/cli/types/tests/actions.ts:6:31
ERROR: 6:31     expect                  TypeScript@next compile error: 
Parameter 'text' implicitly has an 'any' type.
ERROR: 7:3      expect                  Expected type to be:
  string
got:
  any
ERROR: 10:29    expect                  TypeScript@next compile error: 
Parameter 'text' implicitly has an 'any' type.
ERROR: 11:3     expect                  Expected type to be:
  string
got:
  any

    at /root/cypress/cli/node_modules/dtslint/bin/index.js:189:19
    at Generator.next (<anonymous>)
    at fulfilled (/root/cypress/cli/node_modules/dtslint/bin/index.js:5:58)
    at <anonymous>

@NoriSte
Copy link
Contributor Author

NoriSte commented May 24, 2019

Sorry @jennifer-shehane, I've just fixed it

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@flotwig flotwig merged commit d55acde into cypress-io:develop May 24, 2019
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 27, 2019

Released in 3.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants