@dwmkerr/react-native-crash-tester
TypeScript icon, indicating that this package has built-in type declarations

0.5.2 • Public • Published

react-native-crash-tester

Deliberately crash your app. Good for testing crash reporting services.

Installation

npm install react-native-crash-tester

or

yarn add react-native-crash-tester

Usage

Trigger a Native Crash

import CrashTester from 'react-native-crash-tester';

CrashTester.nativeCrash();
CrashTester.nativeCrash('Custom message!');

Implementation notes:

  • For Android, throws a RuntimeException
  • For iOS, calls fatalError which causes a non-recoverable crash

Trigger a Native Exception

import CrashTester from 'react-native-crash-tester';

CrashTester.nativeException();
CrashTester.nativeException('Custom message!');

Implementation notes:

  • For Android, throws a RuntimeException
  • For iOS, raises a NSException, which can potentially be caught by an exception handler

Trigger a JavaScript Crash

import CrashTester from 'react-native-crash-tester';

CrashTester.jsCrash();
CrashTester.jsCrash('Custom message!');

Implementation notes:

  • Calls throw Error to trigger an exception

Block the UI Thread

Blocking the UI thread can be useful to test things like Watchdog Terminations:

import CrashTester from 'react-native-crash-tester';

// block the UI thread for 60 seconds...
CrashTester.blockUIThread(60);

Implementation notes:

  • For iOS, calls sleepForTimeInterval on dispatch_get_main_queue to block the UI thread

Test a React Error Boundary

import * as React from 'react';
import { Button } from 'react-native';
import { CrashingComponent } from 'react-native-crash-tester';
import ErrorBoundary from './MyErrorBoundary';

function TestComponent() {
  const [shown, setShown] = React.useState(false);

  <ErrorBoundary>
    <Button title="Crash" onPress={() => setShown(true)} />
    {shown && <CrashingComponent />}
  </ErrorBoundary>;
}

Implementation notes:

  • Mounts a <View>''</View> component which will throw an exception

You can run the app in ./example to see sample usage.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

Package Sidebar

Install

npm i @dwmkerr/react-native-crash-tester

Weekly Downloads

1

Version

0.5.2

License

MIT

Unpacked Size

112 kB

Total Files

39

Last publish

Collaborators

  • dwmkerr-admin