Skip to content

Commit 4fbc911

Browse files
committed
add tests
1 parent 75ad198 commit 4fbc911

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

packages/sveltekit/src/client/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ export function init(options: BrowserOptions): void {
2828
function addClientIntegrations(options: BrowserOptions): void {
2929
let integrations = options.integrations || [];
3030

31-
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
32-
// will get treeshaken away
31+
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false",
32+
// in which case everything inside will get treeshaken away
3333
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
3434
if (hasTracingEnabled(options)) {
3535
const defaultBrowserTracingIntegration = new BrowserTracing({
36-
tracePropagationTargets: [...defaultRequestInstrumentationOptions.tracePropagationTargets, /^(api\/)/],
36+
tracePropagationTargets: [...defaultRequestInstrumentationOptions.tracePropagationTargets],
3737
// TODO: Add SvelteKit router instrumentations
3838
// routingInstrumentation: sveltekitRoutingInstrumentation,
3939
});

packages/sveltekit/test/client/sdk.test.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { getCurrentHub } from '@sentry/core';
2+
import type { BrowserClient } from '@sentry/svelte';
23
import * as SentrySvelte from '@sentry/svelte';
34
import { SDK_VERSION, WINDOW } from '@sentry/svelte';
45
import { vi } from 'vitest';
56

6-
import { init } from '../../src/client/sdk';
7+
import { BrowserTracing, init } from '../../src/client';
78

89
const svelteInit = vi.spyOn(SentrySvelte, 'init');
910

@@ -47,5 +48,79 @@ describe('Sentry client SDK', () => {
4748
// @ts-ignore need access to protected _tags attribute
4849
expect(currentScope._tags).toEqual({ runtime: 'browser' });
4950
});
51+
52+
describe('automatically added integrations', () => {
53+
it.each([
54+
['tracesSampleRate', { tracesSampleRate: 0 }],
55+
['tracesSampler', { tracesSampler: () => 1.0 }],
56+
['enableTracing', { enableTracing: true }],
57+
])('adds the BrowserTracing integration if tracing is enabled via %s', (_, tracingOptions) => {
58+
init({
59+
dsn: 'https://[email protected]/1337',
60+
...tracingOptions,
61+
});
62+
63+
const integrationsToInit = svelteInit.mock.calls[0][0].integrations;
64+
const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing');
65+
66+
expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
67+
expect(browserTracing).toBeDefined();
68+
});
69+
70+
it('merges the BrowserTracing integration with the user-provided one', () => {});
71+
});
72+
73+
it.each([
74+
['enableTracing', { enableTracing: false }],
75+
['no tracing option set', {}],
76+
])("doesn't add the BrowserTracing integration if tracing is disabled via %s", (_, tracingOptions) => {
77+
init({
78+
dsn: 'https://[email protected]/1337',
79+
...tracingOptions,
80+
});
81+
82+
const integrationsToInit = svelteInit.mock.calls[0][0].integrations;
83+
const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing');
84+
85+
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
86+
expect(browserTracing).toBeUndefined();
87+
});
88+
89+
it("doesn't add the BrowserTracing integration if `__SENTRY_TRACING__` is set to false", () => {
90+
// This is the closest we can get to unit-testing the `__SENTRY_TRACING__` tree-shaking guard
91+
// IRL, the code to add the integration would most likely be removed by the bundler.
92+
93+
globalThis.__SENTRY_TRACING__ = false;
94+
95+
init({
96+
dsn: 'https://[email protected]/1337',
97+
enableTracing: true,
98+
});
99+
100+
const integrationsToInit = svelteInit.mock.calls[0][0].integrations;
101+
const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing');
102+
103+
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
104+
expect(browserTracing).toBeUndefined();
105+
106+
delete globalThis.__SENTRY_TRACING__;
107+
});
108+
109+
// TODO: this test is only meaningful once we have a routing instrumentation which we always want to add
110+
// to a user-provided BrowserTracing integration (see NextJS SDK)
111+
it.skip('Merges the user-provided BrowserTracing integration with the automatically added one', () => {
112+
init({
113+
dsn: 'https://[email protected]/1337',
114+
integrations: [new BrowserTracing({ tracePropagationTargets: ['myDomain.com'] })],
115+
enableTracing: true,
116+
});
117+
118+
const integrationsToInit = svelteInit.mock.calls[0][0].integrations;
119+
const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing');
120+
121+
expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
122+
expect(browserTracing).toBeDefined();
123+
expect((browserTracing as BrowserTracing).options.tracePropagationTargets).toEqual(['myDomain.com']);
124+
});
50125
});
51126
});

0 commit comments

Comments
 (0)