-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
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.
- The top
Windowobject in which the Cypress UI lives, and - The
Windowthat 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;
}
}
}Originally posted by @karol-majewski in #6624 (comment)
Desired behavior:
- Cypress
Windowdefinition should includeglobalThisdefinitions. - The type of Cypress
ApplicationWindowcan be extended.
Test code to reproduce
Versions
Cypress 4.9.0.
