-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsdk.test.ts
344 lines (264 loc) · 11.2 KB
/
sdk.test.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* @vitest-environment jsdom
*/
/* eslint-disable @typescript-eslint/unbound-method */
import type { Mock } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi, afterAll, test } from 'vitest';
import * as SentryCore from '@sentry/core';
import { createTransport } from '@sentry/core';
import { resolvedSyncPromise } from '@sentry/core';
import type { Integration } from '@sentry/core';
import type { BrowserOptions } from '../src';
import { WINDOW } from '../src';
import { applyDefaultOptions, getDefaultIntegrations, init } from '../src/sdk';
const PUBLIC_DSN = 'https://username@domain/123';
function getDefaultBrowserOptions(options: Partial<BrowserOptions> = {}): BrowserOptions {
return {
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => resolvedSyncPromise({})),
stackParser: () => [],
...options,
};
}
export class MockIntegration implements Integration {
public name: string;
public setupOnce: () => void = vi.fn();
public constructor(name: string) {
this.name = name;
}
}
describe('init', () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterAll(() => {
vi.resetAllMocks();
});
test('installs default integrations', () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 0.1'),
new MockIntegration('MockIntegration 0.2'),
];
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS });
init(options);
expect(DEFAULT_INTEGRATIONS[0]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
});
it('installs default integrations if `defaultIntegrations: undefined`', () => {
// @ts-expect-error this is fine for testing
const initAndBindSpy = vi.spyOn(SentryCore, 'initAndBind').mockImplementationOnce(() => {});
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: undefined });
init(options);
expect(initAndBindSpy).toHaveBeenCalledTimes(1);
const optionsPassed = initAndBindSpy.mock.calls[0]?.[1];
expect(optionsPassed?.integrations.length).toBeGreaterThan(0);
});
test("doesn't install default integrations if told not to", () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 0.3'),
new MockIntegration('MockIntegration 0.4'),
];
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: false });
init(options);
expect(DEFAULT_INTEGRATIONS[0]!.setupOnce as Mock).toHaveBeenCalledTimes(0);
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(0);
});
it('installs merged default integrations, with overrides provided through options', () => {
const DEFAULT_INTEGRATIONS = [
new MockIntegration('MockIntegration 1.1'),
new MockIntegration('MockIntegration 1.2'),
];
const integrations = [new MockIntegration('MockIntegration 1.1'), new MockIntegration('MockIntegration 1.3')];
const options = getDefaultBrowserOptions({
dsn: PUBLIC_DSN,
defaultIntegrations: DEFAULT_INTEGRATIONS,
integrations,
});
init(options);
// 'MockIntegration 1' should be overridden by the one with the same name provided through options
expect(DEFAULT_INTEGRATIONS[0]!.setupOnce as Mock).toHaveBeenCalledTimes(0);
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
expect(integrations[0]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
expect(integrations[1]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
});
it('installs integrations returned from a callback function', () => {
const DEFAULT_INTEGRATIONS = [
new MockIntegration('MockIntegration 2.1'),
new MockIntegration('MockIntegration 2.2'),
];
const newIntegration = new MockIntegration('MockIntegration 2.3');
const options = getDefaultBrowserOptions({
defaultIntegrations: DEFAULT_INTEGRATIONS,
dsn: PUBLIC_DSN,
integrations: (integrations: Integration[]) => {
const t = integrations.slice(0, 1).concat(newIntegration);
return t;
},
});
init(options);
expect(DEFAULT_INTEGRATIONS[0]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
expect(newIntegration.setupOnce as Mock).toHaveBeenCalledTimes(1);
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(0);
});
describe('initialization error in browser extension', () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 0.1'),
new MockIntegration('MockIntegration 0.2'),
];
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS });
afterEach(() => {
Object.defineProperty(WINDOW, 'chrome', { value: undefined, writable: true });
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true });
Object.defineProperty(WINDOW, 'nw', { value: undefined, writable: true });
Object.defineProperty(WINDOW, 'window', { value: WINDOW, writable: true });
vi.clearAllMocks();
});
it('logs a browser extension error if executed inside a Chrome extension', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
Object.defineProperty(WINDOW, 'chrome', {
value: { runtime: { id: 'mock-extension-id' } },
writable: true,
});
init(options);
expect(consoleErrorSpy).toBeCalledTimes(1);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);
consoleErrorSpy.mockRestore();
});
it('logs a browser extension error if executed inside a Firefox/Safari extension', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
Object.defineProperty(WINDOW, 'browser', { value: { runtime: { id: 'mock-extension-id' } }, writable: true });
init(options);
expect(consoleErrorSpy).toBeCalledTimes(1);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);
consoleErrorSpy.mockRestore();
});
it.each(['chrome-extension', 'moz-extension', 'ms-browser-extension', 'safari-web-extension'])(
"doesn't log a browser extension error if executed inside an extension running in a dedicated page (%s)",
extensionProtocol => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const locationHrefSpy = vi
.spyOn(SentryCore, 'getLocationHref')
.mockImplementation(() => `${extensionProtocol}://mock-extension-id/dedicated-page.html`);
Object.defineProperty(WINDOW, 'browser', { value: { runtime: { id: 'mock-extension-id' } }, writable: true });
init(options);
expect(consoleErrorSpy).toBeCalledTimes(0);
consoleErrorSpy.mockRestore();
locationHrefSpy.mockRestore();
},
);
it("doesn't log a browser extension error if executed inside regular browser environment", () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
init(options);
expect(consoleErrorSpy).toBeCalledTimes(0);
consoleErrorSpy.mockRestore();
});
it("doesn't log a browser extension error if executed inside an NW.js environment", () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
Object.defineProperty(WINDOW, 'nw', { value: {} });
init(options);
expect(consoleErrorSpy).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
it("doesn't log a browser extension error if the `window` object isn't defined", () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
Object.defineProperty(WINDOW, 'window', { value: undefined });
init(options);
expect(consoleErrorSpy).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
it("doesn't return a client on initialization error", () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
Object.defineProperty(WINDOW, 'chrome', {
value: { runtime: { id: 'mock-extension-id' } },
writable: true,
});
const client = init(options);
expect(client).toBeUndefined();
consoleErrorSpy.mockRestore();
});
});
it('returns a client from init', () => {
const client = init();
expect(client).not.toBeUndefined();
});
});
describe('applyDefaultOptions', () => {
test('it works with empty options', () => {
const options = {};
const actual = applyDefaultOptions(options);
expect(actual).toEqual({
defaultIntegrations: expect.any(Array),
release: undefined,
sendClientReports: true,
});
expect((actual.defaultIntegrations as { name: string }[]).map(i => i.name)).toEqual(
getDefaultIntegrations(options).map(i => i.name),
);
});
test('it works with options', () => {
const options = {
tracesSampleRate: 0.5,
release: '1.0.0',
};
const actual = applyDefaultOptions(options);
expect(actual).toEqual({
defaultIntegrations: expect.any(Array),
release: '1.0.0',
sendClientReports: true,
tracesSampleRate: 0.5,
});
expect((actual.defaultIntegrations as { name: string }[]).map(i => i.name)).toEqual(
getDefaultIntegrations(options).map(i => i.name),
);
});
test('it works with defaultIntegrations=false', () => {
const options = {
defaultIntegrations: false,
} as const;
const actual = applyDefaultOptions(options);
expect(actual.defaultIntegrations).toStrictEqual(false);
});
test('it works with defaultIntegrations=[]', () => {
const options = {
defaultIntegrations: [],
};
const actual = applyDefaultOptions(options);
expect(actual.defaultIntegrations).toEqual([]);
});
test('it works with tracesSampleRate=undefined', () => {
const options = {
tracesSampleRate: undefined,
} as const;
const actual = applyDefaultOptions(options);
// Not defined, not even undefined
expect('tracesSampleRate' in actual).toBe(false);
});
test('it works with tracesSampleRate=null', () => {
const options = {
tracesSampleRate: null,
} as any;
const actual = applyDefaultOptions(options);
expect(actual.tracesSampleRate).toStrictEqual(null);
});
test('it works with tracesSampleRate=0', () => {
const options = {
tracesSampleRate: 0,
} as const;
const actual = applyDefaultOptions(options);
expect(actual.tracesSampleRate).toStrictEqual(0);
});
test('it does not deep-drop undefined keys', () => {
const options = {
obj: {
prop: undefined,
},
} as any;
const actual = applyDefaultOptions(options) as any;
expect('prop' in actual.obj).toBe(true);
expect(actual.obj.prop).toStrictEqual(undefined);
});
});