3

I would like to pass environment variable when I run detox tests in my react-native app:

 "ios.sim.debug": {
    "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/myapp.app",
    "build": "export IS_DETOX=true && xcodebuild -workspace ios/myapp.xcworkspace -scheme my app -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
    "type": "ios.simulator",
    "device": {
      "type": "iPhone 11 Pro"
    }
  }

I've installed react-native-config. But the variable IS_DETOX is undefined in JS when I run detox tests.

Thanks

GChevass
  • 205
  • 3
  • 9
  • Your `IS_DETOX` variable is defined in the build step. Why would you expect it to be there in runtime? – Léo Natan Jan 15 '20 at 17:15
  • do you know how can I made this variable accessible in runtime then? I want for instance to disable animations and toast/alert messages in the detox build thanks to an env var like IS_DETOX – GChevass Jan 15 '20 at 21:04
  • You can use launch arguments, environment variables for the process or mock your stuff. Read the Detox docs. – Léo Natan Jan 15 '20 at 22:11
  • @GChevass Have you figured out how to do it? Could you please post an answer if you did so? – Black Apr 24 '20 at 12:42
  • I was able to make it running with `.env` in root folder. It got pick automatically by Detox – Black Apr 24 '20 at 14:01
  • @Black and what if I don't want to use default .env file but something like .env.test? – Vladislav Vodicka Nov 28 '22 at 14:57

2 Answers2

7

I am setting environmental variable without export.

All my .env variables are stored in config folder and I am able to load different environmental file for different configurations.

iOS

"ios.sim.release": {
  "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/Clim8Invest.app",
  "build": "ENVFILE=config/.env.acceptance xcodebuild -workspace ios/Clim8Invest.xcworkspace -scheme Clim8Invest -configuration Release -sdk iphonesimulator -derivedDataPath ios/build",
  "type": "ios.simulator",
  "device": {
    "type": "iPhone 11"
  }
},

Android

"android.emu.debug.e2e": {
  "binaryPath": "android/app/build/outputs/apk/debug/app-debug.apk",
  "build": "cd android && ENVFILE=../config/.env.e2e ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && cd ..",
  "type": "android.emulator",
  "device": {
    "avdName": "Nexus_5X_A"
  }
}
Black
  • 9,541
  • 3
  • 54
  • 54
2

Thanks @Black !

On my side I ended up using another solution with RN_SRC_EXT=e2e.js from Detox Mocking Guide so that the detox build is using params from MyConfig.e2e.js file instead of MyConfig.js file

GChevass
  • 205
  • 3
  • 9