-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
agents.coffee
244 lines (188 loc) · 5.73 KB
/
agents.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
_ = require("lodash")
sinon = require("sinon")
Promise = require("bluebird")
$utils = require("../../cypress/utils")
$errUtils = require("../../cypress/error_utils")
counts = null
sinon.setFormatter($utils.stringifyArg.bind($utils))
createSandbox = ->
sinon.createSandbox().usingPromise(Promise)
display = (name) ->
switch name
when "spy" then "Spied Obj"
when "stub" then "Stubbed Obj"
formatArgs = (args) ->
_.map args, (arg) -> $utils.stringifyArg(arg)
getMessage = (method, args) ->
method ?= "function"
args = if args.length > 3
formatArgs(args.slice(0, 3)).concat("...")
else
formatArgs(args)
"#{method}(#{args.join(', ')})"
onInvoke = (Cypress, obj, args) ->
agent = obj.agent
agentName = agent._cyName
## bail if we've turned off logging this agent
return if agent._log is false
## fakes are children of the agent created with `withArgs`
fakes = agent.matchingFakes(args)
agent._cyLog.set("callCount", agent.callCount)
for fake in fakes
fake._cyLog.set("callCount", fake.callCount)
logProps = {
name: agentName
message: obj.message
error: obj.error
type: "parent"
end: true
snapshot: !agent._noSnapshot
event: true
consoleProps: ->
consoleObj = {}
consoleObj.Command = null
consoleObj.Error = null
consoleObj.Event = "#{agentName} called"
consoleObj[agent._cyType] = agent
consoleObj["Call #"] = agent.callCount
consoleObj.Alias = agent._cyAlias
consoleObj[display(obj.name)] = obj.obj
consoleObj.Arguments = obj.call.args
consoleObj.Context = obj.call.thisValue
consoleObj.Returned = obj.call.returnValue
if obj.error
consoleObj.Error = obj.error.stack
for fake in fakes
name = fake._cyName
count = fake._cyCount
consoleObj["Child #{fake._cyType} (#{count})"] = "---"
consoleObj[" #{count} #{fake._cyType}"] = fake
consoleObj[" #{count} call #"] = fake.callCount
consoleObj[" #{count} alias"] = fake._cyAlias
consoleObj[" #{count} matching arguments"] = fake.matchingArguments
consoleObj
}
aliases = _.compact([agent._cyAlias].concat(_.map(fakes, "_cyAlias")))
if aliases.length
logProps.alias = aliases
logProps.aliasType = "agent"
Cypress.log(logProps)
onError = (err) ->
$errUtils.throwErr(err)
## create a global sandbox
## to be used through all the tests
sandbox = createSandbox()
reset = ->
counts = {
spy: 0
stub: 0
children: {}
}
sandbox.restore()
return null
module.exports = (Commands, Cypress, cy, state, config) ->
## reset initially on a new run because we could be
## re-running from the UI or from a spec file change
reset()
resetAndSetSandbox = ->
reset()
## attach the sandbox to state
state("sandbox", sandbox)
## before each of our tests we always want
## to reset the counts + the sandbox
Cypress.on("test:before:run", resetAndSetSandbox)
wrap = (ctx, type, agent, obj, method, count) ->
if not count
count = counts[type] += 1
name = "#{type}-#{count}"
if not agent.parent
counts.children[name] = 0
log = Cypress.log({
instrument: "agent"
name: name
type: name
functionName: method
count: count
callCount: 0
})
agent._cyCount = count
agent._cyLog = log
agent._cyName = name
agent._cyType = type
invoke = agent.invoke
agent.invoke = (func, thisValue, args) ->
error = null
returned = null
## because our spy could potentially fail here
## we need to wrap this in a try / catch
## so we still emit the command that failed
## and the user can easily find the error
try
returned = invoke.call(@, func, thisValue, args)
catch e
error = e
props = {
count: count
name: type
message: getMessage(method, args)
obj: obj
agent: agent
call: agent.lastCall
callCount: agent.callCount
error: error
log: log
}
onInvoke(Cypress, props, args)
## if an error did exist then we need
## to bubble it up
onError(error) if error
## make sure we return the invoked return value
## of the spy
return returned
## enable not logging this agent
agent.log = (bool = true) ->
agent._log = bool
return agent
## disable DOM snapshots during log for this agent
agent.snapshot = (bool = true) ->
agent._noSnapshot = !bool
return agent
agent.as = (alias) ->
cy.validateAlias(alias)
cy.addAlias(ctx, {
subject: agent
command: log
alias: alias
})
agent._cyAlias = alias
log.set({
alias: alias
aliasType: "agent"
})
agent.named(alias)
agent
withArgs = agent.withArgs
agent.withArgs = ->
childCount = counts.children[name] += 1
wrap(ctx, type, withArgs.apply(@, arguments), obj, method, "#{count}.#{childCount}")
return agent
spy = (obj, method) ->
theSpy = sandbox.spy(obj, method)
wrap(@, "spy", theSpy, obj, method)
stub = (obj, method, replacerFnOrValue) ->
theStub = sandbox.stub.call(sandbox, obj, method)
## sinon 2 changed the stub signature
## this maintains the 3-argument signature so it's not breaking
if arguments.length is 3
if _.isFunction(replacerFnOrValue)
theStub = theStub.callsFake(replacerFnOrValue)
else
theStub = theStub.value(replacerFnOrValue)
wrap(@, "stub", theStub, obj, method)
Commands.addAllSync({
spy: spy
stub: stub
agents: ->
$errUtils.warnByPath("agents.deprecated_warning")
return {stub, spy}
})