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
|
import {
BrowserClient,
defaultStackParser,
makeFetchTransport,
defaultIntegrations,
BrowserTracing,
// exports
captureException,
SDK_VERSION,
} from 'sentrybrowser';
import * as Sentry from 'sentrybrowser';
import { initSentry } from '~/sentry/init_sentry';
const mockDsn = 'https://123@sentry.gitlab.test/123';
const mockEnvironment = 'development';
const mockCurrentUserId = 1;
const mockGitlabUrl = 'https://gitlab.com';
const mockVersion = '1.0.0';
const mockRevision = '00112233';
const mockFeatureCategory = 'my_feature_category';
const mockPage = 'index:page';
const mockSentryClientsideTracesSampleRate = 0.1;
jest.mock('sentrybrowser', () => {
return {
...jest.createMockFromModule('sentrybrowser'),
// unmock actual configuration options
defaultStackParser: jest.requireActual('sentrybrowser').defaultStackParser,
makeFetchTransport: jest.requireActual('sentrybrowser').makeFetchTransport,
defaultIntegrations: jest.requireActual('sentrybrowser').defaultIntegrations,
};
});
describe('SentryConfig', () => {
let mockBindClient;
let mockSetTags;
let mockSetUser;
let mockBrowserClient;
let mockStartSession;
let mockCaptureSession;
beforeEach(() => {
window.gon = {
sentry_dsn: mockDsn,
sentry_environment: mockEnvironment,
current_user_id: mockCurrentUserId,
gitlab_url: mockGitlabUrl,
version: mockVersion,
revision: mockRevision,
feature_category: mockFeatureCategory,
sentry_clientside_traces_sample_rate: mockSentryClientsideTracesSampleRate,
};
document.body.dataset.page = mockPage;
mockBindClient = jest.fn();
mockSetTags = jest.fn();
mockSetUser = jest.fn();
mockStartSession = jest.fn();
mockCaptureSession = jest.fn();
mockBrowserClient = jest.spyOn(Sentry, 'BrowserClient');
jest.spyOn(Sentry, 'getCurrentHub').mockReturnValue({
bindClient: mockBindClient,
setTags: mockSetTags,
setUser: mockSetUser,
startSession: mockStartSession,
captureSession: mockCaptureSession,
});
});
afterEach(() => {
// eslint-disable-next-line no-underscore-dangle
window._Sentry = undefined;
});
describe('initSentry', () => {
describe('when sentry is initialized', () => {
beforeEach(() => {
initSentry();
});
it('creates BrowserClient with gon values and configuration', () => {
expect(mockBrowserClient).toHaveBeenCalledWith(
expect.objectContaining({
dsn: mockDsn,
release: mockRevision,
allowUrls: [mockGitlabUrl, 'webpack-internal://'],
environment: mockEnvironment,
tracesSampleRate: mockSentryClientsideTracesSampleRate,
tracePropagationTargets: [/^\//],
transport: makeFetchTransport,
stackParser: defaultStackParser,
integrations: [...defaultIntegrations, expect.any(BrowserTracing)],
}),
);
});
it('Uses data-page to set BrowserTracing transaction name', () => {
const context = BrowserTracing.mock.calls[0][0].beforeNavigate();
expect(context).toMatchObject({ name: mockPage });
});
it('binds the BrowserClient to the hub', () => {
expect(mockBindClient).toHaveBeenCalledTimes(1);
expect(mockBindClient).toHaveBeenCalledWith(expect.any(BrowserClient));
});
it('calls Sentry.setTags with gon values', () => {
expect(mockSetTags).toHaveBeenCalledTimes(1);
expect(mockSetTags).toHaveBeenCalledWith({
page: mockPage,
version: mockVersion,
feature_category: mockFeatureCategory,
});
});
it('calls Sentry.setUser with gon values', () => {
expect(mockSetUser).toHaveBeenCalledTimes(1);
expect(mockSetUser).toHaveBeenCalledWith({
id: mockCurrentUserId,
});
});
it('sets global sentry', () => {
// eslint-disable-next-line no-underscore-dangle
expect(window._Sentry).toEqual({
captureException,
SDK_VERSION,
});
});
});
describe('when user is not logged in', () => {
beforeEach(() => {
window.gon.current_user_id = undefined;
initSentry();
});
it('does not call Sentry.setUser', () => {
expect(mockSetUser).not.toHaveBeenCalled();
});
});
describe('when gon is not defined', () => {
beforeEach(() => {
window.gon = undefined;
initSentry();
});
it('Sentry.init is not called', () => {
expect(mockBrowserClient).not.toHaveBeenCalled();
expect(mockBindClient).not.toHaveBeenCalled();
// eslint-disable-next-line no-underscore-dangle
expect(window._Sentry).toBe(undefined);
});
});
describe('when dsn is not configured', () => {
beforeEach(() => {
window.gon.sentry_dsn = undefined;
initSentry();
});
it('Sentry.init is not called', () => {
expect(mockBrowserClient).not.toHaveBeenCalled();
expect(mockBindClient).not.toHaveBeenCalled();
// eslint-disable-next-line no-underscore-dangle
expect(window._Sentry).toBe(undefined);
});
});
describe('when data-page is not defined in the body', () => {
beforeEach(() => {
delete document.body.dataset.page;
initSentry();
});
it('calls Sentry.setTags with gon values', () => {
expect(mockSetTags).toHaveBeenCalledTimes(1);
expect(mockSetTags).toHaveBeenCalledWith(
expect.objectContaining({
page: undefined,
}),
);
});
it('Uses location.path to set BrowserTracing transaction name', () => {
const context = BrowserTracing.mock.calls[0][0].beforeNavigate({ op: 'pageload' });
expect(context).toEqual({ op: 'pageload', name: window.location.pathname });
});
});
});
});
|