From 30ff73b35816dad727dd04487f809497f952add5 Mon Sep 17 00:00:00 2001 From: johnjbarton Date: Thu, 15 Nov 2018 08:03:00 -0800 Subject: [PATCH] fix(browser): report errors to console during singleRun=false (#3209) Fixes #3131 --- lib/browser.js | 24 ++++++++--------- test/e2e/browser_console.feature | 36 +++++++++++++++++++++++++ test/e2e/error.feature | 18 +++++++++++++ test/e2e/step_definitions/core_steps.js | 16 +++++++++-- test/unit/browser.spec.js | 5 +--- 5 files changed, 80 insertions(+), 19 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index 8f0bdf610..81865395a 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -48,25 +48,23 @@ class Browser { onKarmaError (error) { if (this.isNotConnected()) { this.lastResult.error = true - this.emitter.emit('browser_error', this, error) - this.refreshNoActivityTimeout() } + this.emitter.emit('browser_error', this, error) + this.refreshNoActivityTimeout() } onInfo (info) { - if (this.isNotConnected()) { - if (helper.isDefined(info.dump)) { - this.emitter.emit('browser_log', this, info.dump, 'dump') - } - - if (helper.isDefined(info.log)) { - this.emitter.emit('browser_log', this, info.log, info.type) - } else if (!helper.isDefined(info.dump)) { - this.emitter.emit('browser_info', this, info) - } + if (helper.isDefined(info.dump)) { + this.emitter.emit('browser_log', this, info.dump, 'dump') + } - this.refreshNoActivityTimeout() + if (helper.isDefined(info.log)) { + this.emitter.emit('browser_log', this, info.log, info.type) + } else if (!helper.isDefined(info.dump)) { + this.emitter.emit('browser_info', this, info) } + + this.refreshNoActivityTimeout() } onStart (info) { diff --git a/test/e2e/browser_console.feature b/test/e2e/browser_console.feature index d760b76bd..69b3085a1 100644 --- a/test/e2e/browser_console.feature +++ b/test/e2e/browser_console.feature @@ -172,3 +172,39 @@ Feature: Browser Console Configuration . HeadlessChrome """ + Scenario: Execute logging program with singleRun + Given a configuration with: + """ + files = ['browser-console/log.js', 'browser-console/test.js']; + browsers = ['ChromeHeadlessNoSandbox']; + plugins = [ + 'karma-jasmine', + 'karma-chrome-launcher' + ]; + singleRun = false; + """ + When I runOut Karma + Then it passes with like: + """ + LOG: 'foo' + """ + Then it passes with like: + """ + DEBUG: 'bar' + """ + Then it passes with like: + """ + INFO: 'baz' + """ + Then it passes with like: + """ + WARN: 'foobar' + """ + Then it passes with like: + """ + ERROR: 'barbaz' + """ + Then it passes with like: + """ + SUCCESS + """ diff --git a/test/e2e/error.feature b/test/e2e/error.feature index 592740a3e..9d0f3d98d 100644 --- a/test/e2e/error.feature +++ b/test/e2e/error.feature @@ -18,3 +18,21 @@ Feature: Error Display """ SyntaxError: Unexpected token } """ + Scenario: Single-run Syntax Error in a test file + Given a configuration with: + """ + files = ['error/test.js', 'error/under-test.js']; + browsers = ['ChromeHeadlessNoSandbox']; + plugins = [ + 'karma-jasmine', + 'karma-chrome-launcher' + ]; + singleRun = false; + """ + When I monitor Karma + And I stop when the log contains 'SyntaxError' + Then it fails with like: + """ + SyntaxError: Unexpected token } + """ + And I stop a server programmatically diff --git a/test/e2e/step_definitions/core_steps.js b/test/e2e/step_definitions/core_steps.js index 420a38551..3d7581424 100644 --- a/test/e2e/step_definitions/core_steps.js +++ b/test/e2e/step_definitions/core_steps.js @@ -64,7 +64,7 @@ cucumber.defineSupportCode((a) => { } const runOut = command === 'runOut' - if (command === 'run' || command === 'runOut') { + if (command === 'run' || command === 'runOut' || command === 'monitor') { this.child = spawn('' + runtimePath, ['start', '--log-level', 'warn', configFile]) const done = () => { cleansingNeeded = true @@ -96,6 +96,9 @@ cucumber.defineSupportCode((a) => { } done() }) + if (command === 'monitor') { + done() + } }, 1000) }) } else { @@ -157,7 +160,7 @@ cucumber.defineSupportCode((a) => { defineParameterType({ name: 'command', - regexp: /run|runOut|start|init|stop/ + regexp: /run|runOut|start|init|stop|monitor/ }) defineParameterType({ @@ -177,6 +180,15 @@ cucumber.defineSupportCode((a) => { execKarma.apply(this, [command, undefined, proxyPort, proxyPath, callback]) }) + When('I stop when the log contains {string}', function (message, callback) { + setInterval(() => { + if (this.lastRun.stdout.includes(message)) { + this.child && this.child.kill() + callback() + } + }, 100) + }) + defineParameterType({ name: 'exact', regexp: /no\sdebug|like/ diff --git a/test/unit/browser.spec.js b/test/unit/browser.spec.js index 8c670eb97..447bb02cc 100644 --- a/test/unit/browser.spec.js +++ b/test/unit/browser.spec.js @@ -91,14 +91,11 @@ describe('Browser', () => { expect(spy).to.have.been.called }) - it('should ignore if browser not executing', () => { - const spy = sinon.spy() - emitter.on('browser_error', spy) + it('should not set lastResult if browser not executing', () => { browser.state = Browser.STATE_CONNECTED browser.onKarmaError() expect(browser.lastResult.error).to.equal(false) - expect(spy).not.to.have.been.called }) })