-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
assertions.coffee
422 lines (331 loc) · 11.5 KB
/
assertions.coffee
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
_ = require("lodash")
Promise = require("bluebird")
$dom = require("../dom")
$utils = require("../cypress/utils")
## TODO
## bTagOpen + bTagClosed
## are duplicated in assertions.coffee
butRe = /,? but\b/
bTagOpen = /\*\*/g
bTagClosed = /\*\*/g
stackTracesRe = / at .*\n/gm
IS_DOM_TYPES = [$dom.isElement, $dom.isDocument, $dom.isWindow]
invokeWith = (value) ->
return (fn) ->
fn(value)
functionHadArguments = (current) ->
fn = current and current.get("args") and current.get("args")[0]
fn and _.isFunction(fn) and fn.length > 0
isAssertionType = (cmd) ->
cmd and cmd.is("assertion")
isDomSubjectAndMatchesValue = (value, subject) ->
allElsAreTheSame = ->
els1 = $dom.getElements(value)
els2 = $dom.getElements(subject)
## no difference
_.difference(els1, els2).length is 0
## iterate through each dom type until we
## find the function for this particular value
if isDomTypeFn = _.find(IS_DOM_TYPES, invokeWith(value))
## then check that subject also matches this
## and that all the els are the same
return isDomTypeFn(subject) and allElsAreTheSame()
## Rules:
## 1. always remove value
## 2. if value is a jquery object set a subject
## 3. if actual is undefined or its not expected remove both actual + expected
parseValueActualAndExpected = (value, actual, expected) ->
obj = {actual: actual, expected: expected}
if $dom.isJquery(value)
obj.subject = value
if _.isUndefined(actual) or actual isnt expected
delete obj.actual
delete obj.expected
obj
create = (Cypress, state, queue, retryFn) ->
getUpcomingAssertions = ->
current = state("current")
index = state("index") + 1
assertions = []
## grab the rest of the queue'd commands
for cmd in queue.slice(index).get()
## don't break on utilities, just skip over them
if cmd.is("utility")
continue
## grab all of the queued commands which are
## assertions and match our current chainerId
if cmd.is("assertion")
assertions.push(cmd)
else
break
assertions
injectAssertionFns = (cmds) ->
_.map(cmds, injectAssertion)
injectAssertion = (cmd) ->
return (subject) ->
## set assertions to itself or empty array
if not cmd.get("assertions")
cmd.set("assertions", [])
## reset the assertion index back to 0
## so we can track assertions and merge
## them up with existing ones
cmd.set("assertionIndex", 0)
cmd.get("fn").originalFn.apply(
state("ctx"),
[subject].concat(cmd.get("args"))
)
finishAssertions = (assertions) ->
_.each assertions, (log) ->
log.snapshot()
if e = log.get("_error")
log.error(e)
else
log.end()
verifyUpcomingAssertions = (subject, options = {}, callbacks = {}) ->
cmds = getUpcomingAssertions()
state("upcomingAssertions", cmds)
## we're applying the default assertion in the
## case where there are no upcoming assertion commands
isDefaultAssertionErr = cmds.length is 0
options.assertions ?= []
_.defaults callbacks, {
ensureExistenceFor: "dom"
}
ensureExistence = ->
## by default, ensure existence for dom subjects,
## but not non-dom subjects
switch callbacks.ensureExistenceFor
when "dom"
$el = determineEl(options.$el, subject)
return if not $dom.isJquery($el)
cy.ensureElExistence($el)
when "subject"
cy.ensureExistence(subject)
determineEl = ($el, subject) ->
## prefer $el unless it is strickly undefined
if not _.isUndefined($el) then $el else subject
onPassFn = =>
if _.isFunction(callbacks.onPass)
callbacks.onPass.call(@, cmds, options.assertions)
else
subject
onFailFn = (err) =>
## when we fail for whatever reason we need to
## check to see if we would firstly fail if
## we don't have an el in existence. what this
## catches are assertions downstream about an
## elements existence but the element never
## exists in the first place. this will probably
## ensure the error is about existence not about
## the downstream assertion.
try
ensureExistence()
catch e2
err = e2
options.error = err
if err.retry is false
throw err
onFail = callbacks.onFail
onRetry = callbacks.onRetry
if not onFail and not onRetry
throw err
## if our onFail throws then capture it
## and finish the assertions and then throw
## it again
try
if _.isFunction(onFail)
## pass in the err and the upcoming assertion commands
onFail.call(@, err, isDefaultAssertionErr, cmds)
catch e3
finishAssertions(options.assertions)
throw e3
if _.isFunction(onRetry)
retryFn(onRetry, options)
## bail if we have no assertions and apply
## the default assertions if applicable
if not cmds.length
return Promise
.try(ensureExistence)
.then(onPassFn)
.catch(onFailFn)
i = 0
cmdHasFunctionArg = (cmd) ->
_.isFunction(cmd.get("args")[0])
overrideAssert = (args...) ->
do (cmd = cmds[i]) =>
setCommandLog = (log) =>
## our next log may not be an assertion
## due to page events so make sure we wait
## until we see page events
return if log.get("name") isnt "assert"
## when we do immediately unbind this function
state("onBeforeLog", null)
insertNewLog = (log) ->
cmd.log(log)
options.assertions.push(log)
## its possible a single 'should' will assert multiple
## things such as the case with have.property. we can
## detect when that is happening because cmd will be null.
## if thats the case then just set cmd to be the previous
## cmd and do not increase 'i'
## this will prevent 2 logs from ever showing up but still
## provide errors when the 1st assertion fails.
if not cmd
cmd = cmds[i - 1]
else
i += 1
## if our command has a function argument
## then we know it may contain multiple
## assertions
if cmdHasFunctionArg(cmd)
index = cmd.get("assertionIndex")
assertions = cmd.get("assertions")
incrementIndex = ->
## always increase the assertionIndex
## so our next assertion matches up
## to the correct index
cmd.set("assertionIndex", index += 1)
## if we dont have an assertion at this
## index then insert a new log
if not assertion = assertions[index]
assertions.push(log)
incrementIndex()
return insertNewLog(log)
else
## else just merge this log
## into the previous assertion log
incrementIndex()
assertion.merge(log)
## dont output a new log
return false
## if we already have a log
## then just update its attrs from
## the new log
if l = cmd.getLastLog()
l.merge(log)
## and make sure we return false
## which prevents a new log from
## being added
return false
else
insertNewLog(log)
state("onBeforeLog", setCommandLog)
## send verify=true as the last arg
assertFn.apply(@, args.concat(true))
fns = injectAssertionFns(cmds)
subjects = []
## iterate through each subject
## and force the assertion to return
## this value so it does not get
## invoked again
setSubjectAndSkip = ->
for subject, i in subjects
cmd = cmds[i]
cmd.set("subject", subject)
cmd.skip()
assertions = (memo, fn, i) =>
## HACK: bluebird .reduce will not call the callback
## if given an undefined initial value, so in order to
## support undefined subjects, we wrap the initial value
## in an Array and unwrap it if index = 0
if i is 0
memo = memo[0]
fn(memo).then (subject) ->
subjects[i] = subject
restore = ->
state("upcomingAssertions", [])
## no matter what we need to
## restore the assert fn
state("overrideAssert", undefined)
## store this in case our test ends early
## and we reset between tests
state("overrideAssert", overrideAssert)
Promise
.reduce(fns, assertions, [subject])
.then ->
restore()
setSubjectAndSkip()
finishAssertions(options.assertions)
onPassFn()
.catch (err) ->
restore()
## when we're told not to retry
if err.retry is false
## finish the assertions
finishAssertions(options.assertions)
## and then push our command into this err
try
$utils.throwErr(err, { onFail: options._log })
catch e
err = e
throw err
.catch(onFailFn)
assertFn = (passed, message, value, actual, expected, error, verifying = false) ->
## slice off everything after a ', but' or ' but ' for passing assertions, because
## otherwise it doesn't make sense:
## "expected <div> to have a an attribute 'href', but it was 'href'"
if message and passed and butRe.test(message)
message = message.substring(0, message.search(butRe))
if value?.isSinonProxy
message = message.replace(stackTracesRe, "\n")
obj = parseValueActualAndExpected(value, actual, expected)
if $dom.isElement(value)
obj.$el = $dom.wrap(value)
current = state("current")
## if we are simply verifying the upcoming
## assertions then do not immediately end or snapshot
## else do
if verifying
obj._error = error
else
obj.end = true
obj.snapshot = true
obj.error = error
isChildLike = (subject, current) =>
(value is subject) or
isDomSubjectAndMatchesValue(value, subject) or
## if our current command is an assertion type
isAssertionType(current) or
## are we currently verifying assertions?
state("upcomingAssertions")?.length > 0 or
## did the function have arguments
functionHadArguments(current)
_.extend obj,
name: "assert"
# end: true
# snapshot: true
message: message
passed: passed
selector: value?.selector
type: (current, subject) ->
## if our current command has arguments assume
## we are an assertion that's involving the current
## subject or our value is the current subject
if isChildLike(subject, current)
"child"
else
"parent"
consoleProps: =>
obj = {Command: "assert"}
_.extend obj, parseValueActualAndExpected(value, actual, expected)
_.extend obj,
Message: message.replace(bTagOpen, "").replace(bTagClosed, "")
## think about completely gutting the whole object toString
## which chai does by default, its so ugly and worthless
if error
error.onFail = (err) ->
Cypress.log(obj)
return null
assert = ->
## if we've temporarily overriden assertions
## then just bail early with this function
fn = state("overrideAssert") ? assertFn
fn.apply(@, arguments)
return {
finishAssertions
verifyUpcomingAssertions
assert
}
module.exports = {
create
}