Skip to content

Commit

Permalink
feat(config): Add the abillity to supress the client console.
Browse files Browse the repository at this point in the history
This adds the client config option  which controls if the
console output in the client is captured.
Fixes #744.
  • Loading branch information
dignifiedquire committed Dec 7, 2013
1 parent 0a6a0ee commit 4734962
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
32 changes: 17 additions & 15 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit 4734962

Please sign in to comment.