Skip to content

Commit b8f1da9

Browse files
authored
fix(appStart): Handle undefined AppRegistryIntegration.onRunApplication gracefully (#4743)
1 parent 2f9e699 commit b8f1da9

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/core/src/js/tracing/reactnativeprofiler.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { timestampInSeconds } from '@sentry/core';
1+
import { logger, timestampInSeconds } from '@sentry/core';
22
import { getClient, Profiler } from '@sentry/react';
33

44
import { getAppRegistryIntegration } from '../integrations/appRegistry';
@@ -49,7 +49,12 @@ export class ReactNativeProfiler extends Profiler {
4949

5050
client.addIntegration && client.addIntegration(createIntegration(this.name));
5151

52-
getAppRegistryIntegration(client).onRunApplication(ReactNativeProfilerGlobalState.onRunApplicationHook);
52+
const appRegistryIntegration = getAppRegistryIntegration(client);
53+
if (appRegistryIntegration && typeof appRegistryIntegration.onRunApplication === 'function') {
54+
appRegistryIntegration.onRunApplication(ReactNativeProfilerGlobalState.onRunApplicationHook);
55+
} else {
56+
logger.warn('AppRegistryIntegration.onRunApplication not found or invalid.');
57+
}
5358

5459
// eslint-disable-next-line @typescript-eslint/no-floating-promises
5560
_captureAppStart({ isManual: false });

packages/core/test/wrap.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { logger, setCurrentClient } from '@sentry/core';
2+
import { render } from '@testing-library/react-native';
13
import * as React from 'react';
4+
import { Text } from 'react-native';
25

6+
import * as AppRegistry from '../src/js/integrations/appRegistry';
37
import { wrap } from '../src/js/sdk';
8+
import { getDefaultTestClientOptions, TestClient } from './mocks/client';
49

510
describe('Sentry.wrap', () => {
611
it('should not enforce any keys on the wrapped component', () => {
@@ -9,4 +14,23 @@ describe('Sentry.wrap', () => {
914

1015
expect(typeof ActualWrapped.defaultProps).toBe(typeof Mock.defaultProps);
1116
});
17+
18+
it('should wrap the component and init with a warning when getAppRegistryIntegration returns undefined', () => {
19+
logger.warn = jest.fn();
20+
const getAppRegistryIntegration = jest.spyOn(AppRegistry, 'getAppRegistryIntegration').mockReturnValueOnce(undefined);
21+
const Mock: React.FC = () => <Text>Test</Text>;
22+
const client = new TestClient(
23+
getDefaultTestClientOptions(),
24+
);
25+
setCurrentClient(client);
26+
27+
client.init();
28+
const ActualWrapped = wrap(Mock);
29+
30+
const { getByText } = render(<ActualWrapped />);
31+
32+
expect(getAppRegistryIntegration).toHaveBeenCalled();
33+
expect(logger.warn).toHaveBeenCalledWith('AppRegistryIntegration.onRunApplication not found or invalid.');
34+
expect(getByText('Test')).toBeTruthy();
35+
});
1236
});

0 commit comments

Comments
 (0)