Skip to content

Commit

Permalink
fix(server): Add test coverage for config.singleRun true branch. (#3384)
Browse files Browse the repository at this point in the history
Fix async test structure. Correct logic of failOnEmptyTestSuite.
  • Loading branch information
johnjbarton authored Oct 18, 2019
1 parent 1cd87ad commit 259be0d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/browser_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class BrowserCollection {
if (results.skipped && config.failOnSkippedTests) {
return 1
}
if (results.success + results.failed === 0 && !config.failOnEmptyTestSuite) {
return 0
if (results.success + results.failed === 0 && !!config.failOnEmptyTestSuite) {
return 1
}
if (results.error) {
return 1
Expand Down
4 changes: 3 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class Server extends KarmaEventEmitter {

const emitRunCompleteIfAllBrowsersDone = () => {
if (Object.keys(singleRunDoneBrowsers).every((key) => singleRunDoneBrowsers[key])) {
this.emit('run_complete', singleRunBrowsers, singleRunBrowsers.getResults(singleRunBrowserNotCaptured, config.failOnEmptyTestSuite, config.failOnFailingTestSuite))
this.emit('run_complete', singleRunBrowsers, singleRunBrowsers.getResults(singleRunBrowserNotCaptured, config))
}
}

Expand All @@ -309,6 +309,8 @@ class Server extends KarmaEventEmitter {
singleRunDoneBrowsers[completedBrowser.id] = true
emitRunCompleteIfAllBrowsersDone()
})

// This is the normal exit trigger.
this.on('browser_complete_with_no_more_retries', function (completedBrowser) {
singleRunDoneBrowsers[completedBrowser.id] = true

Expand Down
2 changes: 1 addition & 1 deletion test/unit/browser_collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('BrowserCollection', () => {
const results = {
success: 0,
failed: 0,
error: true
error: false
}
it('shall pass if failOnEmptyTestSuite not is set', () => {
const res = collection.calculateExitCode(results)
Expand Down
91 changes: 82 additions & 9 deletions test/unit/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('server', () => {
browsers: ['fake'],
singleRun: true,
logLevel: 'OFF',
plugins: [],
browserDisconnectTolerance: 0
}

Expand Down Expand Up @@ -271,20 +272,92 @@ describe('server', () => {
server.emit('browser_register', {})
expect(browsersReady).to.have.been.called
})
describe('should exit with exit code', () => {
let resolveExitCode

it('should exit with error exit code on load errors', async () => {
mockProcess(process)
async function exitCode () {
return new Promise((resolve) => {
resolveExitCode = resolve
})
}

it('1 on load errors', async () => {
mockProcess(process)

await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
resolveExitCode(exitCode)
})
server.loadErrors.push(['TestError', 'Test'])
fileListOnResolve()

function mockProcess (process) {
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
}

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

server.loadErrors.push(['TestError', 'Test'])
fileListOnResolve()
it('given on run_complete', async () => {
mockProcess(process)

function mockProcess (process) {
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
}
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
resolveExitCode(exitCode)
})

server.emit('run_complete', browserCollection, { exitCode: 15 })

function mockProcess (process) {
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
}
expect(await exitCode()).to.have.equal(15)
})

it('1 on browser_process_failure (singleRunBrowserNotCaptured)', async () => {
mockProcess(process)

await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
resolveExitCode(exitCode)
})

server.emit('browser_process_failure', { id: 'fake' })

function mockProcess (process) {
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
}
expect(await exitCode()).to.have.equal(1)
})

it('0 on browser_complete_with_no_more_retries', async () => {
mockProcess(process)

await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
resolveExitCode(exitCode)
})

server.emit('browser_complete_with_no_more_retries', { id: 'fake' })

function mockProcess (process) {
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
}
expect(await exitCode()).to.have.equal(0)
})

it('1 on browser_complete_with_no_more_retries with config.failOnEmptyTestSuite', async () => {
mockProcess(process)

mockConfig.failOnEmptyTestSuite = true

await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
resolveExitCode(exitCode)
})

server.emit('browser_complete_with_no_more_retries', { id: 'fake' })

function mockProcess (process) {
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
}
expect(await exitCode()).to.have.equal(1)
})
})
})

Expand Down

0 comments on commit 259be0d

Please sign in to comment.