From d1f924c7e0a92446c786bed8eab865b45fe3a8de Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Sun, 26 Jun 2016 14:58:12 +0200 Subject: [PATCH] fix(server): log browser messages to the terminal Fixes #2187 --- lib/config.js | 6 ++++- lib/reporter.js | 11 ++++++--- lib/reporters/base.js | 16 +++++++++---- lib/reporters/dots.js | 4 ++-- lib/reporters/dots_color.js | 4 ++-- lib/reporters/progress.js | 4 ++-- lib/reporters/progress_color.js | 4 ++-- test/e2e/browser_console.feature | 39 +++++++++++++++++++++++++++++++- 8 files changed, 70 insertions(+), 18 deletions(-) diff --git a/lib/config.js b/lib/config.js index fb0a92216..a7b27fdf2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -239,7 +239,11 @@ var Config = function () { this.httpsServerConfig = {} this.basePath = '' this.files = [] - this.browserConsoleLogOptions = {level: 'debug', format: '%b %T: %m', terminal: true} + this.browserConsoleLogOptions = { + level: 'debug', + format: '%b %T: %m', + terminal: true + } this.customContextFile = null this.customDebugFile = null this.exclude = [] diff --git a/lib/reporter.js b/lib/reporter.js index f7dad1039..38f942fe3 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -104,8 +104,8 @@ var createReporters = function (names, config, emitter, injector) { if (['dots', 'progress'].indexOf(name) !== -1) { var Cls = require('./reporters/' + name) var ClsColor = require('./reporters/' + name + '_color') - reporters.push(new Cls(errorFormatter, config.reportSlowerThan, config.colors)) - return reporters.push(new ClsColor(errorFormatter, config.reportSlowerThan, config.colors)) + reporters.push(new Cls(errorFormatter, config.reportSlowerThan, config.colors, config.browserConsoleLogOptions)) + return reporters.push(new ClsColor(errorFormatter, config.reportSlowerThan, config.colors, config.browserConsoleLogOptions)) } var locals = { @@ -146,7 +146,12 @@ var createReporters = function (names, config, emitter, injector) { return new MultiReporter(reporters) } -createReporters.$inject = ['config.reporters', 'config', 'emitter', 'injector'] +createReporters.$inject = [ + 'config.reporters', + 'config', + 'emitter', + 'injector' +] // PUBLISH exports.createReporters = createReporters diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 03b7c6a8d..d3ca539fd 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -2,7 +2,7 @@ var util = require('util') var helper = require('../helper') -var BaseReporter = function (formatError, reportSlow, useColors, browserLogOptions, adapter) { +var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) { this.adapters = [adapter || process.stdout.write.bind(process.stdout)] this.onRunStart = function () { @@ -64,7 +64,7 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserLogOptio } this.onBrowserLog = function (browser, log, type) { - if (!browserLogOptions || !browserLogOptions.terminal) return + if (!browserConsoleLogOptions || !browserConsoleLogOptions.terminal) return if (!helper.isString(log)) { // TODO(vojta): change util to new syntax (config object) log = util.inspect(log, false, undefined, this.USE_COLORS) @@ -136,13 +136,19 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserLogOptio this.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS\n' } -BaseReporter.decoratorFactory = function (formatError, reportSlow, useColors) { +BaseReporter.decoratorFactory = function (formatError, reportSlow, useColors, browserConsoleLogOptions) { + console.log('decorator', arguments) return function (self) { - BaseReporter.call(self, formatError, reportSlow, useColors) + BaseReporter.call(self, formatError, reportSlow, useColors, browserConsoleLogOptions) } } -BaseReporter.decoratorFactory.$inject = ['formatError', 'config.reportSlowerThan', 'config.colors', 'config.browserLogOptions'] +BaseReporter.decoratorFactory.$inject = [ + 'formatError', + 'config.reportSlowerThan', + 'config.colors', + 'config.browserLogOptions' +] // PUBLISH module.exports = BaseReporter diff --git a/lib/reporters/dots.js b/lib/reporters/dots.js index e4aac0515..019d861e4 100644 --- a/lib/reporters/dots.js +++ b/lib/reporters/dots.js @@ -1,7 +1,7 @@ var BaseReporter = require('./base') -var DotsReporter = function (formatError, reportSlow, useColors) { - BaseReporter.call(this, formatError, reportSlow, useColors) +var DotsReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions) { + BaseReporter.call(this, formatError, reportSlow, useColors, browserConsoleLogOptions) var DOTS_WRAP = 80 this.EXCLUSIVELY_USE_COLORS = false diff --git a/lib/reporters/dots_color.js b/lib/reporters/dots_color.js index 3da3ffdb3..312fa26e4 100644 --- a/lib/reporters/dots_color.js +++ b/lib/reporters/dots_color.js @@ -1,8 +1,8 @@ var DotsReporter = require('./dots') var BaseColorReporter = require('./base_color') -var DotsColorReporter = function (formatError, reportSlow, useColors) { - DotsReporter.call(this, formatError, reportSlow, useColors) +var DotsColorReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions) { + DotsReporter.call(this, formatError, reportSlow, useColors, browserConsoleLogOptions) BaseColorReporter.call(this) this.EXCLUSIVELY_USE_COLORS = true } diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index a20a166b8..e437250ae 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -1,7 +1,7 @@ var BaseReporter = require('./base') -var ProgressReporter = function (formatError, reportSlow, useColors) { - BaseReporter.call(this, formatError, reportSlow, useColors) +var ProgressReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions) { + BaseReporter.call(this, formatError, reportSlow, useColors, browserConsoleLogOptions) this.EXCLUSIVELY_USE_COLORS = false diff --git a/lib/reporters/progress_color.js b/lib/reporters/progress_color.js index bd607fa7b..f5c2c3a0c 100644 --- a/lib/reporters/progress_color.js +++ b/lib/reporters/progress_color.js @@ -1,8 +1,8 @@ var ProgressReporter = require('./progress') var BaseColorReporter = require('./base_color') -var ProgressColorReporter = function (formatError, reportSlow, useColors) { - ProgressReporter.call(this, formatError, reportSlow, useColors) +var ProgressColorReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions) { + ProgressReporter.call(this, formatError, reportSlow, useColors, browserConsoleLogOptions) BaseColorReporter.call(this) this.EXCLUSIVELY_USE_COLORS = true } diff --git a/test/e2e/browser_console.feature b/test/e2e/browser_console.feature index 07203dd5b..143930918 100644 --- a/test/e2e/browser_console.feature +++ b/test/e2e/browser_console.feature @@ -3,6 +3,43 @@ Feature: Browser Console Configuration As a person who wants to write great tests I want to be able to customize how the browser console is logged. + Scenario: Execute logging program with defaults + Given a configuration with: + """ + files = ['browser-console/log.js', 'browser-console/test.js']; + browsers = ['PhantomJS']; + plugins = [ + 'karma-jasmine', + 'karma-phantomjs-launcher' + ]; + """ + When I start 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 + """ + + Scenario: Execute logging program Given a configuration with: """ @@ -15,7 +52,7 @@ Feature: Browser Console Configuration browserConsoleLogOptions = { path: 'console.log', format: '%t:%m' - }; + }; """ When I start Karma Then the file at console.log contains: