diff --git a/client/karma.js b/client/karma.js index 8bbb4858b..8d550d901 100644 --- a/client/karma.js +++ b/client/karma.js @@ -63,22 +63,24 @@ var Karma = function(socket, iframe, opener, navigator, location) { } }; - // patch the console - var localConsole = contextWindow.console = getConsole(contextWindow); - var browserConsoleLog = localConsole.log; - var logMethods = ['log', 'info', 'warn', 'error', 'debug']; - var patchConsoleMethod = function(method) { - var orig = localConsole[method]; - if (!orig) { - return; - } - localConsole[method] = function() { - self.log(method, arguments); - return Function.prototype.apply.call(orig, localConsole, arguments); + if (self.config.captureConsole) { + // patch the console + var localConsole = contextWindow.console = getConsole(contextWindow); + var browserConsoleLog = localConsole.log; + var logMethods = ['log', 'info', 'warn', 'error', 'debug']; + var patchConsoleMethod = function(method) { + var orig = localConsole[method]; + if (!orig) { + return; + } + localConsole[method] = function() { + self.log(method, arguments); + return Function.prototype.apply.call(orig, localConsole, arguments); + }; }; - }; - for (var i = 0; i < logMethods.length; i++) { - patchConsoleMethod(logMethods[i]); + for (var i = 0; i < logMethods.length; i++) { + patchConsoleMethod(logMethods[i]); + } } contextWindow.dump = function() { diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index a95d7d716..4082042bf 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -352,6 +352,12 @@ between browsers and the testing server). If true, Karma runs the tests inside an iframe. If false, Karma runs the tests in a new window. Some tests may not run in an iFrame and may need a new window to run. +## client.captureConsole +**Type:** Boolean + +**Default:** `true` + +**Description:** Capture all console output and pipe it to the terminal. ## urlRoot **Type:** String diff --git a/lib/config.js b/lib/config.js index 8a4509b6f..223b4d3d3 100644 --- a/lib/config.js +++ b/lib/config.js @@ -211,7 +211,8 @@ var Config = function() { this.plugins = ['karma-*']; this.client = { args: [], - useIframe: true + useIframe: true, + captureConsole: true }; this.browserDisconnectTimeout = 2000; this.browserDisconnectTolerance = 0; diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index 6c50a708c..43e2f6e6a 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -258,5 +258,35 @@ describe('Karma', function() { k.complete(); expect(windowLocation.href).toBe('http://return.com'); }); + + it('should patch the console if captureConsole is true', function() { + spyOn(k, 'log'); + k.config.captureConsole = true; + + var mockWindow = { + console: { + log: function () {} + } + }; + + k.setupContext(mockWindow); + mockWindow.console.log('What?'); + expect(k.log).toHaveBeenCalledWith('log', ['What?']); + }); + + it('should not patch the console if captureConsole is false', function() { + spyOn(k, 'log'); + k.config.captureConsole = false; + + var mockWindow = { + console: { + log: function () {} + } + }; + + k.setupContext(mockWindow); + mockWindow.console.log('hello'); + expect(k.log).not.toHaveBeenCalled(); + }); }); });