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

Correct window type #6624

Closed
wants to merge 10 commits into from
Closed

Conversation

OliverJAsh
Copy link
Contributor

Previously this would type error:

cy.window().then(window => window.eval('1'));

(window should have type Window & typeof globalThis.)

You may want to fix other instances of Chainable<Window> as well.

  • Closes

User facing changelog

Additional details

How has the user experience changed?

PR Tasks

  • Have tests been added/updated?
  • Has the original issue been tagged with a release in ZenHub?
  • Has a PR for user-facing changes been opened in cypress-documentation?
  • Have API changes been updated in the type definitions?
  • Have new configuration options been added to the cypress.schema.json?

Previously this would type error:

```ts
cy.window().then(window => window.eval('1'));
```
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Mar 3, 2020

Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.

  • Please write [WIP] in the title of your Pull Request if your PR is not ready for review - someone will review your PR as soon as the [WIP] is removed.
  • Please familiarize yourself with the PR Review Checklist and feel free to make updates on your PR based on these guidelines.

PR Review Checklist

If any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'.

User Experience

  • The feature/bugfix is self-documenting from within the product.
  • The change provides the end user with a way to fix their problem (no dead ends).

Functionality

  • The code works and performs its intended function with the correct logic.
  • Performance has been factored in (for example, the code cleans up after itself to not cause memory leaks).
  • The code guards against edge cases and invalid input and has tests to cover it.

Maintainability

  • The code is readable (too many nested 'if's are a bad sign).
  • Names used for variables, methods, etc, clearly describe their function.
  • The code is easy to understood and there are relevant comments explaining.
  • New algorithms are documented in the code with link(s) to external docs (flowcharts, w3c, chrome, firefox).
  • There are comments containing link(s) to the addressed issue (in tests and code).

Quality

  • The change does not reimplement code.
  • There's not a module from the ecosystem that should be used instead.
  • There is no redundant or duplicate code.
  • There are no irrelevant comments left in the code.
  • Tests are testing the code’s intended functionality in the best way possible.

Internal

  • The original issue has been tagged with a release in ZenHub.

Copy link
Member

@jennifer-shehane jennifer-shehane left a comment

Choose a reason for hiding this comment

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

@OliverJAsh There's an error in the TS Lint, additionally, can you add a test for this? In the appropriate file under cli/types/tests

Error: /root/cypress/cli/types/index.d.ts:1967:82
ERROR: 1967:82  expect  Compile error in [email protected] but not in [email protected].
Fix with a comment '// TypeScript Version: 3.4' just under the header.
Cannot find name 'globalThis'.

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

@karol-majewski
Copy link

@OliverJAsh Regarding the issue we talked about — here's a suggestion for how we can really get the right Window yielded by cy.window().

If my suggestion falls outside the scope of this pull request, please let me know — I can open a separate issue or a pull request instead.

Problem

When you run a test with Cypress, there are two Window instances in play.

  1. The top Window object in which the Cypress UI lives, and
  2. The Window that represents the tested application.

These are different Windows, but they share the same definition. If you define additional properties by using declaration merging, they will appear on both, which in most cases is incorrect and leads to bugs.

Solution

Allow users to define global variables separately.

In cli/types/index.d.ts, define an empty interface called ApplicationWindow.

interface ApplicationWindow {}

Change the definition for cy.window so that the yielded object includes the properties defined by ApplicationWindow.

window(options?: Partial<Loggable & Timeoutable>): Chainable<Window & typeof globalThis & ApplicationWindow>

Usage

The users can now define properties that exist on the yielded window, but not on the window accessed directly inside a Cypress test.

declare global {
  namespace Cypress {
    interface ApplicationWindow {
      __FOO__?: string;
    }
  }
}

image

@@ -4,7 +4,7 @@
// Mike Woudenberg <https://github.com/mikewoudenberg>
// Robbert van Markus <https://github.com/rvanmarkus>
// Nicholas Boll <https://github.com/nicholasboll>
// TypeScript Version: 2.9
// TypeScript Version: 3.4
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a spooky/major change? Are we sure this isn't going to impact folks downstream maybe using earlier 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.

It's needed to support globalThis. It may well affect your users, so you might want to put this PR on hold until more of your users are on 3.4 or above.

Copy link
Member

Choose a reason for hiding this comment

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

We've run into this before, so this is not the first time this has been suggested. #4950 (review)

Copy link
Contributor

@CypressJoseph CypressJoseph Mar 18, 2020

Choose a reason for hiding this comment

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

Since we just accepted a pull to move to 3.0, I'm guessing that's going to be a conflict -- curious if these changes work at that level (3.0)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

3.4 is the minimum version if we want to use globalThis

@jennifer-shehane jennifer-shehane removed their request for review March 13, 2020 09:33
@jennifer-shehane jennifer-shehane dismissed their stale review March 13, 2020 09:33

Dismissing my previous reviews to allow for others to review actual code changes.

@jennifer-shehane
Copy link
Member

@OliverJAsh Can you fix the conflicts? We just made some TS changes that we merged in.

@OliverJAsh
Copy link
Contributor Author

@jennifer-shehane Done

@jennifer-shehane jennifer-shehane removed their request for review April 10, 2020 08:00
@jennifer-shehane jennifer-shehane requested review from bahmutov and removed request for CypressJoseph April 23, 2020 08:20
@jennifer-shehane jennifer-shehane requested a review from sainthkh May 6, 2020 08:13
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.

According to the TypeScript breaking changes list, some vendor-specific types are removed in TypeScript 3.1.

I think it's a better idea to apply this PR to Cypress 5.0. It can potentially break users' types.

@jennifer-shehane jennifer-shehane added the type: breaking change Requires a new major release version label May 20, 2020
@jennifer-shehane jennifer-shehane changed the base branch from develop to v5.0-release June 23, 2020 09:27
@jennifer-shehane
Copy link
Member

We're looking to release 5.0, with breaking changes. Is it possible to reevaluate this PR?

@sainthkh
Copy link
Contributor

@jennifer-shehane I will.

@sainthkh
Copy link
Contributor

Close in favor of #7806

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: breaking change Requires a new major release version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants