-
-
Notifications
You must be signed in to change notification settings - Fork 358
feat(tracing): Add Sentry.NavigationContainer wrapper for React Navigation #6199
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
Draft
antonis
wants to merge
7
commits into
main
Choose a base branch
from
feat/sentry-navigation-container
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9d63f25
feat(tracing): Add Sentry.NavigationContainer wrapper for React Navigโฆ
antonis 8fb22ab
feat(core): Add changelog, fix lint, update sample apps for Navigatioโฆ
antonis 2fec2e8
Merge remote-tracking branch 'origin/main' into feat/sentry-navigatioโฆ
antonis 240b1fb
fix(core): Add warnings for missing client and missing integration inโฆ
antonis fc3fa41
fix(core): Call onReady in NavigationContainer fallback path
antonis 7af354a
fix(core): Remove useEffect from NavigationContainer, document fallbaโฆ
antonis 134cc57
fix(core): Wrap SDK logic in try/catch in NavigationContainer onReady
antonis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { debug, getClient } from '@sentry/core'; | ||
| import * as React from 'react'; | ||
|
|
||
| import { getNavigationContainerComponent } from './reactNavigationImport'; | ||
| import { getReactNavigationIntegration } from './tracing/reactnavigation'; | ||
|
|
||
| let _warnedMissing = false; | ||
| let _warnedNoClient = false; | ||
| let _warnedNoIntegration = false; | ||
|
|
||
| /** | ||
| * Drop-in replacement for `NavigationContainer` from `@react-navigation/native` | ||
| * that automatically wires up Sentry's `reactNavigationIntegration`. | ||
| * | ||
| * Sentry registers the navigation container before the user-provided `onReady` | ||
| * callback fires, so navigation spans are captured from the first route. | ||
| * | ||
| * If `@react-navigation/native` is not installed, children are rendered directly | ||
| * and `onReady` is not called. | ||
| * | ||
| * @example | ||
| * ```jsx | ||
| * <Sentry.NavigationContainer> | ||
| * <Stack.Navigator> | ||
| * ... | ||
| * </Stack.Navigator> | ||
| * </Sentry.NavigationContainer> | ||
| * ``` | ||
| */ | ||
| export const NavigationContainer = React.forwardRef<unknown, Record<string, unknown>>((props, forwardedRef) => { | ||
| const { onReady: userOnReady, ...restProps } = props; | ||
| const RealNavigationContainer = getNavigationContainerComponent(); | ||
|
|
||
| const internalRef = React.useRef<unknown>(null); | ||
|
|
||
| const mergedRef = React.useCallback( | ||
| (instance: unknown) => { | ||
| internalRef.current = instance; | ||
| if (typeof forwardedRef === 'function') { | ||
| forwardedRef(instance); | ||
| } else if (forwardedRef != null) { | ||
| (forwardedRef as React.MutableRefObject<unknown>).current = instance; | ||
| } | ||
| }, | ||
| [forwardedRef], | ||
| ); | ||
|
|
||
| const onReady = React.useCallback(() => { | ||
| try { | ||
| const client = getClient(); | ||
| if (!client) { | ||
| if (!_warnedNoClient) { | ||
| _warnedNoClient = true; | ||
| debug.warn( | ||
| '[Sentry] NavigationContainer: Sentry is not initialized. Call Sentry.init() before mounting NavigationContainer.', | ||
| ); | ||
| } | ||
| } else { | ||
| const integration = getReactNavigationIntegration(client); | ||
| if (integration) { | ||
| integration.registerNavigationContainer(internalRef); | ||
| } else if (!_warnedNoIntegration) { | ||
| _warnedNoIntegration = true; | ||
| debug.log( | ||
| '[Sentry] NavigationContainer: reactNavigationIntegration is not registered. Navigation spans will not be captured.', | ||
| ); | ||
| } | ||
| } | ||
| } catch { | ||
| // SDK failures must never break the host app | ||
| } | ||
|
|
||
| if (typeof userOnReady === 'function') { | ||
| (userOnReady as () => void)(); | ||
| } | ||
| }, [userOnReady]); | ||
|
|
||
| if (!RealNavigationContainer) { | ||
| if (!_warnedMissing) { | ||
| _warnedMissing = true; | ||
| debug.warn('[Sentry] NavigationContainer requires @react-navigation/native to be installed.'); | ||
| } | ||
| return <>{restProps.children as React.ReactNode}</>; | ||
| } | ||
|
|
||
| return <RealNavigationContainer {...restProps} ref={mergedRef} onReady={onReady} />; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type * as React from 'react'; | ||
|
|
||
| type NavigationContainerComponent = React.ComponentType<Record<string, unknown>>; | ||
|
|
||
| let _cached: NavigationContainerComponent | null | undefined; | ||
|
|
||
| /** | ||
| * @returns NavigationContainer from @react-navigation/native or null if not installed. | ||
| * The result is cached after the first call. | ||
| */ | ||
| export function getNavigationContainerComponent(): NavigationContainerComponent | null { | ||
| if (_cached !== undefined) { | ||
| return _cached; | ||
| } | ||
| try { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const mod = require('@react-navigation/native') as { | ||
| NavigationContainer?: NavigationContainerComponent; | ||
| }; | ||
| _cached = mod?.NavigationContainer ?? null; | ||
| } catch { | ||
| _cached = null; | ||
| } | ||
| return _cached; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { render } from '@testing-library/react-native'; | ||
| import * as React from 'react'; | ||
| import { Text } from 'react-native'; | ||
|
|
||
| import { NavigationContainer } from '../src/js/NavigationContainer'; | ||
|
|
||
| const mockDebugWarn = jest.fn(); | ||
|
|
||
| jest.mock('@sentry/core', () => ({ | ||
| getClient: () => undefined, | ||
| debug: { | ||
| get log() { | ||
| return jest.fn(); | ||
| }, | ||
| get warn() { | ||
| return mockDebugWarn; | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../src/js/tracing/reactnavigation', () => ({ | ||
| getReactNavigationIntegration: () => undefined, | ||
| })); | ||
|
|
||
| jest.mock('../src/js/reactNavigationImport', () => ({ | ||
| getNavigationContainerComponent: () => null, | ||
| })); | ||
|
|
||
| describe('NavigationContainer without @react-navigation/native', () => { | ||
| it('renders children directly and warns', () => { | ||
| const { getByText } = render( | ||
| <NavigationContainer> | ||
| <Text>Fallback Content</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(getByText('Fallback Content')).toBeTruthy(); | ||
| expect(mockDebugWarn).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { render } from '@testing-library/react-native'; | ||
| import * as React from 'react'; | ||
| import { Text, View } from 'react-native'; | ||
|
|
||
| import { NavigationContainer } from '../src/js/NavigationContainer'; | ||
|
|
||
| const mockRegisterNavigationContainer = jest.fn(); | ||
| const mockGetClient = jest.fn(); | ||
| const mockDebugLog = jest.fn(); | ||
| const mockDebugWarn = jest.fn(); | ||
|
|
||
| jest.mock('@sentry/core', () => ({ | ||
| getClient: (...args: unknown[]) => mockGetClient(...args), | ||
| debug: { | ||
| get log() { | ||
| return mockDebugLog; | ||
| }, | ||
| get warn() { | ||
| return mockDebugWarn; | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| const mockGetReactNavigationIntegration = jest.fn(); | ||
| jest.mock('../src/js/tracing/reactnavigation', () => ({ | ||
| getReactNavigationIntegration: (...args: unknown[]) => mockGetReactNavigationIntegration(...args), | ||
| })); | ||
|
|
||
| const MockNavigationContainerComponent = React.forwardRef<View, Record<string, unknown>>((props, ref) => { | ||
| const { onReady, children, ...rest } = props; | ||
| React.useEffect(() => { | ||
| if (typeof onReady === 'function') { | ||
| (onReady as () => void)(); | ||
| } | ||
| }, [onReady]); | ||
| return ( | ||
| <View ref={ref as React.Ref<View>} testID="mock-navigation-container" {...rest}> | ||
| {children as React.ReactNode} | ||
| </View> | ||
| ); | ||
| }); | ||
|
|
||
| jest.mock('../src/js/reactNavigationImport', () => ({ | ||
| getNavigationContainerComponent: () => MockNavigationContainerComponent, | ||
| })); | ||
|
|
||
| describe('NavigationContainer', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockGetClient.mockReturnValue({ getIntegrationByName: jest.fn() }); | ||
| mockGetReactNavigationIntegration.mockReturnValue({ | ||
| registerNavigationContainer: mockRegisterNavigationContainer, | ||
| }); | ||
| }); | ||
|
|
||
| it('renders children through to the underlying NavigationContainer', () => { | ||
| const { getByText } = render( | ||
| <NavigationContainer> | ||
| <Text>Child Content</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(getByText('Child Content')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('calls registerNavigationContainer on ready', () => { | ||
| render( | ||
| <NavigationContainer> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(mockRegisterNavigationContainer).toHaveBeenCalledTimes(1); | ||
| expect(mockRegisterNavigationContainer).toHaveBeenCalledWith( | ||
| expect.objectContaining({ current: expect.anything() }), | ||
| ); | ||
| }); | ||
|
|
||
| it('forwards ref to the underlying NavigationContainer', () => { | ||
| const ref = React.createRef<unknown>(); | ||
| render( | ||
| <NavigationContainer ref={ref}> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(ref.current).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('calls registerNavigationContainer before user onReady', () => { | ||
| const callOrder: string[] = []; | ||
| mockRegisterNavigationContainer.mockImplementation(() => callOrder.push('sentry')); | ||
| const userOnReady = jest.fn(() => callOrder.push('user')); | ||
| render( | ||
| <NavigationContainer onReady={userOnReady}> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(callOrder).toEqual(['sentry', 'user']); | ||
| }); | ||
|
|
||
| it('chains user-provided onReady callback', () => { | ||
| const userOnReady = jest.fn(); | ||
| render( | ||
| <NavigationContainer onReady={userOnReady}> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(userOnReady).toHaveBeenCalledTimes(1); | ||
| expect(mockRegisterNavigationContainer).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('warns and skips registration when client is not available', () => { | ||
| mockGetClient.mockReturnValue(undefined); | ||
| render( | ||
| <NavigationContainer> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(mockRegisterNavigationContainer).not.toHaveBeenCalled(); | ||
| expect(mockDebugWarn).toHaveBeenCalledWith(expect.stringContaining('Sentry is not initialized')); | ||
| }); | ||
|
|
||
| it('logs when client exists but reactNavigationIntegration is not registered', () => { | ||
| mockGetReactNavigationIntegration.mockReturnValue(undefined); | ||
| render( | ||
| <NavigationContainer> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| expect(mockRegisterNavigationContainer).not.toHaveBeenCalled(); | ||
| expect(mockDebugLog).toHaveBeenCalledWith(expect.stringContaining('reactNavigationIntegration is not registered')); | ||
| }); | ||
|
|
||
| it('passes through all props to NavigationContainer', () => { | ||
| const { getByTestId } = render( | ||
| <NavigationContainer accessibilityLabel="nav"> | ||
| <Text>App</Text> | ||
| </NavigationContainer>, | ||
| ); | ||
| const container = getByTestId('mock-navigation-container'); | ||
| expect(container.props.accessibilityLabel).toBe('nav'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.