Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): expose server event lifecycle in public API #1482

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ var start = function (injector, config, launcher, globalEmitter, preprocess, fil
globalEmitter.on('browser_register', function (browser) {
launcher.markCaptured(browser.id)

// TODO(vojta): This is lame, browser can get captured and then crash (before other browsers get
// captured).
if (config.autoWatch && launcher.areAllCaptured()) {
executor.schedule()
if (launcher.areAllCaptured()) {
globalEmitter.emit('browsers_ready')

// TODO(vojta): This is lame, browser can get captured and then crash (before other browsers get
// captured).
if (config.autoWatch) {
executor.schedule()
}
}
})

Expand Down Expand Up @@ -247,6 +251,8 @@ var start = function (injector, config, launcher, globalEmitter, preprocess, fil
log.error(error)
disconnectBrowsers(1)
})

return globalEmitter
}
start.$inject = ['injector', 'config', 'launcher', 'emitter', 'preprocess', 'fileList',
'webServer', 'capturedBrowsers', 'socketServer', 'executor', 'done']
Expand Down Expand Up @@ -304,5 +310,5 @@ exports.start = function (cliOptions, done) {

var injector = new di.Injector(modules)

injector.invoke(start)
return injector.invoke(start)
}
22 changes: 22 additions & 0 deletions test/unit/server.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ describe 'server', ->
expect(mockWebServer.listen).to.have.been.called
expect(mockInjector.invoke).to.have.been.calledWith mockLauncher.launch, mockLauncher

it 'should return the event emitter', ->
emitter = m.start(mockInjector, mockConfig, mockLauncher, emitter, null, mockFileList,
mockWebServer, browserCollection, mockSocketServer, mockExecutor, doneSpy)

expect(emitter).not.to.be.null
expect(emitter).to.be.an.instanceof(EventEmitter)

it 'should emit a browsers_ready event once all the browsers are captured', ->
emitter = m.start(mockInjector, mockConfig, mockLauncher, emitter, null, mockFileList,
mockWebServer, browserCollection, mockSocketServer, mockExecutor, doneSpy)

browsersReady = sinon.spy()
emitter.on('browsers_ready', browsersReady)

mockLauncher.areAllCaptured = -> false
fileListOnResolve()
expect(browsersReady).not.to.have.been.called

mockLauncher.areAllCaptured = -> true
emitter.emit('browser_register', {})
expect(browsersReady).to.have.been.called

it 'should try next port if already in use', ->
m.start(mockInjector, mockConfig, mockLauncher, emitter, null, mockFileList,
mockWebServer, browserCollection, mockSocketServer, mockExecutor, doneSpy)
Expand Down