Skip to content

Commit c62591f

Browse files
committed
fix(nextjs|sveltekit): Ensure we can pass browserTracingIntegration
more improvements add one e2e test add test for beforestartspan
1 parent cfaa9be commit c62591f

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

dev-packages/e2e-tests/test-applications/nextjs-14/sentry.client.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ Sentry.init({
66
tunnel: `http://localhost:3031/`, // proxy server
77
tracesSampleRate: 1.0,
88
sendDefaultPii: true,
9+
10+
// Ensure it also works with passing the integration
11+
integrations: [Sentry.browserTracingIntegration()],
912
});

packages/nextjs/src/client/browserTracingIntegration.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
startBrowserTracingNavigationSpan,
66
startBrowserTracingPageLoadSpan,
77
} from '@sentry/react';
8-
import type { Integration, StartSpanOptions } from '@sentry/types';
8+
import type { StartSpanOptions } from '@sentry/types';
99
import { nextRouterInstrumentation } from '../index.client';
1010

1111
/**
@@ -42,7 +42,7 @@ export class BrowserTracing extends OriginalBrowserTracing {
4242
*/
4343
export function browserTracingIntegration(
4444
options?: Parameters<typeof originalBrowserTracingIntegration>[0],
45-
): Integration {
45+
): ReturnType<typeof originalBrowserTracingIntegration> {
4646
const browserTracingIntegrationInstance = originalBrowserTracingIntegration({
4747
// eslint-disable-next-line deprecation/deprecation
4848
tracingOrigins:
@@ -61,8 +61,16 @@ export function browserTracingIntegration(
6161
instrumentPageLoad: false,
6262
});
6363

64+
const fullOptions = {
65+
...browserTracingIntegrationInstance.options,
66+
instrumentPageLoad: true,
67+
instrumentNavigation: true,
68+
...options,
69+
};
70+
6471
return {
6572
...browserTracingIntegrationInstance,
73+
options: fullOptions,
6674
afterAllSetup(client) {
6775
const startPageloadCallback = (startSpanOptions: StartSpanOptions): void => {
6876
startBrowserTracingPageLoadSpan(client, startSpanOptions);
@@ -80,7 +88,7 @@ export function browserTracingIntegration(
8088
nextRouterInstrumentation(
8189
() => undefined,
8290
false,
83-
options?.instrumentNavigation,
91+
fullOptions.instrumentNavigation,
8492
startPageloadCallback,
8593
startNavigationCallback,
8694
);
@@ -90,7 +98,7 @@ export function browserTracingIntegration(
9098
// eslint-disable-next-line deprecation/deprecation
9199
nextRouterInstrumentation(
92100
() => undefined,
93-
options?.instrumentPageLoad,
101+
fullOptions.instrumentPageLoad,
94102
false,
95103
startPageloadCallback,
96104
startNavigationCallback,

packages/nextjs/test/clientSdk.test.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { BaseClient, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getActiveSpan, spanToJSON } from '@sentry/core';
1+
import {
2+
BaseClient,
3+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4+
getActiveSpan,
5+
getIsolationScope,
6+
spanToJSON,
7+
} from '@sentry/core';
28
import * as SentryReact from '@sentry/react';
39
import type { BrowserClient } from '@sentry/react';
410
import { browserTracingIntegration } from '@sentry/react';
@@ -35,6 +41,11 @@ function findIntegrationByName(integrations: Integration[] = [], name: string):
3541
const TEST_DSN = 'https://[email protected]/1337';
3642

3743
describe('Client init()', () => {
44+
beforeEach(() => {
45+
getCurrentScope().clear();
46+
getIsolationScope().clear();
47+
});
48+
3849
afterEach(() => {
3950
jest.clearAllMocks();
4051
WINDOW.__SENTRY__.hub = undefined;
@@ -141,9 +152,16 @@ describe('Client init()', () => {
141152
});
142153

143154
it('forces correct router instrumentation if user provides `browserTracingIntegration`', () => {
155+
const beforeStartSpan = jest.fn(options => options);
144156
init({
145157
dsn: TEST_DSN,
146-
integrations: [browserTracingIntegration({ finalTimeout: 10 })],
158+
integrations: [
159+
browserTracingIntegration({
160+
finalTimeout: 10,
161+
instrumentNavigation: false,
162+
beforeStartSpan,
163+
}),
164+
],
147165
enableTracing: true,
148166
});
149167

@@ -158,6 +176,16 @@ describe('Client init()', () => {
158176

159177
// This shows that the user-configured options are still here
160178
expect(integration?.options?.finalTimeout).toEqual(10);
179+
expect(integration?.options.instrumentNavigation).toBe(false);
180+
expect(integration?.options.instrumentPageLoad).toBe(true);
181+
182+
expect(beforeStartSpan).toHaveBeenCalledTimes(1);
183+
expect(beforeStartSpan).toHaveBeenCalledWith(
184+
expect.objectContaining({
185+
name: '/',
186+
op: 'pageload',
187+
}),
188+
);
161189

162190
// it is the svelte kit variety
163191
expect(getActiveSpan()).toBeDefined();
@@ -187,10 +215,10 @@ describe('Client init()', () => {
187215

188216
expect(browserTracingIntegration?.options).toEqual(
189217
expect.objectContaining({
218+
startTransactionOnPageLoad: true,
219+
startTransactionOnLocationChange: false,
190220
// eslint-disable-next-line deprecation/deprecation
191221
routingInstrumentation: nextRouterInstrumentation,
192-
// This proves it's still the user's copy
193-
startTransactionOnLocationChange: false,
194222
}),
195223
);
196224
});

packages/sveltekit/src/client/browserTracingIntegration.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
startBrowserTracingPageLoadSpan,
1010
startInactiveSpan,
1111
} from '@sentry/svelte';
12-
import type { Client, Integration, Span } from '@sentry/types';
12+
import type { Client, Span } from '@sentry/types';
1313
import { svelteKitRoutingInstrumentation } from './router';
1414

1515
/**
@@ -36,7 +36,7 @@ export class BrowserTracing extends OriginalBrowserTracing {
3636
*/
3737
export function browserTracingIntegration(
3838
options: Parameters<typeof originalBrowserTracingIntegration>[0] = {},
39-
): Integration {
39+
): ReturnType<typeof originalBrowserTracingIntegration> {
4040
const integration = {
4141
...originalBrowserTracingIntegration({
4242
...options,
@@ -45,16 +45,24 @@ export function browserTracingIntegration(
4545
}),
4646
};
4747

48+
const fullOptions = {
49+
...integration.options,
50+
instrumentPageLoad: true,
51+
instrumentNavigation: true,
52+
...options,
53+
};
54+
4855
return {
4956
...integration,
57+
options: fullOptions,
5058
afterAllSetup: client => {
5159
integration.afterAllSetup(client);
5260

53-
if (options.instrumentPageLoad !== false) {
61+
if (fullOptions.instrumentPageLoad) {
5462
_instrumentPageload(client);
5563
}
5664

57-
if (options.instrumentNavigation !== false) {
65+
if (fullOptions.instrumentNavigation) {
5866
_instrumentNavigations(client);
5967
}
6068
},

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getActiveSpan, getClient, getCurrentScope, spanToJSON } from '@sentry/core';
1+
import {
2+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
3+
getActiveSpan,
4+
getClient,
5+
getCurrentScope,
6+
getIsolationScope,
7+
spanToJSON,
8+
} from '@sentry/core';
29
import type { BrowserClient } from '@sentry/svelte';
310
import * as SentrySvelte from '@sentry/svelte';
411
import { SDK_VERSION, WINDOW, browserTracingIntegration } from '@sentry/svelte';
@@ -16,6 +23,11 @@ describe('Sentry client SDK', () => {
1623
WINDOW.__SENTRY__.hub = undefined;
1724
});
1825

26+
beforeEach(() => {
27+
getCurrentScope().clear();
28+
getIsolationScope().clear();
29+
});
30+
1931
it('adds SvelteKit metadata to the SDK options', () => {
2032
expect(svelteInit).not.toHaveBeenCalled();
2133

@@ -99,7 +111,7 @@ describe('Sentry client SDK', () => {
99111
init({
100112
dsn: 'https://[email protected]/1337',
101113
// eslint-disable-next-line deprecation/deprecation
102-
integrations: [new BrowserTracing({ finalTimeout: 10 })],
114+
integrations: [new BrowserTracing({ finalTimeout: 10, startTransactionOnLocationChange: false })],
103115
enableTracing: true,
104116
});
105117

@@ -118,12 +130,14 @@ describe('Sentry client SDK', () => {
118130
// But we force the routing instrumentation to be ours
119131
// eslint-disable-next-line deprecation/deprecation
120132
expect(options.routingInstrumentation).toEqual(svelteKitRoutingInstrumentation);
133+
expect(options.startTransactionOnPageLoad).toEqual(true);
134+
expect(options.startTransactionOnLocationChange).toEqual(false);
121135
});
122136

123137
it('Merges a user-provided browserTracingIntegration with the automatically added one', () => {
124138
init({
125139
dsn: 'https://[email protected]/1337',
126-
integrations: [browserTracingIntegration({ finalTimeout: 10 })],
140+
integrations: [browserTracingIntegration({ finalTimeout: 10, instrumentNavigation: false })],
127141
enableTracing: true,
128142
});
129143

@@ -140,6 +154,8 @@ describe('Sentry client SDK', () => {
140154

141155
// This shows that the user-configured options are still here
142156
expect(options?.finalTimeout).toEqual(10);
157+
expect(options?.instrumentPageLoad).toEqual(true);
158+
expect(options?.instrumentNavigation).toEqual(false);
143159

144160
// it is the svelte kit variety
145161
expect(getActiveSpan()).toBeDefined();

0 commit comments

Comments
 (0)