Skip to content

Commit

Permalink
fix(server): log browser messages to the terminal
Browse files Browse the repository at this point in the history
Fixes #2187
  • Loading branch information
dignifiedquire committed Jun 26, 2016
1 parent 9079348 commit d1f924c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 18 deletions.
6 changes: 5 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
11 changes: 8 additions & 3 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
16 changes: 11 additions & 5 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/reporters/dots.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/dots_color.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/progress.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/progress_color.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
39 changes: 38 additions & 1 deletion test/e2e/browser_console.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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:
Expand Down

0 comments on commit d1f924c

Please sign in to comment.