From 375a2e1ec6536c438d0e0142e6d11e365c97491d Mon Sep 17 00:00:00 2001 From: Ziv Levy Date: Wed, 27 Jul 2022 13:46:16 +0300 Subject: [PATCH 1/5] fix: check for given options.transport before setting native transport layer #resolves #2397 --- src/js/client.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/js/client.ts b/src/js/client.ts index a76163e1ba..cef7a6d3fd 100644 --- a/src/js/client.ts +++ b/src/js/client.ts @@ -25,14 +25,14 @@ export class ReactNativeClient extends BaseClient { * @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 From 96dc7ce570fbcfedf71b16f4ef0ef4d1c9e07c76 Mon Sep 17 00:00:00 2001 From: Ziv Levy Date: Wed, 27 Jul 2022 13:48:06 +0300 Subject: [PATCH 2/5] add a test to make sure this wont break in the future --- test/client.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/client.test.ts b/test/client.test.ts index 61afc111da..f5782d7cf7 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -1,3 +1,4 @@ +import { Envelope, Transport } from '@sentry/types'; import * as RN from 'react-native'; import { ReactNativeClient } from '../src/js/client'; @@ -99,6 +100,22 @@ describe('Tests ReactNativeClient', () => { // eslint-disable-next-line deprecation/deprecation await expect(RN.YellowBox.ignoreWarnings).toBeCalled(); }); + + test('use custom transport function', async () => { + const myCustomTransportFn = (): Transport => ({ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + send: (request: Envelope) => Promise.resolve(), + flush: (timeout?: number) => Promise.resolve(Boolean(timeout)) + }); + const client = new ReactNativeClient({ + ...DEFAULT_OPTIONS, + dsn: EXAMPLE_DSN, + transport: myCustomTransportFn + } as ReactNativeClientOptions); + + await expect(client.getTransport()?.flush(1)).resolves.toBe(true); + await expect(client.getTransport()?.send({} as Envelope)).resolves.toBeUndefined(); + }); }); describe('onReady', () => { From d74f95a2562bb0666c5dc2aa8b7d0605eab6c6b5 Mon Sep 17 00:00:00 2001 From: Ziv Levy Date: Wed, 27 Jul 2022 14:45:46 +0300 Subject: [PATCH 3/5] stronger test validation --- test/client.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/client.test.ts b/test/client.test.ts index f5782d7cf7..39a5c4a3df 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -102,10 +102,12 @@ describe('Tests ReactNativeClient', () => { }); 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 => ({ - // eslint-disable-next-line @typescript-eslint/no-unused-vars - send: (request: Envelope) => Promise.resolve(), - flush: (timeout?: number) => Promise.resolve(Boolean(timeout)) + send: mySend, + flush: myFlush }); const client = new ReactNativeClient({ ...DEFAULT_OPTIONS, @@ -113,8 +115,8 @@ describe('Tests ReactNativeClient', () => { transport: myCustomTransportFn } as ReactNativeClientOptions); - await expect(client.getTransport()?.flush(1)).resolves.toBe(true); - await expect(client.getTransport()?.send({} as Envelope)).resolves.toBeUndefined(); + expect(client.getTransport()?.flush).toBe(myFlush); + expect(client.getTransport()?.send).toBe(mySend); }); }); From ce2ac35a30c8ebe7738db8806859758e870ae210 Mon Sep 17 00:00:00 2001 From: Ziv Levy Date: Wed, 27 Jul 2022 14:50:44 +0300 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c60aa4104e..d0de6b109b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 141d1fb12fdc2545a3102b525f8a44f51a45413f Mon Sep 17 00:00:00 2001 From: Ziv Levy Date: Wed, 27 Jul 2022 18:12:04 +0300 Subject: [PATCH 5/5] Update client.test.ts --- test/client.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/client.test.ts b/test/client.test.ts index 39a5c4a3df..eabb455ce0 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -114,8 +114,9 @@ describe('Tests ReactNativeClient', () => { 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); }); });