-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsdk.test.ts
107 lines (87 loc) · 3.55 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
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { BrowserClient } from '@sentry/browser';
import {
browserTracingIntegration,
getActiveSpan,
getCurrentScope,
getGlobalScope,
getIsolationScope,
} from '@sentry/browser';
import * as SentryBrowser from '@sentry/browser';
import { SDK_VERSION, getClient } from '@sentry/browser';
import { init } from '../../src/client/sdk';
const browserInit = vi.spyOn(SentryBrowser, 'init');
describe('Sentry client SDK', () => {
describe('init', () => {
afterEach(() => {
vi.clearAllMocks();
getCurrentScope().clear();
getCurrentScope().setClient(undefined);
getIsolationScope().clear();
getGlobalScope().clear();
});
it('adds Astro metadata to the SDK options', () => {
expect(browserInit).not.toHaveBeenCalled();
init({});
expect(browserInit).toHaveBeenCalledTimes(1);
expect(browserInit).toHaveBeenCalledWith(
expect.objectContaining({
_metadata: {
sdk: {
name: 'sentry.javascript.astro',
version: SDK_VERSION,
packages: [
{ name: 'npm:@sentry/astro', version: SDK_VERSION },
{ name: 'npm:@sentry/browser', version: SDK_VERSION },
],
},
},
}),
);
});
describe('automatically adds integrations', () => {
it.each([
['tracesSampleRate', { tracesSampleRate: 0 }],
['tracesSampler', { tracesSampler: () => 1.0 }],
['no tracing option set', {}],
])('adds browserTracingIntegration if tracing is enabled via %s', (_, tracingOptions) => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
...tracingOptions,
});
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations;
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
expect(browserTracing).toBeDefined();
});
it("doesn't add browserTracingIntegration if `__SENTRY_TRACING__` is set to false", () => {
(globalThis as any).__SENTRY_TRACING__ = false;
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
tracesSampleRate: 1,
});
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations || [];
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
expect(browserTracing).toBeUndefined();
delete (globalThis as any).__SENTRY_TRACING__;
});
it('Overrides the automatically default browserTracingIntegration instance with a a user-provided browserTracingIntegration instance', () => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [
browserTracingIntegration({ finalTimeout: 10, instrumentNavigation: false, instrumentPageLoad: false }),
],
tracesSampleRate: 1,
});
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
expect(browserTracing).toBeDefined();
// no active span means the settings were respected
expect(getActiveSpan()).toBeUndefined();
});
});
it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
});