-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
352 lines (318 loc) · 9.81 KB
/
index.js
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
const debug = require('debug')('cypress-if')
const isIfCommand = (cmd) =>
cmd && cmd.attributes && cmd.attributes.name === 'if'
const skipCommand = (cmd) => {
cmd.attributes.skip = true
cmd.state = 'skipped'
}
function skipRestOfTheChain(cmd, chainerId) {
while (
cmd &&
cmd.attributes.chainerId === chainerId &&
cmd.attributes.name !== 'finally'
) {
debug('skipping "%s"', cmd.attributes.name)
skipCommand(cmd)
cmd = cmd.attributes.next
}
}
function findMyIfSubject(elseCommandAttributes) {
if (!elseCommandAttributes) {
return
}
if (elseCommandAttributes.name === 'if') {
return elseCommandAttributes.ifSubject
}
if (
!elseCommandAttributes.skip &&
!Cypress._.isNil(elseCommandAttributes.subject)
) {
return elseCommandAttributes.subject
}
if (elseCommandAttributes.prev) {
return findMyIfSubject(elseCommandAttributes.prev.attributes)
}
}
function getCypressCurrentSubject() {
if (typeof cy.currentSubject === 'function') {
return cy.currentSubject()
}
// fallback for Cypress v9 and some early v10 versions
return cy.state('subject')
}
// cy.if command
Cypress.Commands.add(
'if',
{ prevSubject: true },
function (subject, assertion, assertionValue) {
const cmd = cy.state('current')
debug('if', cmd.attributes, 'subject', subject, 'assertion?', assertion)
debug('next command', cmd.next)
debug('if() current subject', getCypressCurrentSubject())
// console.log('subjects', cy.state('subjects'))
// keep the subject, if there is an "else" branch
// it can look it up to use
cmd.attributes.ifSubject = subject
// let's be friendly and if the user
// wrote "if exists" just go with it
if (assertion === 'exists') {
assertion = 'exist'
}
let hasSubject = Boolean(subject)
let assertionsPassed = true
const evaluateAssertion = () => {
try {
if (Cypress._.isFunction(assertion)) {
const result = assertion(subject)
if (Cypress._.isBoolean(result)) {
// function was a predicate
if (!result) {
throw new Error('Predicate function failed')
}
}
} else if (
assertion.startsWith('not') ||
assertion.startsWith('have')
) {
const parts = assertion.split('.')
let assertionReduced = expect(subject).to
parts.forEach((assertionPart, k) => {
if (
k === parts.length - 1 &&
typeof assertionValue !== 'undefined'
) {
assertionReduced = assertionReduced[assertionPart](assertionValue)
} else {
assertionReduced = assertionReduced[assertionPart]
}
})
} else {
if (typeof assertionValue !== 'undefined') {
expect(subject).to.be[assertion](assertionValue)
} else {
expect(subject).to.be[assertion]
}
}
} catch (e) {
console.error(e)
assertionsPassed = false
if (e.message.includes('Invalid Chai property')) {
throw e
}
}
}
// check if the previous command was cy.task
// and it has failed and it was expected
if (
assertion === 'failed' &&
Cypress._.get(cmd, 'attributes.prev.attributes.name') === 'task' &&
Cypress._.isError(Cypress._.get(cmd, 'attributes.prev.attributes.error'))
) {
debug('cy.task has failed and it was expected')
// set the subject and the assertions to take the IF branch
hasSubject = Cypress._.get(cmd, 'attributes.prev.attributes.error')
} else {
if (subject === null) {
if (assertion === 'null') {
hasSubject = true
assertionsPassed = true
} else if (assertion === 'not.null') {
hasSubject = true
assertionsPassed = false
}
} else if (hasSubject && assertion) {
evaluateAssertion()
} else if (subject === undefined && assertion) {
evaluateAssertion()
hasSubject = true
}
}
const chainerId = cmd.attributes.chainerId
if (!chainerId) {
throw new Error('Command is missing chainer id')
}
if (!hasSubject || !assertionsPassed) {
let nextCommand = cmd.attributes.next
while (nextCommand && nextCommand.attributes.chainerId === chainerId) {
debug(
'skipping the next "%s" command "%s"',
nextCommand.attributes.type,
nextCommand.attributes.name,
)
// cy.log(`**skipping ${cmd.attributes.next.attributes.name}**`)
if (nextCommand.attributes.name === 'else') {
debug('else branch starts right away')
nextCommand = null
} else {
debug('am skipping "%s"', nextCommand.attributes.name)
debug(nextCommand.attributes)
skipCommand(nextCommand)
nextCommand = nextCommand.attributes.next
if (nextCommand && nextCommand.attributes.name === 'else') {
debug('stop skipping command on "else" command')
nextCommand = null
}
}
}
if (subject) {
debug('wrapping subject', subject)
cy.wrap(subject, { log: false })
}
return
} else {
// skip possible "else" branch
debug('skipping a possible "else" branch')
let nextCommand = cmd.attributes.next
while (nextCommand && nextCommand.attributes.chainerId === chainerId) {
debug(
'next command "%s" type "%s"',
nextCommand.attributes.name,
nextCommand.attributes.type,
nextCommand.attributes,
)
if (nextCommand.attributes.name === 'else') {
// found the "else" command, start skipping
debug('found the "else" branch command start')
skipRestOfTheChain(nextCommand, chainerId)
nextCommand = null
} else {
nextCommand = nextCommand.attributes.next
}
}
}
return subject
},
)
Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
debug('else command, subject', subject)
if (typeof subject === 'undefined') {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
}
if (subject) {
cy.wrap(subject, { log: false })
}
})
Cypress.Commands.add('finally', { prevSubject: true }, (subject) => {
debug('finally with the subject', subject)
// notice: cy.log yields "null" 🤯
// https://github.com/cypress-io/cypress/issues/23400
if (typeof subject === 'undefined' || subject === null) {
// find the subject from the "if()" before
const currentCommand = cy.state('current').attributes
debug('current command is finally', currentCommand)
subject = findMyIfSubject(currentCommand)
debug('found subject', subject)
}
if (subject) {
cy.wrap(subject, { log: false })
}
})
Cypress.Commands.overwrite('get', function (get, selector, options) {
// can we see the next command already?
const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
if (isIfCommand(next)) {
if (selector.startsWith('@')) {
try {
return get(selector, options)
} catch (e) {
if (e.message.includes('could not find a registered alias for')) {
return undefined
}
}
}
// disable the built-in assertion
return get(selector, options).then(
(getResult) => {
debug('internal get result', getResult)
return getResult
},
(noResult) => {
debug('no get result', noResult)
},
)
}
return get(selector, options)
})
Cypress.Commands.overwrite(
'contains',
function (contains, prevSubject, selector, text, options) {
debug('cy.contains arguments number', arguments.length)
if (arguments.length === 3) {
text = selector
selector = undefined
}
debug('cy.contains args', { prevSubject, selector, text, options })
const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
if (next && next.attributes.name === 'if') {
// disable the built-in assertion
return contains(prevSubject, selector, text, options).then(
(getResult) => {
debug('internal contains result', getResult)
return getResult
},
(noResult) => {
debug('no contains result', noResult)
},
)
}
return contains(prevSubject, selector, text, options)
},
)
Cypress.Commands.overwrite(
'find',
function (find, prevSubject, selector, options) {
debug('cy.find args', { prevSubject, selector, options })
const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
if (next && next.attributes.name === 'if') {
// disable the built-in assertion
return find(prevSubject, selector, options).then(
(getResult) => {
debug('internal cy.find result', getResult)
return getResult
},
(noResult) => {
debug('no cy.find result', noResult)
},
)
}
return find(prevSubject, selector, options)
},
)
Cypress.Commands.overwrite('task', function (task, args, options) {
debug('cy.task %o', { args, options })
const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
if (next && next.attributes.name === 'if') {
// disable the built-in assertion
return task(args, options).then(
(taskResult) => {
debug('internal task result', taskResult)
return taskResult
},
(error) => {
debug('task error', error)
cmd.attributes.error = error
},
)
}
return task(args, options)
})
Cypress.Commands.add('raise', (x) => {
if (Cypress._.isError(x)) {
throw x
}
const e = new Error(
String(x) +
'\n' +
'cypress-if tip: pass an error instance to have correct stack',
)
throw e
})