Skip to content

Correct Window type. #7856

@sainthkh

Description

@sainthkh

Current behavior:

It's from #6624.

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.


It's from the comment in #6624

@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

Originally posted by @karol-majewski in #6624 (comment)

Desired behavior:

  • Cypress Window definition should include globalThis definitions.
  • The type of Cypress ApplicationWindow can be extended.

Test code to reproduce

Versions

Cypress 4.9.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: breaking changeRequires a new major release versiontype: typingsIssue related to Cypress types (for TypeScript)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions