Skip to content

Commit

Permalink
feat(async): frameworks can be loaded asynchronously (#3297)
Browse files Browse the repository at this point in the history
Closes #851
  • Loading branch information
krotscheck authored and johnjbarton committed Jul 12, 2019
1 parent 42933c9 commit 177e2ef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
8 changes: 5 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Server extends KarmaEventEmitter {
})
})
config.port = this._boundServer.address().port
this._injector.invoke(this._start, this)
await this._injector.invoke(this._start, this)
} catch (err) {
this.dieOnError(`Server start failed on port ${config.port}: ${err}`)
}
Expand All @@ -148,15 +148,17 @@ class Server extends KarmaEventEmitter {
return this._fileList ? this._fileList.changeFile(path) : Promise.resolve()
}

_start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
async _start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
if (config.detached) {
this._detach(config, done)
return
}

this._fileList = fileList

config.frameworks.forEach((framework) => this._injector.get('framework:' + framework))
await Promise.all(
config.frameworks.map((framework) => this._injector.get('framework:' + framework))
)

const webServer = this._injector.get('webServer')
const socketServer = this._injector.get('socketServer')
Expand Down
33 changes: 16 additions & 17 deletions test/unit/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ describe('server', () => {
server._boundServer = mockBoundServer
})

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

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

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

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

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

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

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

const listening = sinon.spy()
server.on('listening', listening)
Expand All @@ -242,8 +242,8 @@ describe('server', () => {
expect(listening).to.have.been.calledWith(9876)
})

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

const browsersReady = sinon.spy()
server.on('browsers_ready', browsersReady)
Expand All @@ -257,8 +257,8 @@ describe('server', () => {
expect(browsersReady).to.have.been.called
})

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

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

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

server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
expect(exitCode).to.have.equal(1)
done()
})

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

beforeEach(() => {
beforeEach(async () => {
browserCollection = new BrowserCollection(server)
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

mockBrowserSocket = {
id: 'browser-socket-id',
Expand Down

0 comments on commit 177e2ef

Please sign in to comment.