Skip to content

Commit 259be0d

Browse files
authoredOct 18, 2019
fix(server): Add test coverage for config.singleRun true branch. (#3384)
Fix async test structure. Correct logic of failOnEmptyTestSuite.
1 parent 1cd87ad commit 259be0d

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed
 

‎lib/browser_collection.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class BrowserCollection {
4646
if (results.skipped && config.failOnSkippedTests) {
4747
return 1
4848
}
49-
if (results.success + results.failed === 0 && !config.failOnEmptyTestSuite) {
50-
return 0
49+
if (results.success + results.failed === 0 && !!config.failOnEmptyTestSuite) {
50+
return 1
5151
}
5252
if (results.error) {
5353
return 1

‎lib/server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class Server extends KarmaEventEmitter {
283283

284284
const emitRunCompleteIfAllBrowsersDone = () => {
285285
if (Object.keys(singleRunDoneBrowsers).every((key) => singleRunDoneBrowsers[key])) {
286-
this.emit('run_complete', singleRunBrowsers, singleRunBrowsers.getResults(singleRunBrowserNotCaptured, config.failOnEmptyTestSuite, config.failOnFailingTestSuite))
286+
this.emit('run_complete', singleRunBrowsers, singleRunBrowsers.getResults(singleRunBrowserNotCaptured, config))
287287
}
288288
}
289289

@@ -309,6 +309,8 @@ class Server extends KarmaEventEmitter {
309309
singleRunDoneBrowsers[completedBrowser.id] = true
310310
emitRunCompleteIfAllBrowsersDone()
311311
})
312+
313+
// This is the normal exit trigger.
312314
this.on('browser_complete_with_no_more_retries', function (completedBrowser) {
313315
singleRunDoneBrowsers[completedBrowser.id] = true
314316

‎test/unit/browser_collection.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('BrowserCollection', () => {
274274
const results = {
275275
success: 0,
276276
failed: 0,
277-
error: true
277+
error: false
278278
}
279279
it('shall pass if failOnEmptyTestSuite not is set', () => {
280280
const res = collection.calculateExitCode(results)

‎test/unit/server.spec.js

+82-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('server', () => {
4242
browsers: ['fake'],
4343
singleRun: true,
4444
logLevel: 'OFF',
45+
plugins: [],
4546
browserDisconnectTolerance: 0
4647
}
4748

@@ -271,20 +272,92 @@ describe('server', () => {
271272
server.emit('browser_register', {})
272273
expect(browsersReady).to.have.been.called
273274
})
275+
describe('should exit with exit code', () => {
276+
let resolveExitCode
274277

275-
it('should exit with error exit code on load errors', async () => {
276-
mockProcess(process)
278+
async function exitCode () {
279+
return new Promise((resolve) => {
280+
resolveExitCode = resolve
281+
})
282+
}
283+
284+
it('1 on load errors', async () => {
285+
mockProcess(process)
286+
287+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
288+
resolveExitCode(exitCode)
289+
})
290+
server.loadErrors.push(['TestError', 'Test'])
291+
fileListOnResolve()
292+
293+
function mockProcess (process) {
294+
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
295+
}
277296

278-
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
279-
expect(exitCode).to.have.equal(1)
297+
expect(await exitCode()).to.have.equal(1)
280298
})
281299

282-
server.loadErrors.push(['TestError', 'Test'])
283-
fileListOnResolve()
300+
it('given on run_complete', async () => {
301+
mockProcess(process)
284302

285-
function mockProcess (process) {
286-
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
287-
}
303+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
304+
resolveExitCode(exitCode)
305+
})
306+
307+
server.emit('run_complete', browserCollection, { exitCode: 15 })
308+
309+
function mockProcess (process) {
310+
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
311+
}
312+
expect(await exitCode()).to.have.equal(15)
313+
})
314+
315+
it('1 on browser_process_failure (singleRunBrowserNotCaptured)', async () => {
316+
mockProcess(process)
317+
318+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
319+
resolveExitCode(exitCode)
320+
})
321+
322+
server.emit('browser_process_failure', { id: 'fake' })
323+
324+
function mockProcess (process) {
325+
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
326+
}
327+
expect(await exitCode()).to.have.equal(1)
328+
})
329+
330+
it('0 on browser_complete_with_no_more_retries', async () => {
331+
mockProcess(process)
332+
333+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
334+
resolveExitCode(exitCode)
335+
})
336+
337+
server.emit('browser_complete_with_no_more_retries', { id: 'fake' })
338+
339+
function mockProcess (process) {
340+
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
341+
}
342+
expect(await exitCode()).to.have.equal(0)
343+
})
344+
345+
it('1 on browser_complete_with_no_more_retries with config.failOnEmptyTestSuite', async () => {
346+
mockProcess(process)
347+
348+
mockConfig.failOnEmptyTestSuite = true
349+
350+
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
351+
resolveExitCode(exitCode)
352+
})
353+
354+
server.emit('browser_complete_with_no_more_retries', { id: 'fake' })
355+
356+
function mockProcess (process) {
357+
sinon.stub(process, 'kill').callsFake((pid, ev) => process.emit(ev))
358+
}
359+
expect(await exitCode()).to.have.equal(1)
360+
})
288361
})
289362
})
290363

0 commit comments

Comments
 (0)
Please sign in to comment.