# Running Detox tests on Bitrise

Detox is a gray box end-to-end tests and automation library for mobile apps built with React Native. It supports both iOS and Android apps. If you have a React Native app on Bitrise, you can run Detox tests.

## Before you start

Running Detox requires:

- A Mac with a macOS (El Capitan 10.11 or newer version).
- Xcode 8.3 or newer version with Xcode command line tools.
- A working React Native app.

[Install and set up Detox for your project](https://wix.github.io/Detox/docs/introduction/getting-started/). You will need to install Homebrew, Node.js and applesimutils, as well as the Detox command line tools. Add Detox to your project and then create and run Detox tests locally. If you have an Android app, go through [this guide](https://wix.github.io/Detox/docs/guide/android-dev-env/) after the initial setup process.

Once you are done, you can test your Detox-configured project on Bitrise.

## Running a Detox test

1. Create a release device configuration either in the `package.json` file under the `detox` section, or in a separate Detox configuration file. For more information on setting up a Detox configuration, check out the documentation: [Project setup](https://wix.github.io/Detox/docs/introduction/project-setup/).

   Example:

   ```yaml
   "detox": {
    "configurations": {
      "ios.sim.debug": {
        "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/SampleProjectReactNative.app",
        "build": "xcodebuild -project ios/SampleProjectReactNative.xcodeproj -scheme SampleProjectReactNative -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build -UseNewBuildSystem=NO",
        "type": "ios.simulator",
        "name": "iPhone 8"
      },
      "ios.sim.release": {
        "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/SampleProjectReactNative.app",
        "build": "xcodebuild -project ios/SampleProjectReactNative.xcodeproj -scheme SampleProjectReactNative -configuration Release -sdk iphonesimulator -derivedDataPath ios/build -UseNewBuildSystem=NO",
        "type": "ios.simulator",
        "name": "iPhone 8"
      }
    },
   ```
1. On [bitrise.io](https://www.bitrise.io/), go to your project and open the **Workflow Editor.**
1. Switch to the Workflow you want to use.
1. Add a **Run npm command** Step to your Workflow.
1. Install the Detox CLI and the React Native CLI using the **npm command with arguments to run** input:

   ```bash
   install -g detox-cli
   install -g react-native-cli
   ```
1. Install a test runner.

   For example, [our sample app](https://github.com/bitrise-samples/sample-project-react-native) uses `mocha`, installed with the `yarn` Step. To install yarn dependencies, just set the **The yarn command to run** input’s value to `install`.
1. Add a **Script** Step to install the necessary utilities and then run Detox.

   ```bash
   #!/bin/bash

   # applesimutils is a collection of utils for Apple simulators
   brew tap wix/brew
   brew install applesimutils

   # we are building and testing a release device configuration
   detox build --configuration ios.sim.release
   detox test --configuration ios.sim.release --cleanup
   ```

   You can, of course, put each of these commands in separate Script Steps, for the sake of modularity.
1. Run a build.

If the build fails, check out our example `bitrise.yml` file:

```yaml
---
workflows:
  primary:
    steps:
    - activate-ssh-key: {}
    - git-clone:
        inputs:
        - clone_depth: ''
        title: Git Clone Repo
    - yarn:
        inputs:
        - command: install
    - npm:
        inputs:
        - command: install -g detox-cli
        title: Install Detox CLI
    - script:
        inputs:
        - content: |-
            #!/bin/bash
            brew tap wix/brew
            brew install applesimutils
            
            detox build --configuration ios.sim.release
            detox test --configuration ios.sim.release --cleanup
        title: Detox - Build and Test Release App
    - deploy-to-bitrise-io: {}
```

## Troubleshooting Detox tests

If you run into issues with using Detox on Bitrise, we recommend trying to rebuild the entire Detox package before running the test.

To do so, run the following command in your Bitrise build:

```text
`npm rebuild detox`
```

If your Detox tests still fail or hang, contact our support!
