Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
ref: use console.warn for alerts and store them in Set (#455)
Browse files Browse the repository at this point in the history
* ref: use console.warn for alerts and store them in Set
* test: consoleAlert and consoleAlertOnce tests
  • Loading branch information
kamilogorek authored May 9, 2018
1 parent 46c7a51 commit 1e44061
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"env": {
"node": true,
"mocha": true,
"es6": true
},
"rules": {
"block-scoped-var": 2,
Expand Down
14 changes: 9 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var protocolMap = {
https: 443
};

var consoleAlerts = {};
var consoleAlerts = new Set();

// Default Node.js REPL depth
var MAX_SERIALIZE_EXCEPTION_DEPTH = 3;
Expand Down Expand Up @@ -131,16 +131,20 @@ module.exports.disableConsoleAlerts = function disableConsoleAlerts() {
consoleAlerts = false;
};

module.exports.enableConsoleAlerts = function enableConsoleAlerts() {
consoleAlerts = new Set();
};

module.exports.consoleAlert = function consoleAlert(msg) {
if (consoleAlerts) {
console.log('raven@' + ravenVersion + ' alert: ' + msg);
console.warn('raven@' + ravenVersion + ' alert: ' + msg);
}
};

module.exports.consoleAlertOnce = function consoleAlertOnce(msg) {
if (consoleAlerts && !(msg in consoleAlerts)) {
consoleAlerts[msg] = true;
console.log('raven@' + ravenVersion + ' alert: ' + msg);
if (consoleAlerts && !consoleAlerts.has(msg)) {
consoleAlerts.add(msg);
console.warn('raven@' + ravenVersion + ' alert: ' + msg);
}
};

Expand Down
8 changes: 6 additions & 2 deletions test/raven.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var raven = require('../'),
zlib = require('zlib'),
child_process = require('child_process');

raven.utils.disableConsoleAlerts();

var dsn = 'https://public:[email protected]/269';

var _oldConsoleWarn = console.warn;
Expand Down Expand Up @@ -43,6 +41,12 @@ describe('raven.Client', function() {
var client;
beforeEach(function() {
client = new raven.Client(dsn);
raven.utils.disableConsoleAlerts();
});

afterEach(function() {
client = new raven.Client(dsn);
raven.utils.enableConsoleAlerts();
});

it('should parse the DSN with options', function() {
Expand Down
68 changes: 68 additions & 0 deletions test/raven.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ var majorVersion = parseInt(versionRegexp.exec(process.version)[1], 10);

var raven = require('../');

var _oldConsoleWarn = console.warn;

function mockConsoleWarn() {
console.warn = function() {
console.warn._called = true;
++console.warn._callCount;
};
console.warn._called = false;
console.warn._callCount = 0;
}

function restoreConsoleWarn() {
console.warn = _oldConsoleWarn;
}

describe('raven.utils', function() {
describe('#parseDSN()', function() {
it('should parse hosted Sentry DSN without path', function() {
Expand Down Expand Up @@ -599,4 +614,57 @@ describe('raven.utils', function() {
}
});
});

describe('#consoleAlert()', function() {
it('should call console.warn if enabled', function() {
mockConsoleWarn();
raven.utils.consoleAlert('foo');
raven.utils.consoleAlert('foo');
console.warn._called.should.eql(true);
console.warn._callCount.should.eql(2);
restoreConsoleWarn();
});

it('should be disabled after calling disableConsoleAlerts', function() {
mockConsoleWarn();
raven.utils.disableConsoleAlerts();
raven.utils.consoleAlert('foo');
console.warn._called.should.eql(false);
console.warn._callCount.should.eql(0);
raven.utils.enableConsoleAlerts();
restoreConsoleWarn();
});

it('should be disabled after calling disableConsoleAlerts, even after previous successful calls', function() {
mockConsoleWarn();
raven.utils.consoleAlert('foo');
console.warn._called.should.eql(true);
console.warn._callCount.should.eql(1);
raven.utils.disableConsoleAlerts();
raven.utils.consoleAlert('foo');
console.warn._callCount.should.eql(1);
raven.utils.enableConsoleAlerts();
restoreConsoleWarn();
});
});

describe('#consoleAlertOnce()', function() {
it('should call console.warn if enabled, but only once with the same message', function() {
mockConsoleWarn();
raven.utils.consoleAlertOnce('foo');
console.warn._called.should.eql(true);
console.warn._callCount.should.eql(1);
raven.utils.consoleAlertOnce('foo');
console.warn._callCount.should.eql(1);
restoreConsoleWarn();
});

it('should be disable after calling disableConsoleAlerts', function() {
mockConsoleWarn();
raven.utils.disableConsoleAlerts();
raven.utils.consoleAlertOnce('foo');
console.warn._called.should.eql(false);
console.warn._callCount.should.eql(0);
raven.utils.enableConsoleAlerts();
restoreConsoleWarn();
});

0 comments on commit 1e44061

Please sign in to comment.