Skip to content

Commit c4d59b2

Browse files
Disable enableNative if Native SDK was not initialized (#3099)
Co-authored-by: Kryštof Woldřich <[email protected]>
1 parent 1fdb409 commit c4d59b2

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Disable `enableNative` if Native SDK is not available ([#3099](https://github.com/getsentry/sentry-react-native/pull/3099))
78
- Dynamically resolve `collectModulesScript` path to support monorepos ([#3092])(https://github.com/getsentry/sentry-react-native/pull/3092)
89
- Native wrapper methods don't throw disabled error after re-initializing [#3093](https://github.com/getsentry/sentry-react-native/pull/3093)
910

src/js/sdk.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Scope } from '@sentry/core';
2-
import { getIntegrationsToSetup, hasTracingEnabled , Hub, initAndBind, makeMain, setExtra } from '@sentry/core';
2+
import { getIntegrationsToSetup, hasTracingEnabled, Hub, initAndBind, makeMain, setExtra } from '@sentry/core';
33
import { HttpClient } from '@sentry/integrations';
44
import {
55
defaultIntegrations as reactDefaultIntegrations,
@@ -32,13 +32,13 @@ import { ReactNativeProfiler, ReactNativeTracing } from './tracing';
3232
import { DEFAULT_BUFFER_SIZE, makeNativeTransportFactory } from './transports/native';
3333
import { makeUtf8TextEncoder } from './transports/TextEncoder';
3434
import { safeFactory, safeTracesSampler } from './utils/safe';
35+
import { NATIVE } from './wrapper';
3536

3637
const IGNORED_DEFAULT_INTEGRATIONS = [
3738
'GlobalHandlers', // We will use the react-native internal handlers
3839
'TryCatch', // We don't need this
3940
];
4041
const DEFAULT_OPTIONS: ReactNativeOptions = {
41-
enableNative: true,
4242
enableNativeCrashHandling: true,
4343
enableNativeNagger: true,
4444
autoInitializeNativeSdk: true,
@@ -65,15 +65,18 @@ export function init(passedOptions: ReactNativeOptions): void {
6565
// eslint-disable-next-line deprecation/deprecation
6666
?? passedOptions.transportOptions?.bufferSize
6767
?? DEFAULT_OPTIONS.maxQueueSize;
68+
69+
const enableNative = passedOptions.enableNative === undefined || passedOptions.enableNative
70+
? NATIVE.isNativeAvailable()
71+
: false;
6872
const options: ReactNativeClientOptions = {
6973
...DEFAULT_OPTIONS,
7074
...passedOptions,
75+
enableNative,
7176
// If custom transport factory fails the SDK won't initialize
7277
transport: passedOptions.transport
7378
|| makeNativeTransportFactory({
74-
enableNative: passedOptions.enableNative !== undefined
75-
? passedOptions.enableNative
76-
: DEFAULT_OPTIONS.enableNative
79+
enableNative,
7780
})
7881
|| makeFetchTransport,
7982
transportOptions: {

test/sdk.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,51 @@ describe('Tests the SDK functionality', () => {
215215
});
216216

217217
describe('transport initialization', () => {
218+
describe('native SDK unavailable', () => {
219+
it('fetchTransport set and enableNative set to false', () => {
220+
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => false);
221+
init({});
222+
// eslint-disable-next-line @typescript-eslint/unbound-method
223+
expect(NATIVE.isNativeAvailable).toBeCalled();
224+
// @ts-ignore enableNative not publicly available here.
225+
expect(usedOptions()?.enableNative).toEqual(false);
226+
expect(usedOptions()?.transport).toEqual(makeFetchTransport);
227+
});
228+
229+
it('fetchTransport set and passed enableNative ignored when true', () => {
230+
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => false);
231+
init({ enableNative: true });
232+
// eslint-disable-next-line @typescript-eslint/unbound-method
233+
expect(NATIVE.isNativeAvailable).toBeCalled();
234+
// @ts-ignore enableNative not publicly available here.
235+
expect(usedOptions()?.enableNative).toEqual(false);
236+
expect(usedOptions()?.transport).toEqual(makeFetchTransport);
237+
});
238+
239+
it('fetchTransport set and isNativeAvailable not called when passed enableNative set to false', () => {
240+
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => false);
241+
init({ enableNative: false });
242+
// eslint-disable-next-line @typescript-eslint/unbound-method
243+
expect(NATIVE.isNativeAvailable).not.toBeCalled();
244+
// @ts-ignore enableNative not publicly available here.
245+
expect(usedOptions()?.enableNative).toEqual(false);
246+
expect(usedOptions()?.transport).toEqual(makeFetchTransport);
247+
});
248+
249+
it('custom transport set and enableNative set to false', () => {
250+
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => false);
251+
const mockTransport = jest.fn();
252+
init({
253+
transport: mockTransport,
254+
});
255+
expect(usedOptions()?.transport).toEqual(mockTransport);
256+
// eslint-disable-next-line @typescript-eslint/unbound-method
257+
expect(NATIVE.isNativeAvailable).toBeCalled();
258+
// @ts-ignore enableNative not publicly available here.
259+
expect(usedOptions()?.enableNative).toEqual(false);
260+
});
261+
});
262+
218263
it('uses transport from the options', () => {
219264
const mockTransport = jest.fn();
220265
init({

0 commit comments

Comments
 (0)