Skip to content

Commit

Permalink
feat: Allow disabling native SDK initialization but still use it (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennmueng authored Jan 4, 2021
1 parent 0bb79c7 commit 25e7801
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Unreleased

- fix: pas maxBreadcrumbs to Android init
- feat: Allow disabling native SDK initialization but still use it #1259

## 2.1.0

Expand Down
13 changes: 13 additions & 0 deletions src/js/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ export interface ReactNativeOptions extends BrowserOptions {
*/
enableNativeCrashHandling?: boolean;

/**
* Initializes the native SDK on init.
* Set this to `false` if you have an existing native SDK and don't want to re-initialize.
*
* NOTE: Be careful and only use this if you know what you are doing.
* If you use this flag, make sure a native SDK is running before the JS Engine initializes or events might not be captured.
* Also, make sure the DSN on both the React Native side and the native side are the same one.
* We strongly recommend checking the documentation if you need to use this.
*
* @default true
*/
shouldInitializeNativeSdk?: boolean;

/** Maximum time to wait to drain the request queue, before the process is allowed to exit. */
shutdownTimeout?: number;

Expand Down
1 change: 1 addition & 0 deletions src/js/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const DEFAULT_OPTIONS: ReactNativeOptions = {
enableNative: true,
enableNativeCrashHandling: true,
enableNativeNagger: true,
shouldInitializeNativeSdk: true,
};

/**
Expand Down
16 changes: 13 additions & 3 deletions src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,19 @@ export const NATIVE = {
* Starts native with the provided options.
* @param options ReactNativeOptions
*/
async startWithOptions(
options: ReactNativeOptions = { enableNative: true }
): Promise<boolean> {
async startWithOptions(_options: ReactNativeOptions): Promise<boolean> {
const options = {
enableNative: true,
shouldInitializeNativeSdk: true,
..._options,
};

if (!options.shouldInitializeNativeSdk) {
if (options.enableNativeNagger) {
logger.warn("Note: Native Sentry SDK was not initialized.");
}
return false;
}
if (!options.dsn) {
logger.warn(
"Warning: No DSN was provided. The Sentry SDK will be disabled. Native SDK will also not be initalized."
Expand Down
27 changes: 25 additions & 2 deletions test/wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ describe("Tests Native Wrapper", () => {
"Note: Native Sentry SDK is disabled."
);
});

test("does not initialize with shouldInitializeNativeSdk: false", async () => {
const RN = require("react-native");

RN.NativeModules.RNSentry.startWithOptions = jest.fn();
logger.warn = jest.fn();

await NATIVE.startWithOptions({
dsn: "test",
enableNative: true,
shouldInitializeNativeSdk: false,
});

expect(RN.NativeModules.RNSentry.startWithOptions).not.toBeCalled();

await NATIVE.addBreadcrumb({
message: "test",
});

expect(RN.NativeModules.RNSentry.addBreadcrumb).toBeCalledWith({
message: "test",
});
});
});

describe("sendEvent", () => {
Expand Down Expand Up @@ -222,7 +245,7 @@ describe("Tests Native Wrapper", () => {
message: {
message: event.message,
},
type: 'event',
type: "event",
});
const header = JSON.stringify({
event_id: event.event_id,
Expand All @@ -231,7 +254,7 @@ describe("Tests Native Wrapper", () => {
const item = JSON.stringify({
content_type: "application/json",
length: 1,
type: 'event',
type: "event",
});

await expect(NATIVE.sendEvent(event)).resolves.toMatch(
Expand Down

0 comments on commit 25e7801

Please sign in to comment.