Skip to content

Commit 1b7e7f2

Browse files
committed
WIP changed tests
1 parent 816d8a2 commit 1b7e7f2

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

packages/browser/test/tracing/browserTracingIntegration.test.ts

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ import {
2020
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
2121
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
2222
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
23+
SentryNonRecordingSpan,
2324
TRACING_DEFAULTS,
2425
getActiveSpan,
2526
getCurrentScope,
2627
getDynamicSamplingContextFromSpan,
2728
getIsolationScope,
29+
getTraceData,
2830
setCurrentClient,
2931
spanIsSampled,
3032
spanToJSON,
3133
startInactiveSpan,
34+
updateSpanName,
3235
} from '@sentry/core';
3336
import type { Span, StartSpanOptions } from '@sentry/core';
3437
import { JSDOM } from 'jsdom';
@@ -265,6 +268,10 @@ describe('browserTracingIntegration', () => {
265268

266269
expect(span).toBeDefined();
267270
expect(spanIsSampled(span!)).toBe(false);
271+
272+
// Ensure getTraceData is correct in this case
273+
const traceData = getTraceData();
274+
expect(traceData['sentry-trace']).toEqual(`${span?.spanContext().traceId}-${span?.spanContext().spanId}-0`);
268275
});
269276

270277
it('works with integration setup', () => {
@@ -365,7 +372,7 @@ describe('browserTracingIntegration', () => {
365372

366373
const client = new BrowserClient(
367374
getDefaultBrowserClientOptions({
368-
tracesSampleRate: 0,
375+
tracesSampleRate: 1,
369376
integrations: [
370377
browserTracingIntegration({
371378
instrumentPageLoad: false,
@@ -378,9 +385,7 @@ describe('browserTracingIntegration', () => {
378385
setCurrentClient(client);
379386
client.init();
380387

381-
startBrowserTracingPageLoadSpan(client, { name: 'test span' });
382-
383-
const pageloadSpan = getActiveSpan();
388+
const pageloadSpan = startBrowserTracingPageLoadSpan(client, { name: 'test span' });
384389

385390
expect(spanToJSON(pageloadSpan!).op).toBe('test op');
386391
});
@@ -408,7 +413,7 @@ describe('browserTracingIntegration', () => {
408413

409414
const client = new BrowserClient(
410415
getDefaultBrowserClientOptions({
411-
tracesSampleRate: 0,
416+
tracesSampleRate: 1,
412417
integrations: [
413418
browserTracingIntegration({
414419
instrumentPageLoad: false,
@@ -458,6 +463,10 @@ describe('browserTracingIntegration', () => {
458463

459464
expect(span).toBeDefined();
460465
expect(spanIsSampled(span!)).toBe(false);
466+
467+
// Ensure getTraceData is correct in this case
468+
const traceData = getTraceData();
469+
expect(traceData['sentry-trace']).toEqual(`${span?.spanContext().traceId}-${span?.spanContext().spanId}-0`);
461470
});
462471

463472
it('works with integration setup', () => {
@@ -562,7 +571,7 @@ describe('browserTracingIntegration', () => {
562571

563572
const client = new BrowserClient(
564573
getDefaultBrowserClientOptions({
565-
tracesSampleRate: 0,
574+
tracesSampleRate: 1,
566575
integrations: [
567576
browserTracingIntegration({
568577
instrumentPageLoad: false,
@@ -575,9 +584,7 @@ describe('browserTracingIntegration', () => {
575584
setCurrentClient(client);
576585
client.init();
577586

578-
startBrowserTracingNavigationSpan(client, { name: 'test span' });
579-
580-
const navigationSpan = getActiveSpan();
587+
const navigationSpan = startBrowserTracingNavigationSpan(client, { name: 'test span' });
581588

582589
expect(spanToJSON(navigationSpan!).op).toBe('test op');
583590
});
@@ -590,7 +597,7 @@ describe('browserTracingIntegration', () => {
590597

591598
const client = new BrowserClient(
592599
getDefaultBrowserClientOptions({
593-
tracesSampleRate: 0,
600+
tracesSampleRate: 1,
594601
integrations: [
595602
browserTracingIntegration({
596603
instrumentPageLoad: false,
@@ -628,7 +635,7 @@ describe('browserTracingIntegration', () => {
628635
it("updates the scopes' propagationContexts on a navigation", () => {
629636
const client = new BrowserClient(
630637
getDefaultBrowserClientOptions({
631-
integrations: [browserTracingIntegration()],
638+
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
632639
}),
633640
);
634641
setCurrentClient(client);
@@ -637,7 +644,8 @@ describe('browserTracingIntegration', () => {
637644
const oldIsolationScopePropCtx = getIsolationScope().getPropagationContext();
638645
const oldCurrentScopePropCtx = getCurrentScope().getPropagationContext();
639646

640-
startBrowserTracingNavigationSpan(client, { name: 'test navigation span' });
647+
const span = startBrowserTracingNavigationSpan(client, { name: 'test navigation span' });
648+
const traceId = span!.spanContext().traceId;
641649

642650
const newIsolationScopePropCtx = getIsolationScope().getPropagationContext();
643651
const newCurrentScopePropCtx = getCurrentScope().getPropagationContext();
@@ -657,6 +665,22 @@ describe('browserTracingIntegration', () => {
657665

658666
expect(newIsolationScopePropCtx.traceId).not.toEqual(oldIsolationScopePropCtx.traceId);
659667
expect(newCurrentScopePropCtx.traceId).not.toEqual(oldCurrentScopePropCtx.traceId);
668+
669+
const span2 = startBrowserTracingNavigationSpan(client, { name: 'test navigation span 2' });
670+
const traceId2 = span2!.spanContext().traceId;
671+
expect(traceId2).not.toEqual(traceId);
672+
673+
const newCurrentScopePropCtx2 = getCurrentScope().getPropagationContext();
674+
expect(newCurrentScopePropCtx2).toEqual({
675+
traceId: traceId2,
676+
sampled: false,
677+
dsc: {
678+
environment: 'production',
679+
public_key: 'examplePublicKey',
680+
sample_rate: '0',
681+
trace_id: traceId2,
682+
},
683+
});
660684
});
661685

662686
it("saves the span's positive sampling decision and its DSC on the propagationContext when the span finishes", () => {
@@ -679,6 +703,7 @@ describe('browserTracingIntegration', () => {
679703
traceId: expect.stringMatching(/[a-f0-9]{32}/),
680704
});
681705

706+
updateSpanName(navigationSpan!, 'mySpan2');
682707
navigationSpan!.end();
683708

684709
const propCtxAfterEnd = getCurrentScope().getPropagationContext();
@@ -690,7 +715,7 @@ describe('browserTracingIntegration', () => {
690715
public_key: 'examplePublicKey',
691716
sample_rate: '1',
692717
sampled: 'true',
693-
transaction: 'mySpan',
718+
transaction: 'mySpan2',
694719
trace_id: propCtxBeforeEnd.traceId,
695720
},
696721
});
@@ -758,18 +783,19 @@ describe('browserTracingIntegration', () => {
758783
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(idleSpan!);
759784
const propagationContext = getCurrentScope().getPropagationContext();
760785

761-
// Span is correct
762-
expect(spanToJSON(idleSpan).op).toBe('pageload');
763-
expect(spanToJSON(idleSpan).trace_id).toEqual('12312012123120121231201212312012');
764-
expect(spanToJSON(idleSpan).parent_span_id).toEqual('1121201211212012');
765-
expect(spanIsSampled(idleSpan)).toBe(false);
786+
// Span is non-recording
787+
expect(idleSpan instanceof SentryNonRecordingSpan).toBe(true);
766788

767789
expect(dynamicSamplingContext).toBeDefined();
768790
expect(dynamicSamplingContext).toStrictEqual({ release: '2.1.14' });
769791

770792
// Propagation context keeps the meta tag trace data for later events on the same route to add them to the trace
771793
expect(propagationContext.traceId).toEqual('12312012123120121231201212312012');
772794
expect(propagationContext.parentSpanId).toEqual('1121201211212012');
795+
796+
// Ensure getTraceData is correct in this case
797+
const traceData = getTraceData();
798+
expect(traceData['sentry-trace']).toMatch(/12312012123120121231201212312012-[a-f0-9]{16}-0/);
773799
});
774800

775801
it('puts frozen Dynamic Sampling Context on pageload span if sentry-trace data and only 3rd party baggage is present', () => {
@@ -795,18 +821,19 @@ describe('browserTracingIntegration', () => {
795821
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(idleSpan);
796822
const propagationContext = getCurrentScope().getPropagationContext();
797823

798-
// Span is correct
799-
expect(spanToJSON(idleSpan).op).toBe('pageload');
800-
expect(spanToJSON(idleSpan).trace_id).toEqual('12312012123120121231201212312012');
801-
expect(spanToJSON(idleSpan).parent_span_id).toEqual('1121201211212012');
802-
expect(spanIsSampled(idleSpan)).toBe(false);
824+
// Span is NonRecordingSpan
825+
expect(idleSpan instanceof SentryNonRecordingSpan).toBe(true);
803826

804827
expect(dynamicSamplingContext).toBeDefined();
805828
expect(dynamicSamplingContext).toStrictEqual({});
806829

807830
// Propagation context keeps the meta tag trace data for later events on the same route to add them to the trace
808831
expect(propagationContext.traceId).toEqual('12312012123120121231201212312012');
809832
expect(propagationContext.parentSpanId).toEqual('1121201211212012');
833+
834+
// Ensure getTraceData is correct in this case
835+
const traceData = getTraceData();
836+
expect(traceData['sentry-trace']).toMatch(/12312012123120121231201212312012-[a-f0-9]{16}-0/);
810837
});
811838

812839
it('ignores the meta tag data for navigation spans', () => {

0 commit comments

Comments
 (0)