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: Adding overload to screenshot to fix type errors when overwriting this command. #27130

Merged
merged 13 commits into from
Jul 7, 2023

Conversation

0neMiss
Copy link
Contributor

@0neMiss 0neMiss commented Jun 24, 2023

Additional details

  • Why was this change necessary?

When trying to overwrite this command copying what is mentioned in the docs will throw a type error. The example provided in the docs is correct however the type definitions need to have an overload case that accounts for when screenshot is chained from another cypress command. Currently there is no overload that matches this case.

  • What is affected by this change?
    type definitions for the cy.screenshot method.

Steps to test

when copying this code snippet from the cypress docs

Cypress.Commands.overwrite(
  'screenshot',
  (originalFn, subject, fileName, options) => {
    // call another command, no need to return as it is managed
    cy.get('.app')
      .should('be.visible')

      // overwrite the default timeout, because screenshot does that internally
      // otherwise the `then` is limited to the default command timeout
      .then({ timeout: Cypress.config('responseTimeout') }, () => {
        // return the original function so that cypress waits for it
        return originalFn(subject, fileName, options)
      })
  }
)

We should no longer see the type errors outlined in this post from the docs.

cypress-io/cypress-documentation#5323 (comment)

How has the user experience changed?

Should no longer see type errors when trying to overwrite the screenshot command. See the linked comment above for screenshots of the error messages.

PR Tasks

@CLAassistant
Copy link

CLAassistant commented Jun 24, 2023

CLA assistant check
All committers have signed the CLA.

@cypress-app-bot
Copy link
Collaborator

@0neMiss 0neMiss changed the title Adding overload to screenshot to fix type errors when overwriting this command. fix - Adding overload to screenshot to fix type errors when overwriting this command. Jun 24, 2023
@0neMiss 0neMiss changed the title fix - Adding overload to screenshot to fix type errors when overwriting this command. fix: Adding overload to screenshot to fix type errors when overwriting this command. Jun 24, 2023
@cypress
Copy link

cypress bot commented Jun 24, 2023

1 failed and 34 flaky tests on run #48534 ↗︎

1 27936 1349 0 Flakiness 34

Details:

code review changes
Project: cypress Commit: 2d66993bde
Status: Failed Duration: 19:51 💡
Started: Jul 2, 2023 4:24 AM Ended: Jul 2, 2023 4:44 AM
Failed  cypress/e2e/scaffold-project.cy.ts • 1 failed test • launchpad-e2e

View Output Video

Test Artifacts
scaffolding new projects > generates valid config file for pristine project without cypress installed Output Screenshots Video
Flakiness  commands/net_stubbing.cy.ts • 1 flaky test • 5x-driver-electron

View Output Video

Test Artifacts
network stubbing > intercepting request > can delay and throttle a StaticResponse Output Video
Flakiness  cypress/cypress.cy.js • 3 flaky tests • 5x-driver-electron

View Output Video

Test Artifacts
... > correctly returns currentRetry Output Video
... > correctly returns currentRetry Output Video
... > correctly returns currentRetry Output Video
Flakiness  commands/net_stubbing.cy.ts • 1 flaky test • 5x-driver-chrome:beta

View Output Video

Test Artifacts
network stubbing > intercepting request > can delay and throttle a StaticResponse Output Video
Flakiness  e2e/origin/commands/navigation.cy.ts • 1 flaky test • 5x-driver-chrome:beta

View Output Video

Test Artifacts
cy.origin navigation > #consoleProps > .go() Output Video
Flakiness  commands/waiting.cy.js • 1 flaky test • 5x-driver-chrome:beta

View Output Video

Test Artifacts
... > errors > throws when route is never resolved Output Video

The first 5 flaky specs are shown, see all 21 specs in Cypress Cloud.

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings.

@0neMiss 0neMiss marked this pull request as draft June 25, 2023 03:50
@mike-plummer mike-plummer self-assigned this Jun 26, 2023
Copy link
Contributor

