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

fix: include event emitter types from cypress types #14992

Merged
merged 2 commits into from Feb 10, 2021

Conversation

bahmutov
Copy link
Contributor

@bahmutov bahmutov commented Feb 9, 2021

User facing changelog

Types no longer complain when you chain event listeners like

Cypress
  .on('test:before:run', () => {
    console.log('before running a test')
  })
  .on('test:after:run', () => {
    console.log('after running a test')
  })

Additional details

Notice the types returned void while the test happily runs. Also moved the emitter reference line into the file where it was used.

How has the user experience changed?

No more type errors

PR Tasks

  • Have tests been added/updated?
  • Has the original issue or this PR been tagged with a release in ZenHub?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 9, 2021

Thanks for taking the time to open a PR!

@cypress
Copy link

cypress bot commented Feb 9, 2021



Test summary

9314 0 118 3Flakiness 1


Run details

Project cypress
Status Passed
Commit aef3b47
Started Feb 9, 2021 3:23 PM
Ended Feb 9, 2021 3:34 PM
Duration 10:47 💡
OS Linux Debian - 10.5
Browser Multiple

View run in Cypress Dashboard ➡️


Flakiness

cypress/integration/retries.ui.spec.js Flakiness
1 runner/cypress retries.ui.spec > opens attempt on each attempt failure for the screenshot, and closes after test passes

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@bahmutov bahmutov changed the title chore: include event emitter types from cypress types fix: include event emitter types from cypress types Feb 9, 2021
@@ -1,4 +1,5 @@
/// <reference path="./cypress-npm-api.d.ts" />
/// <reference path="./cypress-eventemitter.d.ts" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are using NodeEventEmitter type here

@bahmutov bahmutov marked this pull request as ready for review February 9, 2021 15:39
Copy link
Contributor

@sainthkh sainthkh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I became curious that on really returns Cypress. So, I tested with the code below:

it('t', () => {
  console.log(Cypress
  .on('test:before:run', () => {
    console.log('before running a test')
  }).version)
})

And it returned 6.4.0 correctly.


In #14991, const cy: Cypress.cy & EventEmitter was mentioned but not covered in this PR.

I tried extending EventEmitter on Cypress.cy definition like interface cy extends Chainable<undefined>, EventEmitter {} but failed because of the clash in property types. It's an open issue since 2017.

Maybe, the easiest solution for this would be adding type cy = Cypress.cy & EventEmitter above cy definition and set the type with it.

@github-actions
Copy link
Contributor

Internal Jira issue: TR-668

@bahmutov
Copy link
Contributor Author

bahmutov commented Feb 10, 2021

Yup @sainthkh it returns Cypress - I think that's fine for now, we solve some problems first and leave any other refactoring for another day. Of course judging from purely types we would get

cy.on('...', () => {})
  .version // since now we have returned type `Cypress` and not `cy`, but 🤷‍♂️

I tried interface cy extends Chainable<undefined>, EventEmitter too with same no luck, so again will punt on it

@bahmutov
Copy link
Contributor Author

Also, I do find it funny that you can combine clashing types when declaring a variable like const cy: Cypress.cy & EventEmitter, but not when extending an interface.

@bahmutov
Copy link
Contributor Author

I will split #14991 to have the type merge in a separate issue

@bahmutov bahmutov merged commit ff095df into develop Feb 10, 2021
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.

Cypress types need refactoring
3 participants