---
title: App developers
description: >-
  Install react-native-coverage in an Expo or React Native CLI test app, flush
  at the end of the run, and fail CI when coverage is empty.
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.page/invertase/react-native-coverage/llms.txt
> Use this file to discover all available pages before exploring further.

<Warning>
  Install `react-native-coverage` into a [dedicated test app](/coverage#dedicated-test-app) only — never your shipping product app. Autolinking scans dependencies, so keep the package out of the production `package.json` entirely, not just in `devDependencies`.
</Warning>

## Before you begin

- The [test app](/coverage#dedicated-test-app) runs React Native's New Architecture. The flusher is a TurboModule and does not run on the old bridge.
- Your end-to-end app is that test app, such as a `tests/` or `e2e/` workspace app.
- You have a test runner (Appium, Detox, or Maestro) that can call `Coverage.flush()` when the suite ends.

## Install in the test app

<Tabs groupId="pm">
  <TabItem label="yarn" value="yarn">
    ```sh
    yarn add react-native-coverage
    ```
  </TabItem>
  <TabItem label="npm" value="npm">
    ```sh
    npm install react-native-coverage
    ```
  </TabItem>
</Tabs>

Run the command in the test app's folder, not the repository root.

## Configure the build

<Tabs groupId="host">
  <TabItem label="Expo" value="expo">
    Add the config plugin, then prebuild. The plugin applies the Android Gradle helpers and adds the iOS Podfile helper call.

    <Steps>
      <Step title="Add the plugin to the `app.json` or `app.config.js` file">
        ```json
        {
          "expo": {
            "plugins": [
              [
                "react-native-coverage",
                {
                  "libraryProjectMatchers": ["my-lib"],
                  "frameworkNamePrefixes": ["MyLib"],
                  "enableAndroidCoverage": true,
                  "forceDynamicFrameworks": false
                }
              ]
            ]
          }
        }
        ```

        - `libraryProjectMatchers`: Android library projects, matched by name substring, that you want JaCoCo hits from.
        - `frameworkNamePrefixes`: iOS framework name prefixes whose `LINKEDIT` counters are flushed.
        - `forceDynamicFrameworks`: `false` under Expo, where `React-Core` is a static library.
      </Step>
      <Step title="Prebuild">
        ```sh
        npx expo prebuild
        ```
      </Step>
    </Steps>

    For more information, see [Android](/integration/android) and [iOS](/integration/ios).
  </TabItem>

  <TabItem label="React Native CLI" value="cli">
    Apply the Gradle and CocoaPods helpers that ship with the package.

    <Steps>
      <Step title="Edit the root `android/build.gradle` file">
        ```gradle
        ext.coverageLibraryProjectMatchers = ['my-lib']
        def rnCoverageRoot = new File(
          ["node", "--print", "require.resolve('react-native-coverage/package.json')"]
            .execute(null, rootDir).text.trim()
        ).parentFile
        apply from: new File(rnCoverageRoot, "android/rn-coverage.gradle")
        ```
      </Step>
      <Step title="Edit the `android/app/build.gradle` file">
        ```gradle
        android { buildTypes { debug { testCoverageEnabled true } } }

        def rnCoverageRoot = new File(
          ["node", "--print", "require.resolve('react-native-coverage/package.json')"]
            .execute(null, rootDir).text.trim()
        ).parentFile
        apply from: new File(rnCoverageRoot, "android/rn-coverage-jacoco.gradle")
        ```
      </Step>
      <Step title="Edit the `Podfile` file">
        ```ruby
        require_relative '../node_modules/react-native-coverage/cocoapods/coverage_post_install'

        # After use_expo_modules!, when present
        ReactNativeCoverage.install_installer_hooks!

        post_install do |installer|
          ReactNativeCoverage.apply_post_install!(
            installer,
            framework_name_prefixes: ['MyLib'],
            force_dynamic_frameworks: true # only when React itself is dynamic
          )
        end
        ```
      </Step>
      <Step title="Install pods">
        ```sh
        cd ios && pod install
        ```
      </Step>
    </Steps>

    When the test app needs its own bundle ID, product name, or framework prefixes, copy the `react-native-coverage.config.js.example` file to the `react-native-coverage.config.js` file. For more information, see [Configuration](/reference/config).
  </TabItem>
</Tabs>

## Flush at the end of the test run

When the suite ends, call `Coverage.flush()` one time. It writes the native counters and, when you start Metro with `RN_COVERAGE_JS=1`, the Istanbul `global.__coverage__` object.

```ts
import Coverage from 'react-native-coverage';

// In an afterAll hook, reached through a testID button or a deep link
await Coverage.flush();
```

Your test runner decides when. The package does not ship Appium, Detox, or Maestro. When you want JavaScript coverage, set `RN_COVERAGE_JS=1` before you start Metro. The order of a run is:

<Steps>
  <Step title="Start Metro">
    Start Metro for the test app.
  </Step>
  <Step title="Launch the app">
    Launch the test app on the simulator or emulator.
  </Step>
  <Step title="Run the suite">
    Drive the app with your test runner.
  </Step>
  <Step title="Flush one time">
    When the last test has finished, call `Coverage.flush()` from the app. The files land in the app container: `files/coverage.ec` on Android, `.profraw` files in the simulator container on iOS, and `coverage-final.json` on both when JavaScript is instrumented.
  </Step>
  <Step title="Pull and report on the host">
    Run the `rn-coverage` commands in the next section.
  </Step>
</Steps>

## Pull, report, and fail CI on empty coverage

<Steps>
  <Step title="Pull the files from the device">
    ```sh
    rn-coverage android pull
    rn-coverage ios pull --device UDID
    ```
  </Step>
  <Step title="Turn them into reports">
    ```sh
    rn-coverage android report
    # Default JaCoCo XML: android/app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml
    # Copy or set android.jacocoReportXml / --jacoco-xml if you want coverage/android/jacocoTestReport.xml

    rn-coverage ios export --derived-data DERIVED_DATA_PATH
    rn-coverage ios report --derived-data DERIVED_DATA_PATH
    ```
  </Step>
  <Step title="Fail the job when coverage is empty">
    ```sh
    rn-coverage assert
    ```
  </Step>
</Steps>

`rn-coverage assert` exits 0 when the configured paths have hits. It exits 2 when a report is missing or empty, so the CI job fails. Keep strict mode, the default, in CI. For more information, see [Empty or missing coverage](/troubleshooting/empty-coverage).

For JavaScript and TypeScript coverage remapped to your TypeScript sources, add `rn-coverage js pull` and `rn-coverage js report` from [JavaScript and TypeScript](/integration/js).

## Optional: upload to Codecov

Upload each report under its own flag so iOS, Android, and JavaScript stay separate on the dashboard. For more information about how Codecov relates to this package, see [Upload to Codecov](/integration/codecov).

**Single test app** flags (typical consumer CI — one iOS cell, one Android cell, optional JS):

```sh
# After android report, either upload the Gradle default path or a copied file:
#   android/app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml
# or coverage/android/jacocoTestReport.xml (only if you copied / set --jacoco-xml)
codecov -f coverage/ios/lcov.info -F e2e-ios
codecov -f coverage/android/jacocoTestReport.xml -F e2e-android
codecov -f coverage/js/lcov.info -F e2e-js
```

This repository's own CI uses a **matrix** of flags (`e2e-ios-dynamic`, `e2e-ios-static`, `e2e-android`, `unit-js`) — see [Coverage](/coverage#example-from-this-package) and [Library maintainers](/library-maintainers#upload-one-codecov-flag-per-platform). Do not copy those matrix names into a single-app pipeline unless you run the same split.

## Next steps

<CardGroup cols={3}>
  <Card title="Android" icon="android" href="/integration/android">
    Gradle helpers, the Emma `.ec` file, and JaCoCo.
  </Card>
  <Card title="iOS" icon="apple" href="/integration/ios">
    The CocoaPods helper, static and dynamic frameworks, and LCOV export.
  </Card>
  <Card title="JavaScript and TypeScript" icon="js" href="/integration/js">
    Instrument Metro and remap to TypeScript lines.
  </Card>
  <Card title="CI with Appium" icon="gears" href="/integration/ci-appium">
    Run the job on GitHub Actions.
  </Card>
  <Card title="CLI" icon="terminal" href="/reference/cli">
    Every command, flag, and exit code.
  </Card>
  <Card title="Configuration" icon="sliders" href="/reference/config">
    Every key in the `react-native-coverage.config.js` file.
  </Card>
</CardGroup>
