Skip to content

Commit 1e44061

Browse files
authored
ref: use console.warn for alerts and store them in Set (#455)
* ref: use console.warn for alerts and store them in Set * test: consoleAlert and consoleAlertOnce tests
1 parent 46c7a51 commit 1e44061

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"env": {
33
"node": true,
44
"mocha": true,
5+
"es6": true
56
},
67
"rules": {
78
"block-scoped-var": 2,

lib/utils.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var protocolMap = {
1515
https: 443
1616
};
1717

18-
var consoleAlerts = {};
18+
var consoleAlerts = new Set();
1919

2020
// Default Node.js REPL depth
2121
var MAX_SERIALIZE_EXCEPTION_DEPTH = 3;
@@ -131,16 +131,20 @@ module.exports.disableConsoleAlerts = function disableConsoleAlerts() {
131131
consoleAlerts = false;
132132
};
133133

134+
module.exports.enableConsoleAlerts = function enableConsoleAlerts() {
135+
consoleAlerts = new Set();
136+
};
137+
134138
module.exports.consoleAlert = function consoleAlert(msg) {
135139
if (consoleAlerts) {
136-
console.log('raven@' + ravenVersion + ' alert: ' + msg);
140+
console.warn('raven@' + ravenVersion + ' alert: ' + msg);
137141
}
138142
};
139143

140144
module.exports.consoleAlertOnce = function consoleAlertOnce(msg) {
141-
if (consoleAlerts && !(msg in consoleAlerts)) {
142-
consoleAlerts[msg] = true;
143-
console.log('raven@' + ravenVersion + ' alert: ' + msg);
145+
if (consoleAlerts && !consoleAlerts.has(msg)) {
146+
consoleAlerts.add(msg);
147+
console.warn('raven@' + ravenVersion + ' alert: ' + msg);
144148
}
145149
};
146150

test/raven.client.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ var raven = require('../'),
1111
zlib = require('zlib'),
1212
child_process = require('child_process');
1313

14-
raven.utils.disableConsoleAlerts();
15-
1614
var dsn = 'https://public:[email protected]/269';
1715

1816
var _oldConsoleWarn = console.warn;
@@ -43,6 +41,12 @@ describe('raven.Client', function() {
4341
var client;
4442
beforeEach(function() {
4543
client = new raven.Client(dsn);
44+
raven.utils.disableConsoleAlerts();
45+
});
46+
47+
afterEach(function() {
48+
client = new raven.Client(dsn);
49+
raven.utils.enableConsoleAlerts();
4650
});
4751

4852
it('should parse the DSN with options', function() {

test/raven.utils.js

+68
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ var majorVersion = parseInt(versionRegexp.exec(process.version)[1], 10);
66

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

9+
var _oldConsoleWarn = console.warn;
10+
11+
function mockConsoleWarn() {
12+
console.warn = function() {
13+
console.warn._called = true;
14+
++console.warn._callCount;
15+
};
16+
console.warn._called = false;
17+
console.warn._callCount = 0;
18+
}
19+
20+
function restoreConsoleWarn() {
21+
console.warn = _oldConsoleWarn;
22+
}
23+
924
describe('raven.utils', function() {
1025
describe('#parseDSN()', function() {
1126
it('should parse hosted Sentry DSN without path', function() {
@@ -599,4 +614,57 @@ describe('raven.utils', function() {
599614
}
600615
});
601616
});
617+
618+
describe('#consoleAlert()', function() {
619+
it('should call console.warn if enabled', function() {
620+
mockConsoleWarn();
621+
raven.utils.consoleAlert('foo');
622+
raven.utils.consoleAlert('foo');
623+
console.warn._called.should.eql(true);
624+
console.warn._callCount.should.eql(2);
625+
restoreConsoleWarn();
626+
});
627+
628+
it('should be disabled after calling disableConsoleAlerts', function() {
629+
mockConsoleWarn();
630+
raven.utils.disableConsoleAlerts();
631+
raven.utils.consoleAlert('foo');
632+
console.warn._called.should.eql(false);
633+
console.warn._callCount.should.eql(0);
634+
raven.utils.enableConsoleAlerts();
635+
restoreConsoleWarn();
636+
});
637+
638+
it('should be disabled after calling disableConsoleAlerts, even after previous successful calls', function() {
639+
mockConsoleWarn();
640+
raven.utils.consoleAlert('foo');
641+
console.warn._called.should.eql(true);
642+
console.warn._callCount.should.eql(1);
643+
raven.utils.disableConsoleAlerts();
644+
raven.utils.consoleAlert('foo');
645+
console.warn._callCount.should.eql(1);
646+
raven.utils.enableConsoleAlerts();
647+
restoreConsoleWarn();
648+
});
649+
});
650+
651+
describe('#consoleAlertOnce()', function() {
652+
it('should call console.warn if enabled, but only once with the same message', function() {
653+
mockConsoleWarn();
654+
raven.utils.consoleAlertOnce('foo');
655+
console.warn._called.should.eql(true);
656+
console.warn._callCount.should.eql(1);
657+
raven.utils.consoleAlertOnce('foo');
658+
console.warn._callCount.should.eql(1);
659+
restoreConsoleWarn();
660+
});
661+
662+
it('should be disable after calling disableConsoleAlerts', function() {
663+
mockConsoleWarn();
664+
raven.utils.disableConsoleAlerts();
665+
raven.utils.consoleAlertOnce('foo');
666+
console.warn._called.should.eql(false);
667+
console.warn._callCount.should.eql(0);
668+
raven.utils.enableConsoleAlerts();
669+
restoreConsoleWarn();
602670
});

0 commit comments

Comments
 (0)