From e7e9646a76c3f7c3f322752451a3c025bceb22c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 8 May 2018 12:23:09 +0200 Subject: [PATCH 1/2] ref: use console.warn for alerts and store them in Set --- .eslintrc | 1 + lib/utils.js | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.eslintrc b/.eslintrc index d277d0b..3170e87 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,6 +2,7 @@ "env": { "node": true, "mocha": true, + "es6": true }, "rules": { "block-scoped-var": 2, diff --git a/lib/utils.js b/lib/utils.js index 7ac457c..38c43b1 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; @@ -133,14 +133,14 @@ module.exports.disableConsoleAlerts = function disableConsoleAlerts() { 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); } }; From e74f37d4b0edfd45546028233f57c191f9545a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Wed, 9 May 2018 11:23:28 +0200 Subject: [PATCH 2/2] test: consoleAlert and consoleAlertOnce tests --- lib/utils.js | 4 +++ test/raven.client.js | 8 ++++-- test/raven.utils.js | 68 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 38c43b1..30e045d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -131,6 +131,10 @@ module.exports.disableConsoleAlerts = function disableConsoleAlerts() { consoleAlerts = false; }; +module.exports.enableConsoleAlerts = function enableConsoleAlerts() { + consoleAlerts = new Set(); +}; + module.exports.consoleAlert = function consoleAlert(msg) { if (consoleAlerts) { console.warn('raven@' + ravenVersion + ' alert: ' + msg); diff --git a/test/raven.client.js b/test/raven.client.js index 1253456..5788e08 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -11,8 +11,6 @@ var raven = require('../'), zlib = require('zlib'), child_process = require('child_process'); -raven.utils.disableConsoleAlerts(); - var dsn = 'https://public:private@app.getsentry.com/269'; var _oldConsoleWarn = console.warn; @@ -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() { diff --git a/test/raven.utils.js b/test/raven.utils.js index d88649d..cc8b35c 100644 --- a/test/raven.utils.js +++ b/test/raven.utils.js @@ -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() { @@ -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(); });