Skip to content

Commit

Permalink
Preserve original console methods (fixes #373)
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Aug 25, 2015
1 parent fb26b90 commit f92ff9d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ var _Raven = window.Raven,
authQueryString,
isRavenInstalled = false,
objectPrototype = Object.prototype,
// capture a reference to window.console first before
// the console plugin has a chance to monkey patch
// capture references to window.console *and* all its methods first
// before the console plugin has a chance to monkey patch
originalConsole = window.console || {},
originalConsoleMethods = {},
startTime = now();

for (var method in originalConsole) {
originalConsoleMethods[method] = originalConsole[method];
}
/*
* The core Raven singleton
*
Expand Down Expand Up @@ -852,10 +856,10 @@ function uuid4() {
}

function logDebug(level) {
if (originalConsole[level] && Raven.debug) {
if (originalConsoleMethods[level] && Raven.debug) {
// _slice is coming from vendor/TraceKit/tracekit.js
// so it's accessible globally
originalConsole[level].apply(originalConsole, _slice.call(arguments, 1));
originalConsoleMethods[level].apply(originalConsole, _slice.call(arguments, 1));
}
}

Expand Down
25 changes: 20 additions & 5 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function flushRavenState() {
startTime = 0;
ravenNotConfiguredError = undefined;
originalConsole = window.console || {};
originalConsoleMethods = {};

for (var method in originalConsole) {
originalConsoleMethods[method] = originalConsole[method];
}

Raven.uninstall();
}
Expand Down Expand Up @@ -326,23 +331,33 @@ describe('globals', function() {

it('should not write to console when Raven.debug is false', function() {
Raven.debug = false;
this.sinon.stub(originalConsole, level);
this.sinon.stub(originalConsoleMethods, level);
logDebug(level, message);
assert.isFalse(originalConsole[level].called);
assert.isFalse(originalConsoleMethods[level].called);
});

it('should write to console when Raven.debug is true', function() {
Raven.debug = true;
this.sinon.stub(originalConsole, level);
this.sinon.stub(originalConsoleMethods, level);
logDebug(level, message);
assert.isTrue(originalConsole[level].calledOnce);
assert.isTrue(originalConsoleMethods[level].calledOnce);
});

it('should handle variadic arguments', function() {
Raven.debug = true;
this.sinon.stub(originalConsole, level);
this.sinon.stub(originalConsoleMethods, level);
logDebug(level, message, {}, 'foo');
});

it('should be unaffected by monkeypatches to the console built-in', function() {
Raven.debug = true;
this.sinon.stub(console, level).throws("can't touch this");
this.sinon.stub(originalConsoleMethods, level);
logDebug(level, message);
assert.isTrue(originalConsoleMethods[level].calledOnce);
assert.isFalse(console[level].called);
console[level].restore();
});
});

describe('setAuthQueryString', function() {
Expand Down

0 comments on commit f92ff9d

Please sign in to comment.