react-nativedetox

How to tell Detox is running tests?


I'm using Detox to run end to end tests in my React Native project. I'm also using pretender.js to mock my API requests and I'm struggling to find a way to know if the app is currently in "testing" mode.

I was passing an env variable down (and using babel-transform-inline-environment-variables) to tell if I should mock the requests but that breaks shim.js in our release builds.

Is there any way to tell Detox launched the app & is running tests from within the JS? Ideally I'm looking for some sort of variable set at test time or something passed down from the command line (TESTING=true react-native start or __TESTING__)


Solution

  • Try using react-native-config. Here is also a good article on Managing Configuration in React Native with react-native-config.

    I also gave an answer here animated-button-block-the-detox with working example of how react-native-config can be used to disable looping animations during testing.

    The basic idea is that you create .env config files for all your different build environments (development, production, test, etc). These hold your configuration variables that you can access from either Javascript, Objective-C/Swift, or Java.

    You then specify which .env config file to use when building your app:

    $ ENVFILE=.env.staging react-native run-ios # bash

    And this is an example of package.json file where detox uses .env config files for building the app.

    "detox": {
      "specs": "e2e",
      "configurations": {
        "ios.sim.release": {
          "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/example.app",
          "build": "ENVFILE=.env.production export RCT_NO_LAUNCH_PACKAGER=true && xcodebuild -project ios/example.xcodeproj -scheme example -configuration Release -sdk iphonesimulator -derivedDataPath ios/build",
          "type": "ios.simulator",
          "name": "iPhone 5s, iOS 10.3"
        },
        "ios.sim.test": {
          "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/example.app",
          "build": "ENVFILE=.env.testing xcodebuild -project ios/example.xcodeproj -scheme example -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build -arch x86_64",
          "type": "ios.simulator",
          "name": "iPhone 5s, iOS 10.3"
        }
      }
    }