Skip to content

Commit 8ebddd1

Browse files
committed
add setupBrowserTransport tests
1 parent 6d7967d commit 8ebddd1

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

packages/browser/src/transports/setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function setupBrowserTransport(options: BrowserOptions): { transport: Tra
4646
if (options.transport) {
4747
return { transport: new options.transport(transportOptions) };
4848
}
49+
4950
if (supportsFetch()) {
5051
const requestOptions: RequestInit = { ...transportOptions.fetchParameters };
5152
const newTransport = makeNewFetchTransport({ requestOptions, url });
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { NoopTransport } from '@sentry/core';
2+
import { FetchTransport, setupBrowserTransport, XHRTransport } from '../../../src/transports';
3+
import { SimpleTransport } from '../mocks/simpletransport';
4+
5+
const DSN = 'https://username@domain/123';
6+
7+
let fetchSupported = true;
8+
let getNativeFetchImplCalled = false;
9+
10+
jest.mock('@sentry/utils', () => {
11+
const original = jest.requireActual('@sentry/utils');
12+
return {
13+
...original,
14+
supportsFetch(): boolean {
15+
return fetchSupported;
16+
},
17+
getGlobalObject(): any {
18+
return {
19+
fetch: () => {},
20+
};
21+
},
22+
};
23+
});
24+
25+
jest.mock('@sentry/browser/src/transports/utils', () => {
26+
const original = jest.requireActual('@sentry/browser/src/transports/utils');
27+
return {
28+
...original,
29+
getNativeFetchImplementation() {
30+
getNativeFetchImplCalled = true;
31+
return {
32+
fetch: () => {},
33+
};
34+
},
35+
};
36+
});
37+
38+
describe('setupBrowserTransport', () => {
39+
beforeEach(() => {
40+
getNativeFetchImplCalled = false;
41+
});
42+
43+
it('returns NoopTransport if no dsn is passed', () => {
44+
const { transport, newTransport } = setupBrowserTransport({});
45+
46+
expect(transport).toBeDefined();
47+
expect(transport).toBeInstanceOf(NoopTransport);
48+
expect(newTransport).toBeUndefined();
49+
});
50+
51+
it('returns the instantiated transport passed via the options', () => {
52+
const options = { dsn: DSN, transport: SimpleTransport };
53+
const { transport, newTransport } = setupBrowserTransport(options);
54+
55+
expect(transport).toBeDefined();
56+
expect(transport).toBeInstanceOf(SimpleTransport);
57+
expect(newTransport).toBeUndefined();
58+
});
59+
60+
it('returns fetchTransports if fetch is supported', () => {
61+
const options = { dsn: DSN };
62+
const { transport, newTransport } = setupBrowserTransport(options);
63+
64+
expect(transport).toBeDefined();
65+
expect(transport).toBeInstanceOf(FetchTransport);
66+
expect(newTransport).toBeDefined();
67+
// This is a weird way of testing that `newTransport` is using fetch but it works.
68+
// Given that the new transports are functions, we cannot test their instance.
69+
// Totally open for suggestions how to test this better here
70+
expect(getNativeFetchImplCalled).toBe(true);
71+
});
72+
73+
it('returns xhrTransports if fetch is not supported', () => {
74+
fetchSupported = false;
75+
76+
const options = { dsn: DSN };
77+
const { transport, newTransport } = setupBrowserTransport(options);
78+
79+
expect(transport).toBeDefined();
80+
expect(transport).toBeInstanceOf(XHRTransport);
81+
expect(newTransport).toBeDefined();
82+
// This is a weird way of testing that `newTransport` is using fetch but it works.
83+
// Given that the new transports are functions, we cannot test their instance.
84+
// Totally open for suggestions how to test this better here
85+
expect(getNativeFetchImplCalled).toBe(false);
86+
});
87+
});

0 commit comments

Comments
 (0)