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

TS: separate Window type for application under test #7806

Merged
merged 11 commits into from Jul 6, 2020
34 changes: 22 additions & 12 deletions cli/types/cypress.d.ts
Expand Up @@ -107,6 +107,16 @@ declare namespace Cypress {
fromAutWindow: WindowPosition & { x: number, y: number }
}

/**
* Window type for Application Under Test(AUT)
*/
type AUTWindow = Window & typeof globalThis & ApplicationWindow

/**
* The interface for user-defined properties in Window object under test.
*/
interface ApplicationWindow {} // tslint:disable-line
bahmutov marked this conversation as resolved.
Show resolved Hide resolved

/**
* Several libraries are bundled with Cypress by default.
*
Expand Down Expand Up @@ -1038,7 +1048,7 @@ declare namespace Cypress {
*
* @see https://on.cypress.io/go
*/
go(direction: HistoryDirection | number, options?: Partial<Loggable & Timeoutable>): Chainable<Window>
go(direction: HistoryDirection | number, options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>

/**
* Get the current URL hash of the page that is currently active.
Expand Down Expand Up @@ -1375,7 +1385,7 @@ declare namespace Cypress {
* @example
* cy.reload()
*/
reload(options?: Partial<Loggable & Timeoutable>): Chainable<Window>
reload(options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>
/**
* Reload the page without cache
*
Expand All @@ -1386,7 +1396,7 @@ declare namespace Cypress {
* cy.visit('http://localhost:3000/admin')
* cy.reload(true)
*/
reload(forceReload: boolean): Chainable<Window>
reload(forceReload: boolean): Chainable<AUTWindow>

/**
* Make an HTTP GET request.
Expand Down Expand Up @@ -1929,8 +1939,8 @@ declare namespace Cypress {
* })
*
*/
visit(url: string, options?: Partial<VisitOptions>): Chainable<Window>
visit(options: Partial<VisitOptions> & { url: string }): Chainable<Window>
visit(url: string, options?: Partial<VisitOptions>): Chainable<AUTWindow>
visit(options: Partial<VisitOptions> & { url: string }): Chainable<AUTWindow>

/**
* Wait for a number of milliseconds.
Expand Down Expand Up @@ -2001,7 +2011,7 @@ declare namespace Cypress {
})
```
*/
window(options?: Partial<Loggable & Timeoutable>): Chainable<Window>
window(options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>

/**
* Scopes all subsequent cy commands to within this element.
Expand Down Expand Up @@ -2715,16 +2725,16 @@ declare namespace Cypress {
/**
* Called before your page has loaded all of its resources.
*
* @param {Window} contentWindow the remote page's window object
* @param {AUTWindow} contentWindow the remote page's window object
*/
onBeforeLoad(win: Window): void
onBeforeLoad(win: AUTWindow): void

/**
* Called once your page has fired its load event.
*
* @param {Window} contentWindow the remote page's window object
* @param {AUTWindow} contentWindow the remote page's window object
*/
onLoad(win: Window): void
onLoad(win: AUTWindow): void

/**
* Cypress will automatically apply the right authorization headers
Expand Down Expand Up @@ -4632,12 +4642,12 @@ declare namespace Cypress {
* Fires as the page begins to load, but before any of your applications JavaScript has executed. This fires at the exact same time as `cy.visit()` `onBeforeLoad` callback. Useful to modify the window on a page transition.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:before:load', fn: (win: Window) => void): void
(action: 'window:before:load', fn: (win: AUTWindow) => void): void
/**
* Fires after all your resources have finished loading after a page transition. This fires at the exact same time as a `cy.visit()` `onLoad` callback.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:load', fn: (win: Window) => void): void
(action: 'window:load', fn: (win: AUTWindow) => void): void
/**
* Fires when your application is about to navigate away. The real event object is provided to you. Your app may have set a `returnValue` on the event, which is useful to assert on.
* @see https://on.cypress.io/catalog-of-events#App-Events
Expand Down
2 changes: 1 addition & 1 deletion cli/types/index.d.ts
Expand Up @@ -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: 3.0
// TypeScript Version: 3.4
// Updated by the Cypress team: https://www.cypress.io/about/

/// <reference path="./cy-blob-util.d.ts" />
Expand Down
4 changes: 2 additions & 2 deletions cli/types/tests/actions.ts
Expand Up @@ -12,11 +12,11 @@ Cypress.on('window:alert', (text) => {
})

Cypress.on('window:before:load', (win) => {
win // $ExpectType Window
win // $ExpectType AUTWindow
})

Cypress.on('window:load', (win) => {
win // $ExpectType Window
win // $ExpectType AUTWindow
})

Cypress.on('window:before:unload', (event) => {
Expand Down
18 changes: 18 additions & 0 deletions cli/types/tests/cypress-tests.ts
Expand Up @@ -287,6 +287,24 @@ cy
subject // $ExpectType undefined
})

namespace CypressAUTWindowTests {
cy.go(2).then((win) => {
win // $ExpectType AUTWindow
})

cy.reload().then((win) => {
win // $ExpectType AUTWindow
})

cy.visit('https://google.com').then(win => {
win // $ExpectType AUTWindow
})

cy.window().then(win => {
win // $ExpectType AUTWindow
})
}

namespace CypressOnTests {
Cypress.on('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
Expand Down
6 changes: 6 additions & 0 deletions cli/types/tests/kitchen-sink.ts
Expand Up @@ -142,3 +142,9 @@ namespace BlobTests {
dateUrl // $ExpectType string
})
}

cy.window().then(window => {
window // $ExpectType AUTWindow

window.eval('1')
})