forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cypress-tests.ts
550 lines (473 loc) · 15.8 KB
/
cypress-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
namespace CypressLodashTests {
Cypress._ // $ExpectType LoDashStatic
Cypress._.each([1], item => {
item // $ExpectType number
})
}
namespace CypressMomentTests {
Cypress.moment() // $ExpectType Moment
Cypress.moment('1982-08-23') // $ExpectType Moment
Cypress.moment(Date()) // $ExpectType Moment
Cypress.moment(Date()).format() // $ExpectType string
Cypress.moment().startOf('week') // $ExpectType Moment
}
namespace CypressSinonTests {
Cypress.sinon // $ExpectType SinonStatic
const stub = cy.stub()
stub(2, 'foo')
expect(stub).to.have.been.calledWith(Cypress.sinon.match.number, Cypress.sinon.match('foo'))
const stub2 = Cypress.sinon.stub()
stub2(2, 'foo')
expect(stub2).to.have.been.calledWith(Cypress.sinon.match.number, Cypress.sinon.match('foo'))
}
namespace CypressJqueryTests {
Cypress.$ // $ExpectType JQueryStatic
Cypress.$('selector') // $ExpectType JQuery<HTMLElement>
Cypress.$('selector').click() // $ExpectType JQuery<HTMLElement>
}
namespace CypressAutomationTests {
Cypress.automation('hello') // $ExpectType Promise<any>
}
namespace CypressConfigTests {
// getters
Cypress.config('baseUrl') // $ExpectType string | null
Cypress.config().baseUrl // $ExpectType string | null
// setters
Cypress.config('baseUrl', '.') // $ExpectType void
Cypress.config('baseUrl', null) // $ExpectType void
Cypress.config({ baseUrl: '.', }) // $ExpectType void
Cypress.config('taskTimeout') // $ExpectType number
}
namespace CypressEnvTests {
// Just making sure these are valid - no real type safety
Cypress.env('foo')
Cypress.env('foo', 'bar')
Cypress.env().foo
Cypress.env({
foo: 'bar'
})
}
namespace CypressIsCyTests {
Cypress.isCy(cy) // $ExpectType boolean
Cypress.isCy(undefined) // $ExpectType boolean
const chainer = cy.wrap("foo").then(function() {
if (Cypress.isCy(chainer)) {
chainer // $ExpectType Chainable<string>
}
})
}
namespace CypressCommandsTests {
Cypress.Commands.add('newCommand', () => {
return
})
Cypress.Commands.add('newCommand', { prevSubject: true }, () => {
return
})
Cypress.Commands.overwrite('newCommand', () => {
return
})
}
namespace CypressLogsTest {
const log = Cypress.log({
$el: Cypress.$('body'),
name: 'MyCommand',
displayName: 'My Command',
message: ['foo', 'bar'],
consoleProps: () => {
return {
foo: 'bar',
}
},
})
.set('$el', Cypress.$('body'))
.set({ name: 'MyCommand' })
.snapshot()
.snapshot('before')
.snapshot('before', { next: 'after' })
log.get() // $ExpectType LogConfig
log.get('name') // $ExpectType string
log.get('$el') // $ExpectType JQuery<HTMLElement>
}
namespace CypressLocalStorageTest {
Cypress.LocalStorage.clear = function(keys) {
keys // $ExpectType string[] | undefined
}
}
namespace CypressItsTests {
cy.wrap({ foo: [1, 2, 3] })
.its('foo')
.each((s: number) => {
s
})
cy.wrap({foo: 'bar'}).its('foo') // $ExpectType Chainable<string>
cy.wrap([1, 2]).its(1) // $ExpectType Chainable<number>
cy.wrap(['foo', 'bar']).its(1) // $ExpectType Chainable<string>
.then((s: string) => {
s
})
cy.wrap({baz: { quux: '2' }}).its('baz.quux') // $ExpectType Chainable<any>
}
namespace CypressInvokeTests {
const returnsString = () => 'foo'
const returnsNumber = () => 42
cy.wrap({ a: returnsString }).invoke('a') // $ExpectType Chainable<string>
cy.wrap({ b: returnsNumber }).invoke('b') // $ExpectType Chainable<number>
cy.wrap({ b: returnsNumber }).invoke({ log: true }, 'b') // $ExpectType Chainable<number>
// challenging to define a more precise return type than string | number here
cy.wrap([returnsString, returnsNumber]).invoke(1) // $ExpectType Chainable<string | number>
// invoke through property path results in any
cy.wrap({ a: { fn: (x: number) => x * x }}).invoke('a.fn', 4) // $ExpectType Chainable<any>
// examples below are from previous attempt at typing `invoke`
// (see https://github.com/cypress-io/cypress/issues/4022)
// call methods on arbitrary objects with reasonable return types
cy.wrap({ fn: () => ({a: 1})}).invoke("fn") // $ExpectType Chainable<{ a: number; }>
// call methods on dom elements with reasonable return types
cy.get('.trigger-input-range').invoke('val', 25) // $ExpectType Chainable<string | number | string[] | undefined>
}
cy.wrap({ foo: ['bar', 'baz'] })
.its('foo')
.then(([first, second]) => {
first // $ExpectType string
})
.spread((first: string, second: string) => {
first // $ExpectType string
// return first as string
})
.each((s: string) => {
s // $ExpectType string
})
.then(s => {
s // $ExpectType string[]
})
cy.get('.someSelector')
.each(($el, index, list) => {
$el // $ExpectType JQuery<HTMLElement>
index // $ExpectType number
list // $ExpectType HTMLElement[]
})
cy.wrap(['bar', 'baz'])
.spread((first, second) => {
first // $ExpectType any
})
describe('then', () => {
it('should have the correct type signature', () => {
cy.wrap({ foo: 'bar' })
.then(s => {
s // $ExpectType { foo: string; }
return s
})
.then(s => {
s // $ExpectType { foo: string; }
})
.then(s => s.foo)
.then(s => {
s // $ExpectType string
})
})
it('should have the correct type signature with options', () => {
cy.wrap({ foo: 'bar' })
.then({ timeout: 5000 }, s => {
s // $ExpectType { foo: string; }
return s
})
.then({ timeout: 5000 }, s => {
s // $ExpectType { foo: string; }
})
.then({ timeout: 5000 }, s => s.foo)
.then({ timeout: 5000 }, s => {
s // $ExpectType string
})
})
})
cy.wait(['@foo', '@bar'])
.then(([first, second]) => {
first // $ExpectType WaitXHR
})
cy.wait(1234) // $ExpectType Chainable<undefined>
cy.wrap('foo').wait(1234) // $ExpectType Chainable<string>
cy.wrap([{ foo: 'bar' }, { foo: 'baz' }])
.then(subject => {
subject // $ExpectType { foo: string; }[]
})
.then(([first, second]) => {
first // $ExpectType { foo: string; }
})
.then(subject => {
subject // $ExpectType { foo: string; }[]
})
.then(([first, second]) => {
return first.foo + second.foo
})
.then(subject => {
subject // $ExpectType string
})
cy.wrap([1, 2, 3]).each((num: number, i, array) => {
return new Cypress.Promise((resolve) => {
setTimeout(() => {
resolve()
}, num * 100)
})
})
cy.get('something').should('have.length', 1)
cy.stub().withArgs('').log(false).as('foo')
cy.spy().withArgs('').log(false).as('foo')
cy.wrap('foo').then(subject => {
subject // $ExpectType string
return cy.wrap(subject)
}).then(subject => {
subject // $ExpectType string
})
cy.wrap('foo').then(subject => {
subject // $ExpectType string
return Cypress.Promise.resolve(subject)
}).then(subject => {
subject // $ExpectType string
})
cy.get('body').within(body => {
body // $ExpectType JQuery<HTMLBodyElement>
})
cy.get('body').within({ log: false }, body => {
body // $ExpectType JQuery<HTMLBodyElement>
})
cy.get('body').within(() => {
cy.get('body', { withinSubject: null }).then(body => {
body // $ExpectType JQuery<HTMLBodyElement>
})
})
cy
.get('body')
.then(() => {
return cy.wrap(undefined)
})
.then(subject => {
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
runnable // $ExpectType IRunnable
})
cy.on('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
runnable // $ExpectType IRunnable
})
}
namespace CypressOnceTests {
Cypress.once('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
runnable // $ExpectType IRunnable
})
cy.once('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
runnable // $ExpectType IRunnable
})
}
namespace CypressOffTests {
Cypress.off('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
runnable // $ExpectType IRunnable
})
cy.off('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
runnable // $ExpectType IRunnable
})
}
namespace CypressFilterTests {
cy.get<HTMLDivElement>('#id')
.filter((index: number, element: HTMLDivElement) => {
index // $ExpectType number
element // $ExpectType HTMLDivElement
return true
})
}
namespace CypressScreenshotTests {
cy.screenshot('example-name')
cy.screenshot('example', { log: false })
cy.screenshot({ log: false })
cy.screenshot({
log: true,
blackout: []
})
cy.screenshot('example', {
log: true,
blackout: []
})
}
namespace CypressTriggerTests {
cy.get('something')
.trigger('click') // .trigger(eventName)
.trigger('click', 'center') // .trigger(eventName, position)
.trigger('click', { // .trigger(eventName, options)
arbitraryProperty: 0
})
.trigger('click', 0, 0) // .trigger(eventName, x, y)
.trigger('click', 'center', { // .trigger(eventName, position, options)
arbitraryProperty: 0
})
.trigger('click', 0, 0, { // .trigger(eventName, x, y, options)
arbitraryProperty: 0
})
}
const now = new Date(2019, 3, 2).getTime()
cy.clock(now, ['Date'])
namespace CypressContainsTests {
cy.contains('#app')
cy.contains('my text to find')
cy.contains('#app', 'my text to find')
cy.contains('#app', 'my text to find', {log: false, timeout: 100})
cy.contains('my text to find', {log: false, timeout: 100})
}
// https://github.com/cypress-io/cypress/pull/5574
namespace CypressLocationTests {
cy.location('path') // $ExpectError
cy.location('pathname') // $ExpectType Chainable<string>
}
namespace CypressBrowserTests {
Cypress.isBrowser('chrome')// $ExpectType boolean
Cypress.isBrowser('firefox')// $ExpectType boolean
Cypress.isBrowser('edge')// $ExpectType boolean
Cypress.isBrowser('brave')// $ExpectType boolean
// does not error to allow for user supplied browsers
Cypress.isBrowser('safari')// $ExpectType boolean
Cypress.isBrowser({channel: 'stable'})// $ExpectType boolean
Cypress.isBrowser({family: 'chromium'})// $ExpectType boolean
Cypress.isBrowser({name: 'chrome'})// $ExpectType boolean
Cypress.isBrowser({family: 'foo'}) // $ExpectError
Cypress.isBrowser() // $ExpectError
}
namespace CypressDomTests {
const obj: any = {}
const el = {} as any as HTMLElement
const jel = {} as any as JQuery
const doc = {} as any as Document
Cypress.dom.wrap((x: number) => 'a') // $ExpectType JQuery<HTMLElement>
Cypress.dom.query('foo', el) // $ExpectType JQuery<HTMLElement>
Cypress.dom.unwrap(obj) // $ExpectType any
Cypress.dom.isDom(obj) // $ExpectType boolean
Cypress.dom.isType(el, 'foo') // $ExpectType boolean
Cypress.dom.isVisible(el) // $ExpectType boolean
Cypress.dom.isHidden(el) // $ExpectType boolean
Cypress.dom.isFocusable(el) // $ExpectType boolean
Cypress.dom.isTextLike(el) // $ExpectType boolean
Cypress.dom.isScrollable(el) // $ExpectType boolean
Cypress.dom.isFocused(el) // $ExpectType boolean
Cypress.dom.isDetached(el) // $ExpectType boolean
Cypress.dom.isAttached(el) // $ExpectType boolean
Cypress.dom.isSelector(el, 'foo') // $ExpectType boolean
Cypress.dom.isDescendent(el, el) // $ExpectType boolean
Cypress.dom.isElement(obj) // $ExpectType boolean
Cypress.dom.isDocument(obj) // $ExpectType boolean
Cypress.dom.isWindow(obj) // $ExpectType boolean
Cypress.dom.isJquery(obj) // $ExpectType boolean
Cypress.dom.isInputType(el, 'number') // $ExpectType boolean
Cypress.dom.stringify(el, 'foo') // $ExpectType string
Cypress.dom.getElements(jel) // $ExpectType JQuery<HTMLElement> | HTMLElement[]
Cypress.dom.getContainsSelector('foo', 'bar') // $ExpectType string
Cypress.dom.getFirstDeepestElement([el], 1) // $ExpectType HTMLElement
Cypress.dom.getWindowByElement(el) // $ExpectType HTMLElement | JQuery<HTMLElement>
Cypress.dom.getReasonIsHidden(el) // $ExpectType string
Cypress.dom.getFirstScrollableParent(el) // $ExpectType HTMLElement | JQuery<HTMLElement>
Cypress.dom.getFirstFixedOrStickyPositionParent(el) // $ExpectType HTMLElement | JQuery<HTMLElement>
Cypress.dom.getFirstStickyPositionParent(el) // $ExpectType HTMLElement | JQuery<HTMLElement>
Cypress.dom.getCoordsByPosition(1, 2) // $ExpectType number
Cypress.dom.getElementPositioning(el) // $ExpectType ElementPositioning
Cypress.dom.getElementAtPointFromViewport(doc, 1, 2) // $ExpectType Element | null
Cypress.dom.getElementCoordinatesByPosition(el, 'top') // $ExpectType ElementCoordinates
Cypress.dom.getElementCoordinatesByPositionRelativeToXY(el, 1, 2) // $ExpectType ElementPositioning
Cypress.dom.wrap() // $ExpectError
Cypress.dom.query(el, 'foo') // $ExpectError
Cypress.dom.unwrap() // $ExpectError
Cypress.dom.isDom() // $ExpectError
Cypress.dom.isType(el) // $ExpectError
Cypress.dom.isVisible('') // $ExpectError
Cypress.dom.isHidden('') // $ExpectError
Cypress.dom.isFocusable('') // $ExpectError
Cypress.dom.isTextLike('') // $ExpectError
Cypress.dom.isScrollable('') // $ExpectError
Cypress.dom.isFocused('') // $ExpectError
Cypress.dom.isDetached('') // $ExpectError
Cypress.dom.isAttached('') // $ExpectError
Cypress.dom.isSelector('', 'foo') // $ExpectError
Cypress.dom.isDescendent('', '') // $ExpectError
Cypress.dom.isElement() // $ExpectError
Cypress.dom.isDocument() // $ExpectError
Cypress.dom.isWindow() // $ExpectError
Cypress.dom.isJquery() // $ExpectError
Cypress.dom.isInputType('', 'number') // $ExpectError
Cypress.dom.stringify('', 'foo') // $ExpectError
Cypress.dom.getElements(el) // $ExpectError
Cypress.dom.getContainsSelector(el, 'bar') // $ExpectError
Cypress.dom.getFirstDeepestElement(el, 1) // $ExpectError
Cypress.dom.getWindowByElement('') // $ExpectError
Cypress.dom.getReasonIsHidden('') // $ExpectError
Cypress.dom.getFirstScrollableParent('') // $ExpectError
Cypress.dom.getFirstFixedOrStickyPositionParent('') // $ExpectError
Cypress.dom.getFirstStickyPositionParent('') // $ExpectError
Cypress.dom.getCoordsByPosition(1) // $ExpectError
Cypress.dom.getElementPositioning('') // $ExpectError
Cypress.dom.getElementAtPointFromViewport(el, 1, 2) // $ExpectError
Cypress.dom.getElementCoordinatesByPosition(doc, 'top') // $ExpectError
Cypress.dom.getElementCoordinatesByPositionRelativeToXY(doc, 1, 2) // $ExpectError
}
namespace CypressTestConfigOverridesTests {
// set config on a per-test basis
it('test', {
browser: {name: 'firefox'}
}, () => {})
it('test', {
browser: [{name: 'firefox'}, {name: 'chrome'}]
}, () => {})
it('test', {
baseUrl: 'www.foobar.com',
browser: 'firefox'
}, () => {})
it('test', {
browser: {foo: 'bar'} // $ExpectError
}, () => {})
it.skip('test', {}, () => {})
it.only('test', {}, () => {})
xit('test', {}, () => {})
specify('test', {}, () => {})
specify.only('test', {}, () => {})
specify.skip('test', {}, () => {})
xspecify('test', {}, () => {})
// set config on a per-suite basis
describe('suite', {
browser: {family: 'firefox'},
baseUrl: 'www.example.com'
}, () => {})
context('suite', {}, () => {})
describe('suite', {
browser: {family: 'firefox'},
baseUrl: 'www.example.com'
foo: 'foo' // $ExpectError
}, () => {})
describe.only('suite', {}, () => {})
describe.skip('suite', {}, () => {})
xdescribe('suite', {}, () => {})
}
namespace CypressShadowTests {
cy
.get('.foo')
.shadow()
.find('.bar')
.click()
cy.get('.foo', { includeShadowDom: true }).click()
cy
.get('.foo')
.find('.bar', {includeShadowDom: true})
}