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

feat: Allow disabling native SDK initialization but still use it #1259

Merged
merged 3 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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