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: Implement console.assert wrapping #1044

Merged
merged 1 commit into from
Sep 18, 2017
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
5 changes: 3 additions & 2 deletions plugins/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ function consolePlugin(Raven, console, pluginOptions) {
console = console || window.console || {};
pluginOptions = pluginOptions || {};

var logLevels = pluginOptions.levels || ['debug', 'info', 'warn', 'error'],
level = logLevels.pop();
var logLevels = pluginOptions.levels || ['debug', 'info', 'warn', 'error'];
if ('assert' in console) logLevels.push('assert');

var callback = function(msg, data) {
Raven.captureMessage(msg, data);
};

var level = logLevels.pop();
while (level) {
wrapConsoleMethod(console, level, callback);
level = logLevels.pop();
Expand Down
12 changes: 11 additions & 1 deletion src/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ var wrapMethod = function(console, level, callback) {

var msg = '' + args.join(' ');
var data = {level: sentryLevel, logger: 'console', extra: {arguments: args}};
callback && callback(msg, data);

if (level === 'assert') {
if (args[0] === false) {
// Default browsers message
msg = 'Assertion failed: ' + (args.slice(1).join(' ') || 'console.assert');
data.extra.arguments = args.slice(1);
callback && callback(msg, data);
}
} else {
callback && callback(msg, data);
}

// this fails for some browsers. :(
if (originalConsoleLevel) {
Expand Down
53 changes: 52 additions & 1 deletion test/plugins/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('console plugin', function() {
debug: function() {},
info: function() {},
warn: function() {},
error: function() {}
error: function() {},
assert: function() {}
};

consolePlugin(Raven, console);
Expand Down Expand Up @@ -51,4 +52,54 @@ describe('console plugin', function() {
}
});
});

describe('console.assert', function() {
var console;

beforeEach(function() {
console = {
assert: function() {}
};
consolePlugin(Raven, console);
this.sinon.stub(Raven, 'captureMessage');
});

it("shouldn't trigger captureMessage when assertion passes", function() {
console.assert(2 + 2 === 4, 'random message');
assert.equal(Raven.captureMessage.callCount, 0);
});

it('should trigger captureMessages when assertion fails', function() {
console.assert(2 + 2 === 5, 'random message', 'and even more');
assert.equal(Raven.captureMessage.callCount, 1);
assert.equal(
Raven.captureMessage.getCall(0).args[0],
'Assertion failed: random message and even more'
);
assert.deepEqual(Raven.captureMessage.getCall(0).args[1], {
level: 'assert',
logger: 'console',
extra: {
arguments: ['random message', 'and even more']
}
});
});

// As this is default browsers behaviour
it("should default to 'console.assert' message for failed assertion", function() {
console.assert(2 + 2 === 5);
assert.equal(Raven.captureMessage.callCount, 1);
assert.equal(
Raven.captureMessage.getCall(0).args[0],
'Assertion failed: console.assert'
);
assert.deepEqual(Raven.captureMessage.getCall(0).args[1], {
level: 'assert',
logger: 'console',
extra: {
arguments: []
}
});
});
});
});