summaryrefslogtreecommitdiff
path: root/spec/frontend/sentry/sentry_config_spec.js
blob: f7102f9b2f9f6922265be839b71e8578de642007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import SentryConfig from '~/sentry/sentry_config';
import * as Sentry from '~/sentry/wrapper';

describe('SentryConfig', () => {
  describe('IGNORE_ERRORS', () => {
    it('should be an array of strings', () => {
      const areStrings = SentryConfig.IGNORE_ERRORS.every((error) => typeof error === 'string');

      expect(areStrings).toBe(true);
    });
  });

  describe('BLACKLIST_URLS', () => {
    it('should be an array of regexps', () => {
      const areRegExps = SentryConfig.BLACKLIST_URLS.every((url) => url instanceof RegExp);

      expect(areRegExps).toBe(true);
    });
  });

  describe('SAMPLE_RATE', () => {
    it('should be a finite number', () => {
      expect(typeof SentryConfig.SAMPLE_RATE).toEqual('number');
    });
  });

  describe('init', () => {
    const options = {
      currentUserId: 1,
    };

    beforeEach(() => {
      jest.spyOn(SentryConfig, 'configure');
      jest.spyOn(SentryConfig, 'bindSentryErrors');
      jest.spyOn(SentryConfig, 'setUser');

      SentryConfig.init(options);
    });

    it('should set the options property', () => {
      expect(SentryConfig.options).toEqual(options);
    });

    it('should call the configure method', () => {
      expect(SentryConfig.configure).toHaveBeenCalled();
    });

    it('should call the error bindings method', () => {
      expect(SentryConfig.bindSentryErrors).toHaveBeenCalled();
    });

    it('should call setUser', () => {
      expect(SentryConfig.setUser).toHaveBeenCalled();
    });

    it('should not call setUser if there is no current user ID', () => {
      SentryConfig.setUser.mockClear();
      options.currentUserId = undefined;

      SentryConfig.init(options);

      expect(SentryConfig.setUser).not.toHaveBeenCalled();
    });
  });

  describe('configure', () => {
    const sentryConfig = {};
    const options = {
      dsn: 'https://123@sentry.gitlab.test/123',
      whitelistUrls: ['//gitlabUrl', 'webpack-internal://'],
      environment: 'test',
      release: 'revision',
      tags: {
        revision: 'revision',
      },
    };

    beforeEach(() => {
      jest.spyOn(Sentry, 'init').mockImplementation();

      sentryConfig.options = options;
      sentryConfig.IGNORE_ERRORS = 'ignore_errors';
      sentryConfig.BLACKLIST_URLS = 'blacklist_urls';

      SentryConfig.configure.call(sentryConfig);
    });

    it('should call Sentry.init', () => {
      expect(Sentry.init).toHaveBeenCalledWith({
        dsn: options.dsn,
        release: options.release,
        tags: options.tags,
        sampleRate: 0.95,
        whitelistUrls: options.whitelistUrls,
        environment: 'test',
        ignoreErrors: sentryConfig.IGNORE_ERRORS,
        blacklistUrls: sentryConfig.BLACKLIST_URLS,
      });
    });

    it('should set environment from options', () => {
      sentryConfig.options.environment = 'development';

      SentryConfig.configure.call(sentryConfig);

      expect(Sentry.init).toHaveBeenCalledWith({
        dsn: options.dsn,
        release: options.release,
        tags: options.tags,
        sampleRate: 0.95,
        whitelistUrls: options.whitelistUrls,
        environment: 'development',
        ignoreErrors: sentryConfig.IGNORE_ERRORS,
        blacklistUrls: sentryConfig.BLACKLIST_URLS,
      });
    });
  });

  describe('setUser', () => {
    let sentryConfig;

    beforeEach(() => {
      sentryConfig = { options: { currentUserId: 1 } };
      jest.spyOn(Sentry, 'setUser');

      SentryConfig.setUser.call(sentryConfig);
    });

    it('should call .setUser', () => {
      expect(Sentry.setUser).toHaveBeenCalledWith({
        id: sentryConfig.options.currentUserId,
      });
    });
  });

  describe('handleSentryErrors', () => {
    let event;
    let req;
    let config;
    let err;

    beforeEach(() => {
      event = {};
      req = { status: 'status', responseText: 'Unknown response text', statusText: 'statusText' };
      config = { type: 'type', url: 'url', data: 'data' };
      err = {};

      jest.spyOn(Sentry, 'captureMessage');

      SentryConfig.handleSentryErrors(event, req, config, err);
    });

    it('should call Sentry.captureMessage', () => {
      expect(Sentry.captureMessage).toHaveBeenCalledWith(err, {
        extra: {
          type: config.type,
          url: config.url,
          data: config.data,
          status: req.status,
          response: req.responseText,
          error: err,
          event,
        },
      });
    });

    describe('if no err is provided', () => {
      beforeEach(() => {
        SentryConfig.handleSentryErrors(event, req, config);
      });

      it('should use req.statusText as the error value', () => {
        expect(Sentry.captureMessage).toHaveBeenCalledWith(req.statusText, {
          extra: {
            type: config.type,
            url: config.url,
            data: config.data,
            status: req.status,
            response: req.responseText,
            error: req.statusText,
            event,
          },
        });
      });
    });

    describe('if no req.responseText is provided', () => {
      beforeEach(() => {
        req.responseText = undefined;

        SentryConfig.handleSentryErrors(event, req, config, err);
      });

      it('should use `Unknown response text` as the response', () => {
        expect(Sentry.captureMessage).toHaveBeenCalledWith(err, {
          extra: {
            type: config.type,
            url: config.url,
            data: config.data,
            status: req.status,
            response: 'Unknown response text',
            error: err,
            event,
          },
        });
      });
    });
  });
});