Skip to content

Commit bc8eda8

Browse files
Add error boundary e2e tests in React Native (#2590)
* add error boundary e2e test * fix lint errors * fix lint * add react import * add timer for error * update scenario * add jsConfig * add set timeout * revert not needed changes * add empty onError callback to prevent app crashing * add new step in scenario * add metadata to the event * fix the event metadata --------- Co-authored-by: Ben Wilson <ben.wilson@smartbear.com>
1 parent ebae010 commit bc8eda8

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Bugsnag from '@bugsnag/react-native'
2+
import * as React from 'react'
3+
import { Text, View } from 'react-native'
4+
import Scenario from './Scenario'
5+
6+
const onError = (event) => {
7+
// callback will only run for errors caught by boundary
8+
event.addMetadata('errorBoundary', 'caughtByErrorBoundary', true)
9+
event.addMetadata('errorBoundary', 'handlerType', 'react-native-error-boundary')
10+
}
11+
12+
export class ReactNativeErrorBoundaryScenario extends Scenario {
13+
view () {
14+
const ErrorBoundary = Bugsnag.getPlugin('react').createErrorBoundary(React)
15+
return (
16+
<ErrorBoundary FallbackComponent={ErrorView} onError={onError}>
17+
<MainView />
18+
</ErrorBoundary>
19+
)
20+
}
21+
22+
run () {
23+
// Error is thrown during render
24+
}
25+
}
26+
27+
function ErrorView () {
28+
return (
29+
<View>
30+
<Text>There was an error</Text>
31+
</View>
32+
)
33+
}
34+
35+
function MainView () {
36+
return (
37+
<View>
38+
<Text>Hello world {text()}</Text>
39+
</View>
40+
)
41+
}
42+
43+
const text = function () {
44+
throw new Error('borked')
45+
}

test/react-native/features/fixtures/scenario-launcher/src/scenarios/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export { NativeFeatureFlagsScenario } from './NativeFeatureFlagsScenario'
7878
export { ReactNavigationBreadcrumbsEnabledScenario } from './ReactNavigationBreadcrumbsEnabledScenario'
7979
export { ReactNavigationBreadcrumbsDisabledScenario } from './ReactNavigationBreadcrumbsDisabledScenario'
8080

81+
// react-native-error-boundary.feature
82+
export { ReactNativeErrorBoundaryScenario } from './ReactNativeErrorBoundaryScenario'
83+
8184
// react-native-navigation.feature
8285
export { ReactNativeNavigationBreadcrumbsEnabledScenario } from './ReactNativeNavigationBreadcrumbsEnabledScenario'
8386
export { ReactNativeNavigationBreadcrumbsDisabledScenario } from './ReactNativeNavigationBreadcrumbsDisabledScenario'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: ReactNative ErrorBoundary support
2+
3+
Scenario: ErrorBoundary component reports errors
4+
When I run "ReactNativeErrorBoundaryScenario"
5+
And I relaunch the app after a crash
6+
And I configure Bugsnag for "ReactNativeErrorBoundaryScenario"
7+
Then I wait to receive an error
8+
And the exception "errorClass" equals "Error"
9+
And the exception "message" equals "borked"
10+
And the event "metaData.react.componentStack" is not null
11+
And the event "metaData.errorBoundary.caughtByErrorBoundary" is true
12+
And the event "metaData.errorBoundary.handlerType" equals "react-native-error-boundary"

0 commit comments

Comments
 (0)