Skip to content

Commit 9e754d5

Browse files
sainthkhOliverJAsh
andauthored
TS: separate Window type for application under test (#7806)
Co-authored-by: Oliver Joseph Ash <[email protected]>
1 parent 2e35094 commit 9e754d5

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

cli/types/cypress.d.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ declare namespace Cypress {
107107
fromAutWindow: WindowPosition & { x: number, y: number }
108108
}
109109

110+
/**
111+
* Window type for Application Under Test(AUT)
112+
*/
113+
type AUTWindow = Window & typeof globalThis & ApplicationWindow
114+
115+
/**
116+
* The interface for user-defined properties in Window object under test.
117+
*/
118+
interface ApplicationWindow {} // tslint:disable-line
119+
110120
/**
111121
* Several libraries are bundled with Cypress by default.
112122
*
@@ -1038,7 +1048,7 @@ declare namespace Cypress {
10381048
*
10391049
* @see https://on.cypress.io/go
10401050
*/
1041-
go(direction: HistoryDirection | number, options?: Partial<Loggable & Timeoutable>): Chainable<Window>
1051+
go(direction: HistoryDirection | number, options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>
10421052

10431053
/**
10441054
* Get the current URL hash of the page that is currently active.
@@ -1375,7 +1385,7 @@ declare namespace Cypress {
13751385
* @example
13761386
* cy.reload()
13771387
*/
1378-
reload(options?: Partial<Loggable & Timeoutable>): Chainable<Window>
1388+
reload(options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>
13791389
/**
13801390
* Reload the page without cache
13811391
*
@@ -1386,7 +1396,7 @@ declare namespace Cypress {
13861396
* cy.visit('http://localhost:3000/admin')
13871397
* cy.reload(true)
13881398
*/
1389-
reload(forceReload: boolean): Chainable<Window>
1399+
reload(forceReload: boolean): Chainable<AUTWindow>
13901400

13911401
/**
13921402
* Make an HTTP GET request.
@@ -1929,8 +1939,8 @@ declare namespace Cypress {
19291939
* })
19301940
*
19311941
*/
1932-
visit(url: string, options?: Partial<VisitOptions>): Chainable<Window>
1933-
visit(options: Partial<VisitOptions> & { url: string }): Chainable<Window>
1942+
visit(url: string, options?: Partial<VisitOptions>): Chainable<AUTWindow>
1943+
visit(options: Partial<VisitOptions> & { url: string }): Chainable<AUTWindow>
19341944

19351945
/**
19361946
* Wait for a number of milliseconds.
@@ -2001,7 +2011,7 @@ declare namespace Cypress {
20012011
})
20022012
```
20032013
*/
2004-
window(options?: Partial<Loggable & Timeoutable>): Chainable<Window>
2014+
window(options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>
20052015

20062016
/**
20072017
* Scopes all subsequent cy commands to within this element.
@@ -2715,16 +2725,16 @@ declare namespace Cypress {
27152725
/**
27162726
* Called before your page has loaded all of its resources.
27172727
*
2718-
* @param {Window} contentWindow the remote page's window object
2728+
* @param {AUTWindow} contentWindow the remote page's window object
27192729
*/
2720-
onBeforeLoad(win: Window): void
2730+
onBeforeLoad(win: AUTWindow): void
27212731

27222732
/**
27232733
* Called once your page has fired its load event.
27242734
*
2725-
* @param {Window} contentWindow the remote page's window object
2735+
* @param {AUTWindow} contentWindow the remote page's window object
27262736
*/
2727-
onLoad(win: Window): void
2737+
onLoad(win: AUTWindow): void
27282738

27292739
/**
27302740
* Cypress will automatically apply the right authorization headers
@@ -4632,12 +4642,12 @@ declare namespace Cypress {
46324642
* 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.
46334643
* @see https://on.cypress.io/catalog-of-events#App-Events
46344644
*/
4635-
(action: 'window:before:load', fn: (win: Window) => void): void
4645+
(action: 'window:before:load', fn: (win: AUTWindow) => void): void
46364646
/**
46374647
* 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.
46384648
* @see https://on.cypress.io/catalog-of-events#App-Events
46394649
*/
4640-
(action: 'window:load', fn: (win: Window) => void): void
4650+
(action: 'window:load', fn: (win: AUTWindow) => void): void
46414651
/**
46424652
* 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.
46434653
* @see https://on.cypress.io/catalog-of-events#App-Events

cli/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Mike Woudenberg <https://github.com/mikewoudenberg>
55
// Robbert van Markus <https://github.com/rvanmarkus>
66
// Nicholas Boll <https://github.com/nicholasboll>
7-
// TypeScript Version: 3.0
7+
// TypeScript Version: 3.4
88
// Updated by the Cypress team: https://www.cypress.io/about/
99

1010
/// <reference path="./cy-blob-util.d.ts" />

cli/types/tests/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Cypress.on('window:alert', (text) => {
1212
})
1313

1414
Cypress.on('window:before:load', (win) => {
15-
win // $ExpectType Window
15+
win // $ExpectType AUTWindow
1616
})
1717

1818
Cypress.on('window:load', (win) => {
19-
win // $ExpectType Window
19+
win // $ExpectType AUTWindow
2020
})
2121

2222
Cypress.on('window:before:unload', (event) => {

cli/types/tests/cypress-tests.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,24 @@ cy
287287
subject // $ExpectType undefined
288288
})
289289

290+
namespace CypressAUTWindowTests {
291+
cy.go(2).then((win) => {
292+
win // $ExpectType AUTWindow
293+
})
294+
295+
cy.reload().then((win) => {
296+
win // $ExpectType AUTWindow
297+
})
298+
299+
cy.visit('https://google.com').then(win => {
300+
win // $ExpectType AUTWindow
301+
})
302+
303+
cy.window().then(win => {
304+
win // $ExpectType AUTWindow
305+
})
306+
}
307+
290308
namespace CypressOnTests {
291309
Cypress.on('uncaught:exception', (error, runnable) => {
292310
error // $ExpectType Error

cli/types/tests/kitchen-sink.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,9 @@ namespace BlobTests {
142142
dateUrl // $ExpectType string
143143
})
144144
}
145+
146+
cy.window().then(window => {
147+
window // $ExpectType AUTWindow
148+
149+
window.eval('1')
150+
})

0 commit comments

Comments
 (0)