forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agents.ts
286 lines (220 loc) · 6.13 KB
/
agents.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
// @ts-nocheck
import _ from 'lodash'
import sinon from 'sinon'
import Promise from 'bluebird'
import $utils from '../../cypress/utils'
import $errUtils from '../../cypress/error_utils'
let counts = null
sinon.setFormatter($utils.stringifyArg.bind($utils))
const createSandbox = () => {
return sinon.createSandbox().usingPromise(Promise)
}
const display = (name) => {
if (name === 'spy') {
return 'Spied Obj'
}
if (name === 'stub') {
return 'Stubbed Obj'
}
}
const formatArgs = (args) => {
return _.map(args, (arg) => {
return $utils.stringifyArg(arg)
})
}
const getMessage = (method, args) => {
method = method ?? 'function'
args = args.length > 3
? formatArgs(args.slice(0, 3)).concat('...')
: formatArgs(args)
return `${method}(${args.join(', ')})`
}
const onInvoke = function (Cypress, obj, args) {
const { agent } = obj
const agentName = agent._cyName
// fakes are children of the agent created with `withArgs`
const fakes = agent.matchingFakes(args)
agent._cyLog.set('callCount', agent.callCount)
for (let fake of fakes) {
fake._cyLog.set('callCount', fake.callCount)
}
// bail if we've turned off logging this agent
if (agent._log === false) {
return
}
const logProps = {
name: agentName,
message: obj.message,
error: obj.error,
type: 'parent',
end: true,
snapshot: !agent._noSnapshot,
event: true,
consoleProps () {
const 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 (let fake of fakes) {
const 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
}
return consoleObj
},
}
const aliases = _.compact([agent._cyAlias].concat(_.map(fakes, '_cyAlias')))
if (aliases.length) {
logProps.alias = aliases
logProps.aliasType = 'agent'
}
return Cypress.log(logProps)
}
const onError = (err) => {
return $errUtils.throwErr(err)
}
// create a global sandbox
// to be used through all the tests
const sandbox = createSandbox()
const reset = () => {
counts = {
spy: 0,
stub: 0,
children: {},
}
sandbox.restore()
return null
}
export default function (Commands, Cypress, cy, state) {
// reset initially on a new run because we could be
// re-running from the UI or from a spec file change
reset()
const resetAndSetSandbox = () => {
reset()
// attach the sandbox to state
return state('sandbox', sandbox)
}
// before each of our tests we always want
// to reset the counts + the sandbox
Cypress.on('test:before:run', resetAndSetSandbox)
const wrap = function (ctx, type, agent, obj, method, count) {
if (!count) {
count = (counts[type] += 1)
}
const name = `${type}-${count}`
if (!agent.parent) {
counts.children[name] = 0
}
const log = Cypress.log({
instrument: 'agent',
name,
type: name,
functionName: method,
count,
callCount: 0,
})
agent._cyCount = count
agent._cyLog = log
agent._cyName = name
agent._cyType = type
const { invoke } = agent
agent.invoke = function (func, thisValue, args) {
let error = null
let 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(this, func, thisValue, args)
} catch (e) {
error = e
}
const props = {
count,
name: type,
message: getMessage(method, args),
obj,
agent,
call: agent.lastCall,
callCount: agent.callCount,
error,
log,
}
onInvoke(Cypress, props, args)
// if an error did exist then we need
// to bubble it up
if (error) {
onError(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,
})
agent._cyAlias = alias
log.set({
alias,
aliasType: 'agent',
})
agent.named(alias)
return agent
}
const { withArgs } = agent
agent.withArgs = function (...args) {
const childCount = (counts.children[name] += 1)
return wrap(ctx, type, withArgs.apply(this, args), obj, method, `${count}.${childCount}`)
}
return agent
}
const spy = function (obj, method) {
const theSpy = sandbox.spy(obj, method)
return wrap(this, 'spy', theSpy, obj, method)
}
const stub = function (obj, method, replacerFnOrValue) {
let 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 === 3) {
if (_.isFunction(replacerFnOrValue)) {
theStub = theStub.callsFake(replacerFnOrValue)
} else {
theStub = theStub.value(replacerFnOrValue)
}
}
return wrap(this, 'stub', theStub, obj, method)
}
return Commands.addAllSync({
spy,
stub,
})
}