-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
runner.spec.js
375 lines (315 loc) · 11.3 KB
/
runner.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
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
var path = require('path')
var EventEmitter = require('events').EventEmitter
var mocks = require('mocks')
var Promise = require('bluebird')
var _ = require('lodash')
var Browser = require('../../../lib/browser')
var BrowserCollection = require('../../../lib/browser_collection')
var MultReporter = require('../../../lib/reporters/multi')
var createRunnerMiddleware = require('../../../lib/middleware/runner').create
var HttpResponseMock = mocks.http.ServerResponse
var HttpRequestMock = mocks.http.ServerRequest
describe('middleware.runner', () => {
var nextSpy
var response
var mockReporter
var capturedBrowsers
var emitter
var config
var executor
var handler
var fileListMock
before(() => {
Promise.setScheduler((fn) => fn())
})
after(() => {
Promise.setScheduler((fn) => process.nextTick(fn))
})
beforeEach(() => {
mockReporter = {
adapters: [],
write (msg) {
return this.adapters.forEach((adapter) => adapter(msg))
}
}
executor = {
schedule: () => emitter.emit('run_start')
}
emitter = new EventEmitter()
capturedBrowsers = new BrowserCollection(emitter)
fileListMock = {
refresh: () => Promise.resolve(),
addFile: () => null,
removeFile: () => null,
changeFile: () => null
}
nextSpy = sinon.spy()
response = new HttpResponseMock()
config = {client: {}, basePath: '/'}
handler = createRunnerMiddleware(
emitter,
fileListMock,
capturedBrowsers,
new MultReporter([mockReporter]),
executor,
'http:',
'localhost',
8877,
'/',
config
)
})
it('should trigger test run and stream the reporter', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
response.once('end', () => {
expect(nextSpy).to.not.have.been.called
expect(response).to.beServedAs(200, 'result\x1FEXIT10')
done()
})
handler(new HttpRequestMock('/__run__'), response, nextSpy)
mockReporter.write('result')
emitter.emit('run_complete', capturedBrowsers, {exitCode: 0})
})
it('should set the empty to 0 if empty results', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
response.once('end', () => {
expect(nextSpy).to.not.have.been.called
expect(response).to.beServedAs(200, 'result\x1FEXIT00')
done()
})
handler(new HttpRequestMock('/__run__'), response, nextSpy)
mockReporter.write('result')
emitter.emit('run_complete', capturedBrowsers, {exitCode: 0, success: 0, failed: 0})
})
it('should set the empty to 1 if successful tests', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
response.once('end', () => {
expect(nextSpy).to.not.have.been.called
expect(response).to.beServedAs(200, 'result\x1FEXIT10')
done()
})
handler(new HttpRequestMock('/__run__'), response, nextSpy)
mockReporter.write('result')
emitter.emit('run_complete', capturedBrowsers, {exitCode: 0, success: 3, failed: 0})
})
it('should set the empty to 1 if failed tests', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
response.once('end', () => {
expect(nextSpy).to.not.have.been.called
expect(response).to.beServedAs(200, 'result\x1FEXIT10')
done()
})
handler(new HttpRequestMock('/__run__'), response, nextSpy)
mockReporter.write('result')
emitter.emit('run_complete', capturedBrowsers, {exitCode: 0, success: 0, failed: 6})
})
it('should not run if there is no browser captured', (done) => {
sinon.stub(fileListMock, 'refresh')
response.once('end', () => {
expect(nextSpy).to.not.have.been.called
expect(response).to.beServedAs(200, 'No captured browser, open http://localhost:8877/\n')
expect(fileListMock.refresh).not.to.have.been.called
done()
})
handler(new HttpRequestMock('/__run__'), response, nextSpy)
})
var clientArgsRuns = [
{
desc: 'should parse body and set client.args',
expected: ['arg1', 'arg2'],
rawMessage: '{"args": ["arg1", "arg2"]}'
},
{
desc: 'should set array client args passed by run when there are no existing client.args',
expected: ['my_args'],
rawMessage: '{"args": ["my_args"]}'
},
{
desc: 'should set object client args passed by run when there are no existing client.args',
expected: {arg2: 'fig', arg3: 'chocolate'},
rawMessage: '{"args": {"arg2": "fig", "arg3": "chocolate"}}'
},
{
desc: 'should overwrite empty array client.args when run passes an array for client.args',
expected: ['user_arg1'],
rawMessage: '{"args": ["user_arg1"]}',
existingConfig: [],
expectedOriginalArgs: []
},
{
desc: 'should overwrite empty array client.args when run passes an object for client.args',
expected: {arg2: 'figs', arg3: 'chocolates'},
rawMessage: '{"args": {"arg2": "figs", "arg3": "chocolates"}}',
existingConfig: [],
expectedOriginalArgs: []
},
{
desc: 'should overwrite empty object client.args when run passes an array for client.args',
expected: ['user_arg'],
rawMessage: '{"args": ["user_arg"]}',
existingConfig: {},
expectedOriginalArgs: {}
},
{
desc: 'should not overwrite existing array client.args when run passes an empty array for client.args',
expected: ['user_arg'],
rawMessage: '{"args": []}',
existingConfig: ['user_arg']
},
{
desc: 'should not overwrite existing array client.args when run passes an empty object for client.args',
expected: ['user_arg'],
rawMessage: '{"args": {}}',
existingConfig: ['user_arg']
},
{
desc: 'should not overwrite existing array client.args when run passes no client.args',
expected: ['user_arg'],
rawMessage: '{}',
existingConfig: ['user_arg']
},
{
desc: 'should merge existing client.args with client.args passed by run',
expected: {arg1: 'cherry', arg2: 'fig', arg3: 'chocolate'},
rawMessage: '{"args": {"arg2": "fig", "arg3": "chocolate"}}',
existingConfig: {arg1: 'cherry', arg2: 'mango'},
expectedOriginalArgs: {arg1: 'cherry', arg2: 'mango'}
},
{
desc: 'should merge empty client.args with client.args passed by run',
expected: {arg2: 'fig', arg3: 'chocolate'},
rawMessage: '{"args": {"arg2": "fig", "arg3": "chocolate"}}',
existingConfig: {},
expectedOriginalArgs: {}
},
{
desc: 'should not persist client.args passed by user from previous run',
expected: [],
rawMessage: '{"args": []}',
existingConfig: ['--somearg'],
expectedOriginalArgs: null,
originalArgs: []
}
]
describe('', function () {
clientArgsRuns.forEach(function (run) {
it(run.desc, (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
if (run.existingConfig) {
config = _.merge(config, {client: {args: run.existingConfig}})
if (run.originalArgs) {
config = _.merge(config, {client: {originalArgs: run.originalArgs}})
}
}
emitter.once('run_start', () => {
expect(config.client.args).to.deep.equal(run.expected)
if (run.expectedOriginalArgs) {
expect(config.client.originalArgs).to.deep.equal(run.expectedOriginalArgs)
}
done()
})
var RAW_MESSAGE = run.rawMessage
var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})
handler(request, response, nextSpy)
request.emit('data', RAW_MESSAGE)
request.emit('end')
})
})
})
it('should refresh explicit files if specified', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
sinon.stub(fileListMock, 'refresh')
sinon.stub(fileListMock, 'addFile')
sinon.stub(fileListMock, 'changeFile')
sinon.stub(fileListMock, 'removeFile')
var RAW_MESSAGE = JSON.stringify({
addedFiles: ['/new.js'],
removedFiles: ['/foo.js', '/bar.js'],
changedFiles: ['/changed.js']
})
var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})
handler(request, response, nextSpy)
request.emit('data', RAW_MESSAGE)
request.emit('end')
process.nextTick(() => {
expect(fileListMock.refresh).not.to.have.been.called
expect(fileListMock.addFile).to.have.been.calledWith(path.resolve('/new.js'))
expect(fileListMock.removeFile).to.have.been.calledWith(path.resolve('/foo.js'))
expect(fileListMock.removeFile).to.have.been.calledWith(path.resolve('/bar.js'))
expect(fileListMock.changeFile).to.have.been.calledWith(path.resolve('/changed.js'))
done()
})
})
it('should wait for refresh to finish if applicable before scheduling execution', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
var res = null
var fileListPromise = new Promise((resolve, reject) => {
res = resolve
})
sinon.stub(fileListMock, 'refresh').returns(fileListPromise)
sinon.stub(executor, 'schedule')
var request = new HttpRequestMock('/__run__')
handler(request, response, nextSpy)
process.nextTick(() => {
expect(fileListMock.refresh).to.have.been.called
expect(executor.schedule).to.not.have.been.called
// Now try resolving the promise
res()
setTimeout(() => {
expect(executor.schedule).to.have.been.called
done()
}, 2)
})
})
it('should schedule execution if no refresh', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
sinon.spy(fileListMock, 'refresh')
sinon.stub(executor, 'schedule')
var RAW_MESSAGE = JSON.stringify({refresh: false})
var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})
handler(request, response, nextSpy)
request.emit('data', RAW_MESSAGE)
request.emit('end')
process.nextTick(() => {
expect(fileListMock.refresh).not.to.have.been.called
expect(executor.schedule).to.have.been.called
done()
})
})
it('should not schedule execution if refreshing and autoWatch', (done) => {
config.autoWatch = true
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady').callsFake(() => true)
sinon.spy(fileListMock, 'refresh')
sinon.stub(executor, 'schedule')
handler(new HttpRequestMock('/__run__'), response, nextSpy)
process.nextTick(() => {
expect(fileListMock.refresh).to.have.been.called
expect(executor.schedule).not.to.have.been.called
done()
})
})
it('should ignore other urls', (done) => {
handler(new HttpRequestMock('/something'), response, () => {
expect(response).to.beNotServed()
done()
})
})
})