Skip to content

Commit 76e1cff

Browse files
committed
add unit tests
1 parent e70a84a commit 76e1cff

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ jest.mock('@sentry/utils', () => {
3434

3535
jest.mock('../../src/browser/metrics');
3636

37+
const instrumentOutgoingRequestsMock = jest.fn();
38+
jest.mock('./../../src/browser/request', () => {
39+
const actual = jest.requireActual('./../../src/browser/request');
40+
return {
41+
...actual,
42+
instrumentOutgoingRequests: (options: Partial<BrowserTracingOptions>) => instrumentOutgoingRequestsMock(options),
43+
};
44+
});
45+
3746
beforeAll(() => {
3847
const dom = new JSDOM();
3948
// @ts-ignore need to override global document
@@ -128,6 +137,7 @@ describe('BrowserTracing', () => {
128137
expect(transaction.endTimestamp).toBe(span.endTimestamp);
129138
});
130139

140+
// TODO (v8): remove these tests
131141
describe('tracingOrigins', () => {
132142
it('sets tracing origins if provided and does not warn', () => {
133143
const sampleTracingOrigins = ['something'];
@@ -152,6 +162,43 @@ describe('BrowserTracing', () => {
152162
});
153163
});
154164

165+
describe('tracePropagationTargets', () => {
166+
it('sets tracePropagationTargets if provided', () => {
167+
const sampleTracePropagationTargets = ['something'];
168+
const inst = createBrowserTracing(true, {
169+
routingInstrumentation: customInstrumentRouting,
170+
tracePropagationTargets: sampleTracePropagationTargets,
171+
});
172+
173+
expect(inst.options.tracePropagationTargets).toEqual(sampleTracePropagationTargets);
174+
});
175+
176+
it('sets tracePropagationTargets to an empty array and does not warn', () => {
177+
const sampleTracePropagationTargets: string[] = [];
178+
const inst = createBrowserTracing(true, {
179+
routingInstrumentation: customInstrumentRouting,
180+
tracePropagationTargets: sampleTracePropagationTargets,
181+
});
182+
183+
expect(inst.options.tracePropagationTargets).toEqual(sampleTracePropagationTargets);
184+
});
185+
186+
it('correctly passes tracePropagationTargets to `instrumentOutgoingRequests` in `setupOnce`', () => {
187+
jest.clearAllMocks();
188+
const sampleTracePropagationTargets = ['something'];
189+
createBrowserTracing(true, {
190+
routingInstrumentation: customInstrumentRouting,
191+
tracePropagationTargets: sampleTracePropagationTargets,
192+
});
193+
194+
expect(instrumentOutgoingRequestsMock).toHaveBeenCalledWith({
195+
traceFetch: true,
196+
traceXHR: true,
197+
tracePropagationTargets: ['something'],
198+
});
199+
});
200+
});
201+
155202
describe('beforeNavigate', () => {
156203
it('is called on transaction creation', () => {
157204
const mockBeforeNavigation = jest.fn().mockReturnValue({ name: 'here/is/my/path' });

packages/tracing/test/browser/request.test.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -392,32 +392,18 @@ describe('callbacks', () => {
392392
});
393393

394394
// TODO (v8): Adapt these tests once we remove `tracingOrigins`
395-
describe('[pre-v8]: shouldAttachHeaders', () => {
396-
describe('prefer `tracePropagationTargets` over `tracingOrigins`', () => {
395+
describe('shouldAttachHeaders', () => {
396+
describe('should prefer `tracePropagationTargets` over defaults', () => {
397397
it('should return `true` if the url matches the new tracePropagationTargets', () => {
398-
expect(shouldAttachHeaders('http://example.com', ['example.com'], undefined)).toBe(true);
398+
expect(shouldAttachHeaders('http://example.com', ['example.com'])).toBe(true);
399399
});
400400

401401
it('should return `false` if tracePropagationTargets array is empty', () => {
402-
expect(shouldAttachHeaders('http://localhost:3000/test', [], ['localhost'])).toBe(false);
402+
expect(shouldAttachHeaders('http://localhost:3000/test', [])).toBe(false);
403403
});
404404

405405
it("should return `false` if tracePropagationTargets array doesn't match", () => {
406-
expect(shouldAttachHeaders('http://localhost:3000/test', ['example.com'], ['localhost'])).toBe(false);
407-
});
408-
});
409-
410-
describe('tracingOrigins backwards compatibility (tracePropagationTargets not defined)', () => {
411-
it('should return `true` if the url matches tracingOrigns', () => {
412-
expect(shouldAttachHeaders('http://example.com', undefined, ['example.com'])).toBe(true);
413-
});
414-
415-
it('should return `false` if tracePropagationTargets array is empty', () => {
416-
expect(shouldAttachHeaders('http://localhost:3000/test', undefined, [])).toBe(false);
417-
});
418-
419-
it("should return `false` if tracePropagationTargets array doesn't match", () => {
420-
expect(shouldAttachHeaders('http://localhost:3000/test', undefined, ['example.com'])).toBe(false);
406+
expect(shouldAttachHeaders('http://localhost:3000/test', ['example.com'])).toBe(false);
421407
});
422408
});
423409

@@ -428,11 +414,11 @@ describe('[pre-v8]: shouldAttachHeaders', () => {
428414
'http://somewhere.com/test/localhost/123',
429415
'http://somewhere.com/test?url=localhost:3000&test=123',
430416
])('return `true` for urls matching defaults (%s)', url => {
431-
expect(shouldAttachHeaders(url, undefined, undefined)).toBe(true);
417+
expect(shouldAttachHeaders(url, undefined)).toBe(true);
432418
});
433419

434420
it.each(['notmydoman/api/test', 'example.com'])('return `false` for urls not matching defaults (%s)', url => {
435-
expect(shouldAttachHeaders(url, undefined, undefined)).toBe(false);
421+
expect(shouldAttachHeaders(url, undefined)).toBe(false);
436422
});
437423
});
438424
});

0 commit comments

Comments
 (0)