-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
runner.spec.coffee
192 lines (142 loc) · 5.94 KB
/
runner.spec.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
describe 'middleware.runner', ->
mocks = require 'mocks'
HttpResponseMock = mocks.http.ServerResponse
HttpRequestMock = mocks.http.ServerRequest
path = require('path')
EventEmitter = require('events').EventEmitter
Browser = require '../../../lib/browser'
BrowserCollection = require '../../../lib/browser_collection'
MultReporter = require('../../../lib/reporters/multi')
createRunnerMiddleware = require('../../../lib/middleware/runner').create
Promise = require('bluebird')
handler = nextSpy = response = mockReporter = capturedBrowsers = emitter = config = null
fileListMock = executor = null
beforeEach ->
mockReporter =
adapters: []
write: (msg) -> @adapters.forEach (adapter) -> adapter msg
executor =
schedule: -> emitter.emit 'run_start'
emitter = new EventEmitter
capturedBrowsers = new BrowserCollection emitter
fileListMock =
refresh: -> Promise.resolve(null)
addFile: -> null
removeFile: -> null
changeFile: -> null
nextSpy = sinon.spy()
response = new HttpResponseMock
config = {client: {}, basePath: '/'}
handler = createRunnerMiddleware emitter, fileListMock, capturedBrowsers,
new MultReporter([mockReporter]), executor, 'localhost', 8877, '/', config
it 'should trigger test run and stream the reporter', (done) ->
capturedBrowsers.add new Browser
sinon.stub capturedBrowsers, 'areAllReady', -> true
response.once 'end', ->
expect(nextSpy).to.not.have.been.called
expect(response).to.beServedAs 200, 'result\x1FEXIT0'
done()
handler new HttpRequestMock('/__run__'), response, nextSpy
# Wrap this in a setTimeout so the fileListPromise has time to resolve.
setTimeout( ->
mockReporter.write 'result'
emitter.emit 'run_complete', capturedBrowsers, {exitCode: 0}
, 2)
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
it 'should parse body and set client.args', (done) ->
capturedBrowsers.add new Browser
sinon.stub capturedBrowsers, 'areAllReady', -> true
emitter.once 'run_start', ->
expect(config.client.args).to.deep.equal ['arg1', 'arg2']
done()
RAW_MESSAGE = '{"args": ["arg1", "arg2"]}'
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', -> true
sinon.stub fileListMock, 'refresh'
sinon.stub fileListMock, 'addFile'
sinon.stub fileListMock, 'changeFile'
sinon.stub fileListMock, 'removeFile'
RAW_MESSAGE = JSON.stringify
addedFiles: ['/new.js']
removedFiles: ['/foo.js', '/bar.js']
changedFiles: ['/changed.js']
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 schedule execution if no refresh', (done) ->
capturedBrowsers.add new Browser
sinon.stub capturedBrowsers, 'areAllReady', -> true
sinon.stub fileListMock, 'refresh'
sinon.stub executor, 'schedule'
RAW_MESSAGE = JSON.stringify {refresh: false}
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 wait for refresh to finish if applicable before scheduling execution', (done) ->
capturedBrowsers.add new Browser
sinon.stub capturedBrowsers, 'areAllReady', -> true
resolve = null
fileListPromise = new Promise (_resolve, _reject) ->
resolve = _resolve
sinon.stub(fileListMock, 'refresh').returns fileListPromise
sinon.stub executor, 'schedule'
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
resolve()
setTimeout(->
expect(executor.schedule).to.have.been.called
done()
, 2)
it 'should not schedule execution if refreshing and autoWatch', (done) ->
config.autoWatch = true
capturedBrowsers.add new Browser
sinon.stub capturedBrowsers, 'areAllReady', -> true
sinon.stub(fileListMock, 'refresh').returns Promise.resolve(null)
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()