Skip to content

Commit

Permalink
sentry [nfc]: move is-active test into the main code base
Browse files Browse the repository at this point in the history
  • Loading branch information
rk-for-zulip authored and gnprice committed Jan 1, 2020
1 parent 281bf5f commit 52b6dff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/__tests__/sentry-test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
// @flow

import * as Sentry from '@sentry/react-native';
import { isSentryActive } from '../sentry';

describe('sentry', () => {
describe('is usable without initialization', () => {
const expectSentryUninitialized = () => {
// Hub#getClient() is documented as possibly returning undefined, but the
// significance of `undefined` is not. In practice, it appears to be
// `undefined` exactly when `Sentry.init()` has not yet been called.
expect(Sentry.getCurrentHub().getClient()).toBeUndefined();
};
// Sentry shouldn't be active at all in debug mode -- certainly not while
// we're specifically testing that its entry points can be used while it's
// uninitialized.
beforeEach(() => {
expect(isSentryActive()).toBeFalse();
});

test('breadcrumbs', () => {
expectSentryUninitialized();
afterEach(() => {
expect(isSentryActive()).toBeFalse();
});

test('breadcrumbs', () => {
Sentry.addBreadcrumb({
message: 'test message',
level: Sentry.Severity.Debug,
});
});

test('exception reporting', () => {
expectSentryUninitialized();

// The text here is intended to prevent some hypothetical future reader of
// Sentry event logs dismissing the error as harmless expected noise, in
// case Sentry is somehow actually initialized at this point.
Expand Down
15 changes: 15 additions & 0 deletions src/sentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ import * as Sentry from '@sentry/react-native';
import DeviceInfo from 'react-native-device-info';
import config from './config';

export const isSentryActive = (): boolean => {
// Hub#getClient() is documented as possibly returning undefined, but the
// significance of `undefined` is not. In practice, it appears to be
// `undefined` exactly when `Sentry.init()` has not yet been called.
const client = Sentry.getCurrentHub().getClient();

/* The `enabled` option in getOptions() is theoretically togglable at runtime:
https://github.com/getsentry/sentry-javascript/issues/2039#issuecomment-486674574
We avoid this, however, as it will only toggle the JavaScript SDK and not
the lower-level native-code SDKs. */
// return (client && client.getOptions().enabled) ?? false;

return !!client;
};

const preventNoise = (): void => {
/* Sentry should not normally be used in debug mode. (For one thing, the
debug-mode build process doesn't ordinarily create bundles or .map files,
Expand Down

0 comments on commit 52b6dff

Please sign in to comment.