Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

should not ignore options.transport function provided in Sentry.init(...) #2398

Merged
merged 6 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- SENTRY_DIST accepts non-number values on Android ([#2395](https://github.com/getsentry/sentry-react-native/pull/2395))
- should not ignore `options.transport` function provided in `Sentry.init(...)` ([#2398](https://github.com/getsentry/sentry-react-native/pull/2398))

### Features

Expand Down
14 changes: 7 additions & 7 deletions src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
* @param options Configuration options for this SDK.
*/
public constructor(options: ReactNativeClientOptions) {
const transport = (options: BrowserTransportOptions, nativeFetch?: FetchImpl): Transport => {
if (NATIVE.isNativeTransportAvailable()) {
return new NativeTransport();
}
return makeFetchTransport(options, nativeFetch);
if (!options.transport) {
options.transport = (options: BrowserTransportOptions, nativeFetch?: FetchImpl): Transport => {
if (NATIVE.isNativeTransportAvailable()) {
return new NativeTransport();
}
return makeFetchTransport(options, nativeFetch);
};
}

options.transport = transport;
super(options);

// This is a workaround for now using fetch on RN, this is a known issue in react-native and only generates a warning
Expand Down
20 changes: 20 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Envelope, Transport } from '@sentry/types';
import * as RN from 'react-native';

import { ReactNativeClient } from '../src/js/client';
Expand Down Expand Up @@ -99,6 +100,25 @@ describe('Tests ReactNativeClient', () => {
// eslint-disable-next-line deprecation/deprecation
await expect(RN.YellowBox.ignoreWarnings).toBeCalled();
});

test('use custom transport function', async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mySend = (request: Envelope) => Promise.resolve();
const myFlush = (timeout?: number) => Promise.resolve(Boolean(timeout));
const myCustomTransportFn = (): Transport => ({
send: mySend,
flush: myFlush
});
const client = new ReactNativeClient({
...DEFAULT_OPTIONS,
dsn: EXAMPLE_DSN,
transport: myCustomTransportFn
} as ReactNativeClientOptions);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(client.getTransport()?.flush).toBe(myFlush);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(client.getTransport()?.send).toBe(mySend);
});
});

describe('onReady', () => {
Expand Down