/
kitchen-sink.ts
144 lines (122 loc) · 3.54 KB
/
kitchen-sink.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
// additional tests that confirm types are done correctly
// these tests are in addition to type checking cypress-example-kitchensink
// (https://github.com/cypress-io/cypress-example-kitchensink)
// and should not repeat them
// extra code that is not in the kitchensink that type checks edge cases
cy.wrap('foo').then(subject => {
subject // $ExpectType string
return cy.wrap(subject)
}).then(subject => {
subject // $ExpectType string
})
const result = Cypress.minimatch('/users/1/comments', '/users/*/comments', {
matchBase: true,
})
result // $ExpectType boolean
Cypress.minimatch('/users/1/comments', '/users/*/comments') // $ExpectType boolean
// check if cy.server() yields default server options
cy.server().should((server) => {
server // $ExpectType ServerOptions
expect(server.delay).to.eq(0)
expect(server.method).to.eq('GET')
expect(server.status).to.eq(200)
})
cy.visit('https://www.acme.com/', {
auth: {
username: 'wile',
password: 'coyote'
}
})
const serverOptions: Partial<Cypress.ServerOptions> = {
delay: 100,
ignore: () => true
}
cy.server(serverOptions)
Cypress.spec.name // $ExpectType string
Cypress.spec.relative // $ExpectType string
Cypress.spec.absolute // $ExpectType string
Cypress.browser // $ExpectType Browser
// stubbing window.alert type on "Cypress" should
// work with plain function or with a Sinon stub
Cypress.on('window:alert', () => { })
Cypress.on('window:alert', cy.spy())
Cypress.on('window:alert', cy.stub())
// same for a single test
cy.on('window:alert', () => { })
cy.on('window:alert', cy.spy())
cy.on('window:alert', cy.stub())
// sinon-chai example
const stub = cy.stub()
expect(stub).to.not.have.been.called
stub()
expect(stub).to.have.been.calledOnce
cy.wrap(stub).should('have.been.calledOnce')
namespace EventInterfaceTests {
// window:confirm stubbing
Cypress.on('window:confirm', () => { })
Cypress.on('window:confirm', cy.spy())
Cypress.on('window:confirm', cy.stub())
cy.on('window:confirm', () => { })
cy.on('window:confirm', cy.spy())
cy.on('window:confirm', cy.stub())
Cypress.removeListener('fail', () => {})
Cypress.removeAllListeners('fail')
cy.removeListener('fail', () => {})
cy.removeAllListeners('fail')
}
// specifying HTTP method directly in the options object
cy.request({
url: "http://localhost:3000/myressource",
method: "POST",
body: {}
})
// specify query parameters
// https://github.com/cypress-io/cypress/issues/2305
cy.request({
url: "http://localhost:3000/myressource",
qs: {
param: 'someValue'
}
})
// if you want a separate variable, you need specify its type
// otherwise TSC does not cast string "POST" as HttpMethod
// https://github.com/cypress-io/cypress/issues/2093
const opts: Partial<Cypress.RequestOptions> = {
url: "http://localhost:3000/myressource",
method: "POST",
body: {}
}
cy.request(opts)
// you can cast just the "method" property
const opts2 = {
url: "http://localhost:3000/myressource",
method: "POST" as Cypress.HttpMethod,
body: {}
}
cy.request(opts2)
const obj = {
foo: () => { }
}
cy.spy(obj, 'foo').as('my-spy')
// use path-based access for nested structures
cy.wrap({
foo: {
bar: 1
}
}).its('foo.bar')
cy.wrap({
foo: {
quux: () => 2
}
}).invoke('foo.quux')
// different clearLocalStorage signatures
cy.clearLocalStorage()
cy.clearLocalStorage('todos')
cy.clearLocalStorage('todos', { log: false })
cy.clearLocalStorage({ log: false })
namespace BlobTests {
Cypress.Blob.imgSrcToDataURL('/some/path', undefined, 'anonymous')
.then((dateUrl) => {
dateUrl // $ExpectType string
})
}