Skip to content

Commit 177e2ef

Browse files
krotscheckjohnjbarton
authored andcommitted
feat(async): frameworks can be loaded asynchronously (#3297)
Closes #851
1 parent 42933c9 commit 177e2ef

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

lib/server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Server extends KarmaEventEmitter {
130130
})
131131
})
132132
config.port = this._boundServer.address().port
133-
this._injector.invoke(this._start, this)
133+
await this._injector.invoke(this._start, this)
134134
} catch (err) {
135135
this.dieOnError(`Server start failed on port ${config.port}: ${err}`)
136136
}
@@ -148,15 +148,17 @@ class Server extends KarmaEventEmitter {
148148
return this._fileList ? this._fileList.changeFile(path) : Promise.resolve()
149149
}
150150

151-
_start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
151+
async _start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
152152
if (config.detached) {
153153
this._detach(config, done)
154154
return
155155
}
156156

157157
this._fileList = fileList
158158

159-
config.frameworks.forEach((framework) => this._injector.get('framework:' + framework))
159+
await Promise.all(
160+
config.frameworks.map((framework) => this._injector.get('framework:' + framework))
161+
)
160162

161163
const webServer = this._injector.get('webServer')
162164
const socketServer = this._injector.get('socketServer')

test/unit/server.spec.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ describe('server', () => {
190190
server._boundServer = mockBoundServer
191191
})
192192

193-
it('should start the web server after all files have been preprocessed successfully', () => {
194-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
193+
it('should start the web server after all files have been preprocessed successfully', async () => {
194+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
195195

196196
expect(mockFileList.refresh).to.have.been.called
197197
expect(fileListOnResolve).not.to.be.null
@@ -204,8 +204,8 @@ describe('server', () => {
204204
expect(server._injector.invoke).to.have.been.calledWith(mockLauncher.launch, mockLauncher)
205205
})
206206

207-
it('should start the web server after all files have been preprocessed with an error', () => {
208-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
207+
it('should start the web server after all files have been preprocessed with an error', async () => {
208+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
209209

210210
expect(mockFileList.refresh).to.have.been.called
211211
expect(fileListOnReject).not.to.be.null
@@ -218,8 +218,8 @@ describe('server', () => {
218218
expect(server._injector.invoke).to.have.been.calledWith(mockLauncher.launch, mockLauncher)
219219
})
220220

221-
it('should launch browsers after the web server has started', () => {
222-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
221+
it('should launch browsers after the web server has started', async () => {
222+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
223223

224224
expect(mockWebServer.listen).not.to.have.been.called
225225
expect(webServerOnError).not.to.be.null
@@ -230,8 +230,8 @@ describe('server', () => {
230230
expect(server._injector.invoke).to.have.been.calledWith(mockLauncher.launch, mockLauncher)
231231
})
232232

233-
it('should emit a listening event once server begin accepting connections', () => {
234-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
233+
it('should emit a listening event once server begin accepting connections', async () => {
234+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
235235

236236
const listening = sinon.spy()
237237
server.on('listening', listening)
@@ -242,8 +242,8 @@ describe('server', () => {
242242
expect(listening).to.have.been.calledWith(9876)
243243
})
244244

245-
it('should emit a browsers_ready event once all the browsers are captured', () => {
246-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
245+
it('should emit a browsers_ready event once all the browsers are captured', async () => {
246+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
247247

248248
const browsersReady = sinon.spy()
249249
server.on('browsers_ready', browsersReady)
@@ -257,8 +257,8 @@ describe('server', () => {
257257
expect(browsersReady).to.have.been.called
258258
})
259259

260-
it('should emit a browser_register event for each browser added', () => {
261-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
260+
it('should emit a browser_register event for each browser added', async () => {
261+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
262262

263263
const browsersReady = sinon.spy()
264264
server.on('browsers_ready', browsersReady)
@@ -272,12 +272,11 @@ describe('server', () => {
272272
expect(browsersReady).to.have.been.called
273273
})
274274

275-
it('should exit with error exit code on load errors', (done) => {
275+
it('should exit with error exit code on load errors', async () => {
276276
mockProcess(process)
277277

278-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
278+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
279279
expect(exitCode).to.have.equal(1)
280-
done()
281280
})
282281

283282
server.loadErrors.push(['TestError', 'Test'])
@@ -292,9 +291,9 @@ describe('server', () => {
292291
describe('reconnecting browser', () => {
293292
let mockBrowserSocket
294293

295-
beforeEach(() => {
294+
beforeEach(async () => {
296295
browserCollection = new BrowserCollection(server)
297-
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
296+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
298297

299298
mockBrowserSocket = {
300299
id: 'browser-socket-id',

0 commit comments

Comments
 (0)