2017-04-04 09:58:45 -04:00
|
|
|
import Raven from 'raven-js';
|
2017-04-15 07:29:46 -04:00
|
|
|
import RavenConfig from '~/raven/raven_config';
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-05-03 13:37:52 -04:00
|
|
|
describe('RavenConfig', () => {
|
2017-04-18 13:58:03 -04:00
|
|
|
describe('IGNORE_ERRORS', () => {
|
|
|
|
it('should be an array of strings', () => {
|
|
|
|
const areStrings = RavenConfig.IGNORE_ERRORS.every(error => typeof error === 'string');
|
|
|
|
|
|
|
|
expect(areStrings).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('IGNORE_URLS', () => {
|
|
|
|
it('should be an array of regexps', () => {
|
|
|
|
const areRegExps = RavenConfig.IGNORE_URLS.every(url => url instanceof RegExp);
|
|
|
|
|
|
|
|
expect(areRegExps).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SAMPLE_RATE', () => {
|
|
|
|
it('should be a finite number', () => {
|
|
|
|
expect(typeof RavenConfig.SAMPLE_RATE).toEqual('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-04 09:58:45 -04:00
|
|
|
describe('init', () => {
|
2017-05-21 08:38:03 -04:00
|
|
|
const options = {
|
|
|
|
currentUserId: 1,
|
|
|
|
};
|
2017-04-14 16:09:16 -04:00
|
|
|
|
2017-04-04 09:58:45 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(RavenConfig, 'configure');
|
|
|
|
spyOn(RavenConfig, 'bindRavenErrors');
|
|
|
|
spyOn(RavenConfig, 'setUser');
|
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
RavenConfig.init(options);
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
it('should set the options property', () => {
|
|
|
|
expect(RavenConfig.options).toEqual(options);
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
it('should call the configure method', () => {
|
|
|
|
expect(RavenConfig.configure).toHaveBeenCalled();
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
it('should call the error bindings method', () => {
|
|
|
|
expect(RavenConfig.bindRavenErrors).toHaveBeenCalled();
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
it('should call setUser', () => {
|
|
|
|
expect(RavenConfig.setUser).toHaveBeenCalled();
|
2017-04-04 09:58:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call setUser if there is no current user ID', () => {
|
2017-04-14 16:09:16 -04:00
|
|
|
RavenConfig.setUser.calls.reset();
|
|
|
|
|
2017-05-21 08:38:03 -04:00
|
|
|
options.currentUserId = undefined;
|
|
|
|
|
|
|
|
RavenConfig.init(options);
|
2017-04-04 09:58:45 -04:00
|
|
|
|
|
|
|
expect(RavenConfig.setUser).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('configure', () => {
|
2017-04-14 16:09:16 -04:00
|
|
|
let raven;
|
2017-04-28 11:01:22 -04:00
|
|
|
let ravenConfig;
|
2017-05-21 08:38:03 -04:00
|
|
|
const options = {
|
|
|
|
sentryDsn: '//sentryDsn',
|
|
|
|
whitelistUrls: ['//gitlabUrl'],
|
|
|
|
isProduction: true,
|
|
|
|
release: 'revision',
|
|
|
|
tags: {
|
|
|
|
revision: 'revision',
|
|
|
|
},
|
|
|
|
};
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
beforeEach(() => {
|
2017-04-28 11:01:22 -04:00
|
|
|
ravenConfig = jasmine.createSpyObj('ravenConfig', ['shouldSendSample']);
|
2017-04-14 16:09:16 -04:00
|
|
|
raven = jasmine.createSpyObj('raven', ['install']);
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
spyOn(Raven, 'config').and.returnValue(raven);
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-28 11:01:22 -04:00
|
|
|
ravenConfig.options = options;
|
|
|
|
ravenConfig.IGNORE_ERRORS = 'ignore_errors';
|
|
|
|
ravenConfig.IGNORE_URLS = 'ignore_urls';
|
|
|
|
|
|
|
|
RavenConfig.configure.call(ravenConfig);
|
2017-04-14 16:09:16 -04:00
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
it('should call Raven.config', () => {
|
|
|
|
expect(Raven.config).toHaveBeenCalledWith(options.sentryDsn, {
|
2017-05-19 16:13:57 -04:00
|
|
|
release: options.release,
|
|
|
|
tags: options.tags,
|
2017-04-14 16:09:16 -04:00
|
|
|
whitelistUrls: options.whitelistUrls,
|
|
|
|
environment: 'production',
|
2017-04-28 11:01:22 -04:00
|
|
|
ignoreErrors: ravenConfig.IGNORE_ERRORS,
|
|
|
|
ignoreUrls: ravenConfig.IGNORE_URLS,
|
|
|
|
shouldSendCallback: jasmine.any(Function),
|
2017-04-04 09:58:45 -04:00
|
|
|
});
|
2017-04-14 16:09:16 -04:00
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
it('should call Raven.install', () => {
|
|
|
|
expect(raven.install).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .environment to development if isProduction is false', () => {
|
2017-04-28 11:01:22 -04:00
|
|
|
ravenConfig.options.isProduction = false;
|
2017-04-14 16:09:16 -04:00
|
|
|
|
2017-04-28 11:01:22 -04:00
|
|
|
RavenConfig.configure.call(ravenConfig);
|
2017-04-04 09:58:45 -04:00
|
|
|
|
2017-04-14 16:09:16 -04:00
|
|
|
expect(Raven.config).toHaveBeenCalledWith(options.sentryDsn, {
|
2017-05-21 08:38:03 -04:00
|
|
|
release: options.release,
|
|
|
|
tags: options.tags,
|
2017-04-14 16:09:16 -04:00
|
|
|
whitelistUrls: options.whitelistUrls,
|
|
|
|
environment: 'development',
|
2017-04-28 11:01:22 -04:00
|
|
|
ignoreErrors: ravenConfig.IGNORE_ERRORS,
|
|
|
|
ignoreUrls: ravenConfig.IGNORE_URLS,
|
|
|
|
shouldSendCallback: jasmine.any(Function),
|
2017-04-04 09:58:45 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setUser', () => {
|
2017-04-14 16:09:16 -04:00
|
|
|
let ravenConfig;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
ravenConfig = { options: { currentUserId: 1 } };
|
|
|
|
spyOn(Raven, 'setUserContext');
|
|
|
|
|
|
|
|
RavenConfig.setUser.call(ravenConfig);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .setUserContext', function () {
|
|
|
|
expect(Raven.setUserContext).toHaveBeenCalledWith({
|
|
|
|
id: ravenConfig.options.currentUserId,
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handleRavenErrors', () => {
|
2017-04-14 22:38:59 -04:00
|
|
|
let event;
|
|
|
|
let req;
|
|
|
|
let config;
|
|
|
|
let err;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
event = {};
|
|
|
|
req = { status: 'status', responseText: 'responseText', statusText: 'statusText' };
|
|
|
|
config = { type: 'type', url: 'url', data: 'data' };
|
|
|
|
err = {};
|
|
|
|
|
|
|
|
spyOn(Raven, 'captureMessage');
|
|
|
|
|
|
|
|
RavenConfig.handleRavenErrors(event, req, config, err);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call Raven.captureMessage', () => {
|
|
|
|
expect(Raven.captureMessage).toHaveBeenCalledWith(err, {
|
|
|
|
extra: {
|
|
|
|
type: config.type,
|
|
|
|
url: config.url,
|
|
|
|
data: config.data,
|
|
|
|
status: req.status,
|
2017-05-03 09:47:21 -04:00
|
|
|
response: req.responseText,
|
2017-04-14 22:38:59 -04:00
|
|
|
error: err,
|
|
|
|
event,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if no err is provided', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
Raven.captureMessage.calls.reset();
|
|
|
|
|
|
|
|
RavenConfig.handleRavenErrors(event, req, config);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use req.statusText as the error value', () => {
|
|
|
|
expect(Raven.captureMessage).toHaveBeenCalledWith(req.statusText, {
|
|
|
|
extra: {
|
|
|
|
type: config.type,
|
|
|
|
url: config.url,
|
|
|
|
data: config.data,
|
|
|
|
status: req.status,
|
2017-05-03 09:47:21 -04:00
|
|
|
response: req.responseText,
|
2017-04-14 22:38:59 -04:00
|
|
|
error: req.statusText,
|
|
|
|
event,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-15 07:29:46 -04:00
|
|
|
|
|
|
|
describe('if no req.responseText is provided', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
req.responseText = undefined;
|
|
|
|
|
|
|
|
Raven.captureMessage.calls.reset();
|
|
|
|
|
|
|
|
RavenConfig.handleRavenErrors(event, req, config, err);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use `Unknown response text` as the response', () => {
|
|
|
|
expect(Raven.captureMessage).toHaveBeenCalledWith(err, {
|
|
|
|
extra: {
|
|
|
|
type: config.type,
|
|
|
|
url: config.url,
|
|
|
|
data: config.data,
|
|
|
|
status: req.status,
|
|
|
|
response: 'Unknown response text',
|
|
|
|
error: err,
|
|
|
|
event,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
});
|
2017-04-18 13:58:03 -04:00
|
|
|
|
|
|
|
describe('shouldSendSample', () => {
|
|
|
|
let randomNumber;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
RavenConfig.SAMPLE_RATE = 50;
|
|
|
|
|
|
|
|
spyOn(Math, 'random').and.callFake(() => randomNumber);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call Math.random', () => {
|
|
|
|
RavenConfig.shouldSendSample();
|
|
|
|
|
|
|
|
expect(Math.random).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true if the sample rate is greater than the random number * 100', () => {
|
|
|
|
randomNumber = 0.1;
|
|
|
|
|
|
|
|
expect(RavenConfig.shouldSendSample()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if the sample rate is less than the random number * 100', () => {
|
|
|
|
randomNumber = 0.9;
|
|
|
|
|
|
|
|
expect(RavenConfig.shouldSendSample()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true if the sample rate is equal to the random number * 100', () => {
|
|
|
|
randomNumber = 0.5;
|
|
|
|
|
|
|
|
expect(RavenConfig.shouldSendSample()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2017-04-04 09:58:45 -04:00
|
|
|
});
|