Skip to content

Commit 1172281

Browse files
committed
add tests
1 parent 43baead commit 1172281

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed
Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
import {
2+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
3+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4+
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
5+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
6+
getActiveSpan,
7+
getCurrentScope,
8+
setCurrentClient,
9+
spanIsSampled,
10+
spanToJSON,
11+
} from '@sentry/core';
12+
import { JSDOM } from 'jsdom';
13+
import { browserTracingIntegration, startBrowserTracingNavigationSpan, startBrowserTracingPageLoadSpan } from '../..';
14+
import { WINDOW } from '../../src/browser/types';
15+
import { TestClient, getDefaultClientOptions } from '../utils/TestClient';
16+
17+
function setToUrl(url: string): void {}
18+
19+
// We're setting up JSDom here because the Next.js routing instrumentations requires a few things to be present on pageload:
20+
// 1. Access to window.document API for `window.document.getElementById`
21+
// 2. Access to window.location API for `window.location.pathname`
22+
23+
const dom = new JSDOM(undefined, { url: 'https://example.com/' });
24+
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
25+
Object.defineProperty(global, 'location', { value: dom.window.document.location, writable: true });
26+
Object.defineProperty(global, 'history', { value: dom.window.history, writable: true });
27+
28+
const originalGlobalDocument = WINDOW.document;
29+
const originalGlobalLocation = WINDOW.location;
30+
const originalGlobalHistory = WINDOW.history;
31+
afterAll(() => {
32+
// Clean up JSDom
33+
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
34+
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
35+
Object.defineProperty(WINDOW, 'history', { value: originalGlobalHistory });
36+
});
37+
38+
describe('browserTracingIntegration', () => {
39+
afterEach(() => {
40+
getCurrentScope().clear();
41+
});
42+
43+
it('works with tracing enabled', () => {
44+
const client = new TestClient(
45+
getDefaultClientOptions({
46+
tracesSampleRate: 1,
47+
integrations: [browserTracingIntegration()],
48+
}),
49+
);
50+
setCurrentClient(client);
51+
client.init();
52+
53+
const span = getActiveSpan();
54+
expect(span).toBeDefined();
55+
expect(spanIsSampled(span!)).toBe(true);
56+
expect(spanToJSON(span!)).toEqual({
57+
description: '/',
58+
op: 'pageload',
59+
origin: 'auto.pageload.browser',
60+
data: {
61+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
62+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.browser',
63+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
64+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
65+
},
66+
span_id: expect.any(String),
67+
start_timestamp: expect.any(Number),
68+
trace_id: expect.any(String),
69+
});
70+
});
71+
72+
it('works with tracing disabled', () => {
73+
const client = new TestClient(
74+
getDefaultClientOptions({
75+
integrations: [browserTracingIntegration()],
76+
}),
77+
);
78+
setCurrentClient(client);
79+
client.init();
80+
81+
const span = getActiveSpan();
82+
expect(span).toBeDefined();
83+
expect(spanIsSampled(span!)).toBe(false);
84+
});
85+
86+
it('works with tracing enabled but unsampled', () => {
87+
const client = new TestClient(
88+
getDefaultClientOptions({
89+
tracesSampleRate: 0,
90+
integrations: [browserTracingIntegration()],
91+
}),
92+
);
93+
setCurrentClient(client);
94+
client.init();
95+
96+
const span = getActiveSpan();
97+
expect(span).toBeDefined();
98+
expect(spanIsSampled(span!)).toBe(false);
99+
});
100+
101+
it('starts navigation when URL changes', () => {
102+
const client = new TestClient(
103+
getDefaultClientOptions({
104+
tracesSampleRate: 1,
105+
integrations: [browserTracingIntegration()],
106+
}),
107+
);
108+
setCurrentClient(client);
109+
client.init();
110+
111+
const span = getActiveSpan();
112+
expect(span).toBeDefined();
113+
expect(spanIsSampled(span!)).toBe(true);
114+
expect(span!.isRecording()).toBe(true);
115+
expect(spanToJSON(span!)).toEqual({
116+
description: '/',
117+
op: 'pageload',
118+
origin: 'auto.pageload.browser',
119+
data: {
120+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
121+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.browser',
122+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
123+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
124+
},
125+
span_id: expect.any(String),
126+
start_timestamp: expect.any(Number),
127+
trace_id: expect.any(String),
128+
});
129+
130+
// this is what is used to get the span name - JSDOM does not update this on it's own!
131+
const dom = new JSDOM(undefined, { url: 'https://example.com/test' });
132+
Object.defineProperty(global, 'location', { value: dom.window.document.location, writable: true });
133+
134+
WINDOW.history.pushState({}, '', '/test');
135+
136+
expect(span!.isRecording()).toBe(false);
137+
138+
const span2 = getActiveSpan();
139+
expect(span2).toBeDefined();
140+
expect(spanIsSampled(span2!)).toBe(true);
141+
expect(span2!.isRecording()).toBe(true);
142+
expect(spanToJSON(span2!)).toEqual({
143+
description: '/test',
144+
op: 'navigation',
145+
origin: 'auto.navigation.browser',
146+
data: {
147+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
148+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.browser',
149+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
150+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
151+
},
152+
span_id: expect.any(String),
153+
start_timestamp: expect.any(Number),
154+
trace_id: expect.any(String),
155+
});
156+
157+
// this is what is used to get the span name - JSDOM does not update this on it's own!
158+
const dom2 = new JSDOM(undefined, { url: 'https://example.com/test2' });
159+
Object.defineProperty(global, 'location', { value: dom2.window.document.location, writable: true });
160+
161+
WINDOW.history.pushState({}, '', '/test2');
162+
163+
expect(span2!.isRecording()).toBe(false);
164+
165+
const span3 = getActiveSpan();
166+
expect(span3).toBeDefined();
167+
expect(spanIsSampled(span3!)).toBe(true);
168+
expect(span3!.isRecording()).toBe(true);
169+
expect(spanToJSON(span3!)).toEqual({
170+
description: '/test2',
171+
op: 'navigation',
172+
origin: 'auto.navigation.browser',
173+
data: {
174+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
175+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.browser',
176+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
177+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
178+
},
179+
span_id: expect.any(String),
180+
start_timestamp: expect.any(Number),
181+
trace_id: expect.any(String),
182+
});
183+
});
184+
185+
describe('startBrowserTracingPageLoadSpan', () => {
186+
it('works without integration setup', () => {
187+
const client = new TestClient(
188+
getDefaultClientOptions({
189+
integrations: [],
190+
}),
191+
);
192+
setCurrentClient(client);
193+
client.init();
194+
195+
const span = startBrowserTracingPageLoadSpan(client, { name: 'test span' });
196+
197+
expect(span).toBeUndefined();
198+
});
199+
200+
it('works with unsampled span', () => {
201+
const client = new TestClient(
202+
getDefaultClientOptions({
203+
tracesSampleRate: 0,
204+
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
205+
}),
206+
);
207+
setCurrentClient(client);
208+
client.init();
209+
210+
const span = startBrowserTracingPageLoadSpan(client, { name: 'test span' });
211+
212+
expect(span).toBeDefined();
213+
expect(spanIsSampled(span!)).toBe(false);
214+
});
215+
216+
it('works with integration setup', () => {
217+
const client = new TestClient(
218+
getDefaultClientOptions({
219+
tracesSampleRate: 1,
220+
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
221+
}),
222+
);
223+
setCurrentClient(client);
224+
client.init();
225+
226+
const span = startBrowserTracingPageLoadSpan(client, { name: 'test span' });
227+
228+
expect(span).toBeDefined();
229+
expect(spanToJSON(span!)).toEqual({
230+
description: 'test span',
231+
op: 'pageload',
232+
origin: 'manual',
233+
data: {
234+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
235+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'manual',
236+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
237+
},
238+
span_id: expect.any(String),
239+
start_timestamp: expect.any(Number),
240+
trace_id: expect.any(String),
241+
});
242+
expect(spanIsSampled(span!)).toBe(true);
243+
});
244+
245+
it('allows to overwrite properties', () => {
246+
const client = new TestClient(
247+
getDefaultClientOptions({
248+
tracesSampleRate: 1,
249+
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
250+
}),
251+
);
252+
setCurrentClient(client);
253+
client.init();
254+
255+
const span = startBrowserTracingPageLoadSpan(client, {
256+
name: 'test span',
257+
origin: 'auto.test',
258+
attributes: { testy: 'yes' },
259+
});
260+
261+
expect(span).toBeDefined();
262+
expect(spanToJSON(span!)).toEqual({
263+
description: 'test span',
264+
op: 'pageload',
265+
origin: 'auto.test',
266+
data: {
267+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
268+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.test',
269+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
270+
testy: 'yes',
271+
},
272+
span_id: expect.any(String),
273+
start_timestamp: expect.any(Number),
274+
trace_id: expect.any(String),
275+
});
276+
});
277+
});
278+
279+
describe('startBrowserTracingNavigationSpan', () => {
280+
it('works without integration setup', () => {
281+
const client = new TestClient(
282+
getDefaultClientOptions({
283+
integrations: [],
284+
}),
285+
);
286+
setCurrentClient(client);
287+
client.init();
288+
289+
const span = startBrowserTracingNavigationSpan(client, { name: 'test span' });
290+
291+
expect(span).toBeUndefined();
292+
});
293+
294+
it('works with unsampled span', () => {
295+
const client = new TestClient(
296+
getDefaultClientOptions({
297+
tracesSampleRate: 0,
298+
integrations: [browserTracingIntegration({ instrumentNavigation: false })],
299+
}),
300+
);
301+
setCurrentClient(client);
302+
client.init();
303+
304+
const span = startBrowserTracingNavigationSpan(client, { name: 'test span' });
305+
306+
expect(span).toBeDefined();
307+
expect(spanIsSampled(span!)).toBe(false);
308+
});
309+
310+
it('works with integration setup', () => {
311+
const client = new TestClient(
312+
getDefaultClientOptions({
313+
tracesSampleRate: 1,
314+
integrations: [browserTracingIntegration({ instrumentNavigation: false })],
315+
}),
316+
);
317+
setCurrentClient(client);
318+
client.init();
319+
320+
const span = startBrowserTracingNavigationSpan(client, { name: 'test span' });
321+
322+
expect(span).toBeDefined();
323+
expect(spanToJSON(span!)).toEqual({
324+
description: 'test span',
325+
op: 'navigation',
326+
origin: 'manual',
327+
data: {
328+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
329+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'manual',
330+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
331+
},
332+
span_id: expect.any(String),
333+
start_timestamp: expect.any(Number),
334+
trace_id: expect.any(String),
335+
});
336+
expect(spanIsSampled(span!)).toBe(true);
337+
});
338+
339+
it('allows to overwrite properties', () => {
340+
const client = new TestClient(
341+
getDefaultClientOptions({
342+
tracesSampleRate: 1,
343+
integrations: [browserTracingIntegration({ instrumentNavigation: false })],
344+
}),
345+
);
346+
setCurrentClient(client);
347+
client.init();
348+
349+
const span = startBrowserTracingNavigationSpan(client, {
350+
name: 'test span',
351+
origin: 'auto.test',
352+
attributes: { testy: 'yes' },
353+
});
354+
355+
expect(span).toBeDefined();
356+
expect(spanToJSON(span!)).toEqual({
357+
description: 'test span',
358+
op: 'navigation',
359+
origin: 'auto.test',
360+
data: {
361+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
362+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.test',
363+
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
364+
testy: 'yes',
365+
},
366+
span_id: expect.any(String),
367+
start_timestamp: expect.any(Number),
368+
trace_id: expect.any(String),
369+
});
370+
});
371+
});
372+
});

0 commit comments

Comments
 (0)