-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
util_spec.js
237 lines (183 loc) · 7.1 KB
/
util_spec.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
require('../../spec_helper')
const Promise = require('bluebird')
const util = require(`${root}../lib/plugins/util`)
describe('lib/plugins/util', () => {
context('#wrapIpc', () => {
beforeEach(function () {
this.theProcess = {
send: sinon.spy(),
on: sinon.stub(),
}
this.ipc = util.wrapIpc(this.theProcess)
})
it('#send sends event through the process', function () {
this.ipc.send('event-name', 'arg1', 'arg2')
expect(this.theProcess.send).to.be.calledWith({
event: 'event-name',
args: ['arg1', 'arg2'],
})
})
it('#send does not send if process has been killed', function () {
this.theProcess.killed = true
this.ipc.send('event-name')
expect(this.theProcess.send).not.to.be.called
})
it('#send does not send if process has been disconnected', function () {
this.theProcess.connected = false
this.ipc.send('event-name')
expect(this.theProcess.send).not.to.be.called
})
it('#on listens for process messages that match event', function () {
const handler = sinon.spy()
this.ipc.on('event-name', handler)
this.theProcess.on.yield({
event: 'event-name',
args: ['arg1', 'arg2'],
})
expect(handler).to.be.calledWith('arg1', 'arg2')
})
it('#removeListener emoves handler', function () {
const handler = sinon.spy()
this.ipc.on('event-name', handler)
this.ipc.removeListener('event-name', handler)
this.theProcess.on.yield({
event: 'event-name',
args: ['arg1', 'arg2'],
})
expect(handler).not.to.be.called
})
})
context('#wrapChildPromise', () => {
beforeEach(function () {
this.ipc = {
send: sinon.spy(),
on: sinon.stub(),
removeListener: sinon.spy(),
}
this.invoke = sinon.stub()
this.ids = {
eventId: 0,
invocationId: '00',
}
this.args = []
})
it('calls the invoke function with the callback id and args', function () {
return util.wrapChildPromise(this.ipc, this.invoke, this.ids).then(() => {
expect(this.invoke).to.be.calledWith(0, this.args)
})
})
it('wraps the invocation in a promise', function () {
this.invoke.throws('some error') // test that we're Promise.try-ing invoke
expect(util.wrapChildPromise(this.ipc, this.invoke, this.ids)).to.be.an.instanceOf(Promise)
})
it('sends "promise:fulfilled:{invocatationId}" with value when promise resolves', function () {
this.invoke.resolves('value')
return util.wrapChildPromise(this.ipc, this.invoke, this.ids).then(() => {
expect(this.ipc.send).to.be.calledWith('promise:fulfilled:00', null, 'value')
})
})
it('serializes undefined', function () {
this.invoke.resolves(undefined)
return util.wrapChildPromise(this.ipc, this.invoke, this.ids).then(() => {
expect(this.ipc.send).to.be.calledWith('promise:fulfilled:00', null, '__cypress_undefined__')
})
})
it('sends "promise:fulfilled:{invocatationId}" with error when promise rejects', function () {
const err = new Error('fail')
err.code = 'ERM_DUN_FAILED'
err.annotated = 'annotated error'
this.invoke.rejects(err)
return util.wrapChildPromise(this.ipc, this.invoke, this.ids).then(() => {
expect(this.ipc.send).to.be.calledWith('promise:fulfilled:00')
const actualError = this.ipc.send.lastCall.args[1]
expect(actualError.name).to.equal(err.name)
expect(actualError.message).to.equal(err.message)
expect(actualError.stack).to.equal(err.stack)
expect(actualError.code).to.equal(err.code)
expect(actualError.annotated).to.equal(err.annotated)
})
})
})
context('#wrapParentPromise', () => {
beforeEach(function () {
this.ipc = {
send: sinon.spy(),
on: sinon.stub(),
removeListener: sinon.spy(),
}
this.callback = sinon.spy()
})
it('returns a promise', function () {
expect(util.wrapParentPromise(this.ipc, 0, this.callback)).to.be.an.instanceOf(Promise)
})
it('resolves the promise when "promise:fulfilled:{invocationId}" event is received without error', function () {
const promise = util.wrapParentPromise(this.ipc, 0, this.callback)
const invocationId = this.callback.lastCall.args[0]
this.ipc.on.withArgs(`promise:fulfilled:${invocationId}`).yield(null, 'value')
return promise.then((value) => {
expect(value).to.equal('value')
})
})
it('deserializes undefined', function () {
const promise = util.wrapParentPromise(this.ipc, 0, this.callback)
const invocationId = this.callback.lastCall.args[0]
this.ipc.on.withArgs(`promise:fulfilled:${invocationId}`).yield(null, '__cypress_undefined__')
return promise.then((value) => {
expect(value).to.equal(undefined)
})
})
it('rejects the promise when "promise:fulfilled:{invocationId}" event is received with error', function () {
const promise = util.wrapParentPromise(this.ipc, 0, this.callback)
const invocationId = this.callback.lastCall.args[0]
const err = {
name: 'the name',
message: 'the message',
stack: 'the stack',
}
this.ipc.on.withArgs(`promise:fulfilled:${invocationId}`).yield(err)
return promise.catch((actualErr) => {
expect(actualErr).to.be.an.instanceOf(Error)
expect(actualErr.name).to.equal(err.name)
expect(actualErr.message).to.equal(err.message)
expect(actualErr.stack).to.equal(err.stack)
})
})
it('invokes callback with unique invocation id', function () {
const firstCall = util.wrapParentPromise(this.ipc, 0, this.callback)
const invocationId = this.callback.lastCall.args[0]
this.ipc.on.withArgs(`promise:fulfilled:${invocationId}`).yield()
return firstCall.then(() => {
expect(this.callback).to.be.called
const firstId = this.callback.lastCall.args[0]
util.wrapParentPromise(this.ipc, 0, this.callback)
const secondId = this.callback.lastCall.args[0]
expect(firstId).not.to.equal(secondId)
})
})
it('removes event listener once promise is fulfilled', function () {
util.wrapParentPromise(this.ipc, 0, this.callback)
const invocationId = this.callback.lastCall.args[0]
this.ipc.on.withArgs(`promise:fulfilled:${invocationId}`).yield(null, 'value')
expect(this.ipc.removeListener).to.be.calledWith(`promise:fulfilled:${invocationId}`)
})
})
context('#serializeError', () => {
it('sends error with name, message, stack, code, and annotated properties', () => {
const err = {
name: 'the name',
message: 'the message',
stack: 'the stack',
code: 'the code',
annotated: 'the annotated version',
extra: 'this is extra',
}
expect(util.serializeError(err)).to.eql({
name: 'the name',
message: 'the message',
stack: 'the stack',
code: 'the code',
annotated: 'the annotated version',
})
})
})
})