Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): Add the abillity to supress the client console. #850

Merged
merged 1 commit into from
Dec 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});