Skip to content

Commit 1e120d4

Browse files
committed
more tests
1 parent 016f674 commit 1e120d4

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

packages/browser/src/tracing/previousTrace.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const PREVIOUS_TRACE_KEY = 'sentry_previous_trace';
2525
* Adds a previous_trace span link to the passed span if the passed
2626
* previousTraceInfo is still valid.
2727
*
28-
* @returns the updated previous trace info (based on the current span/trace) or `undefined` the
28+
* @returns the updated previous trace info (based on the current span/trace) to
29+
* be used on the next call
2930
*/
3031
export function addPreviousTraceSpanLink(
3132
previousTraceInfo: PreviousTraceInfo | undefined,

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

+70
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
spanIsSampled,
3030
spanToJSON,
3131
startInactiveSpan,
32+
withScope,
3233
} from '@sentry/core';
3334
import type { Span, StartSpanOptions } from '@sentry/core';
3435
import { JSDOM } from 'jsdom';
@@ -1028,6 +1029,75 @@ describe('browserTracingIntegration', () => {
10281029
});
10291030
});
10301031

1032+
describe('enablePreviousTrace', () => {
1033+
it('registers the previous trace listener on span start by default', () => {
1034+
const client = new BrowserClient(
1035+
getDefaultBrowserClientOptions({
1036+
tracesSampleRate: 1,
1037+
integrations: [browserTracingIntegration({ instrumentPageLoad: false, instrumentNavigation: false })],
1038+
}),
1039+
);
1040+
setCurrentClient(client);
1041+
client.init();
1042+
1043+
const span1 = startInactiveSpan({ name: 'test span 1', forceTransaction: true });
1044+
span1.end();
1045+
const span1Json = spanToJSON(span1);
1046+
1047+
expect(span1Json.links).toBeUndefined();
1048+
1049+
// ensure we start a new trace
1050+
getCurrentScope().setPropagationContext({ traceId: '123', sampleRand: 0.2 });
1051+
1052+
const span2 = startInactiveSpan({ name: 'test span 2', forceTransaction: true });
1053+
span2.end();
1054+
const spanJson2 = spanToJSON(span2);
1055+
1056+
expect(spanJson2.links).toEqual([
1057+
{
1058+
attributes: {
1059+
'sentry.link.type': 'previous_trace',
1060+
},
1061+
sampled: true,
1062+
span_id: span1Json.span_id,
1063+
trace_id: span1Json.trace_id,
1064+
},
1065+
]);
1066+
});
1067+
1068+
it("doesn't register the previous trace listener on span start if disabled", () => {
1069+
const client = new BrowserClient(
1070+
getDefaultBrowserClientOptions({
1071+
tracesSampleRate: 1,
1072+
integrations: [
1073+
browserTracingIntegration({
1074+
instrumentPageLoad: false,
1075+
instrumentNavigation: false,
1076+
enablePreviousTrace: false,
1077+
}),
1078+
],
1079+
}),
1080+
);
1081+
setCurrentClient(client);
1082+
client.init();
1083+
1084+
const span1 = startInactiveSpan({ name: 'test span 1', forceTransaction: true });
1085+
span1.end();
1086+
const span1Json = spanToJSON(span1);
1087+
1088+
expect(span1Json.links).toBeUndefined();
1089+
1090+
// ensure we start a new trace
1091+
getCurrentScope().setPropagationContext({ traceId: '123', sampleRand: 0.2 });
1092+
1093+
const span2 = startInactiveSpan({ name: 'test span 2', forceTransaction: true });
1094+
span2.end();
1095+
const spanJson2 = spanToJSON(span2);
1096+
1097+
expect(spanJson2.links).toBeUndefined();
1098+
});
1099+
});
1100+
10311101
// TODO(lforst): I cannot manage to get this test to pass.
10321102
/*
10331103
it('heartbeatInterval can be a custom value', () => {

0 commit comments

Comments
 (0)