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

Commit

Permalink
test: consoleAlert and consoleAlertOnce tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed May 9, 2018
1 parent f56d9f0 commit 3d1fea8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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);
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:private@app.getsentry.com/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
70 changes: 70 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 @@ -566,4 +581,59 @@ describe('raven.utils', function() {
raven.utils.serializeKeysForMessage('foo').should.eql('foo');
});
});

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 3d1fea8

Please sign in to comment.