@mike-plummer mike-plummer left a comment

Choose a reason for hiding this comment

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

Hey @0neMiss , thanks for chasing this down. Couple things I'll point out:

  1. This PR is marked as a Draft - are you still working on it, or was it ready for review?
  2. Unfortunately there was a release yesterday so there's a conflict on the CHANGELOG file now that'll have to get resolved
  3. Could you take a look at adding a case or two to cli/types/tests/cypress-tests.ts under CypressScreenshotTests so we can validate that Typescript remains happy with this new definition? You can use the // $ExpectType hint to ensure proper subject handling

* @example
* cy.get(".post").screenshot("post-element")
*/
screenshot(context: string, fileName?: string, options?: Partial<Loggable & Timeoutable & ScreenshotOptions>): Chainable<null>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming the context parameter is meant to represent the subject from the previously-chained command? I think that should look more like this (note the Chainable<Subject> instead of Chainable<null>) based on examples elsewhere in this file

screenshot(fileName?: string, options?: Partial<Loggable & Timeoutable & ScreenshotOptions>): Chainable<Subject>,

Copy link
Contributor Author

@0neMiss 0neMiss Jun 28, 2023

Choose a reason for hiding this comment

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

Hey sorry! Yea I'm having a hard time getting the yarn install to work. Something about python versions and some microsoft tools. That's why I marked it as a draft because I haven't actually been able to run and build this locally yet.

I also saw that this check was failing and I wasn't sure what steps to take to resolve that. Any help on either of those things would be appreciated!

I'll update to fix the Chainable<Subject> and the changelog. I actually don't know why I called that variable context haha.

I'll add those test cases as well, granted that I figure out this issue with running locally. If you have some suggestions for me to try i'd be happy to. Otherwise this might have to wait till later this week when I'm a little more available.

Thanks for taking a look @mike-plummer

Copy link
Contributor Author

@0neMiss 0neMiss Jul 2, 2023

Choose a reason for hiding this comment

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

@mike-plummer Ready for another pass I think!

@0neMiss 0neMiss marked this pull request as ready for review July 2, 2023 04:13
@mike-plummer
Copy link
Contributor

Hey @0neMiss , thanks for the update. I pulled this down and made a few tweaks based on what I found (hope you don't mind 😬 ). I think there are really two bits of magic sauce we have to apply to get this working:

  1. On the TS types for screenshot we need to specify the return type as Chainable<Subject> which lets TS infer the subject based on the chaining. This means there's no need for an actual subject parameter, it's implicitly added to the function signature based on how TS infers the command is being invoked.
  2. When overwriting there isn't an implicit chain, so TS can't 100% tell if there's a subject or not. In this case we need to provide appropriate generics to the overwrite command to tell it the command we're overwriting is being overwritten in a way that handles a subject. This is the Cypress.Commands.overwrite<'screenshot', 'element'> bit I added

I also added a couple tests to validate the subject passing behavior. Hopefully CI agrees that things look good (tests run fine locally). LMK if you disagree or see a hole in my logic. I'm going to push one more minor tweak to the changelog just for formatting purposes - assuming you're good with my changes I can wrangle some reviewers and get this merged. We're running a release today so this probably won't make the cut, but we should be able to get it in for the next one

@lmiller1990
Copy link
Contributor

Looks good, did you want more changes or can we ✅ this one @mike-plummer ?

@0neMiss
Copy link
Contributor Author

0neMiss commented Jul 7, 2023

Thanks @mike-plummer for the feedback and the assistance!

@mike-plummer mike-plummer merged commit d56b628 into cypress-io:develop Jul 7, 2023
60 of 62 checks passed
@mike-plummer
Copy link
Contributor

@0neMiss Merged! This should go out in our next release - we typically try to maintain ~2 week release cadence. Thanks again for the contribution!

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jul 10, 2023

Released in 12.17.1.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jul 10, 2023
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 this pull request may close these issues.

None yet

5 participants