We are taking advantage of the fact detox invokes your binary with --args -detoxServer ... -detoxSessionId ...
on the iOS command line and { detoxServer: ..., detoxSessionId: ... }
set in InstrumentationRegistry in android.
The way we are currently exposing this to JS is a bit much for a StackOverflow answer, but here's some sample code that along with react native's docs should get you there - for Android:
// This will throw ClassNotFoundException if not running under any test,
// but it still might not be running under Detox
Class<?> instrumentationRegistry = Class.forName("android.support.test.InstrumentationRegistry");
Method getArguments = instrumentationRegistry.getMethod("getArguments");
Bundle argumentsBundle = (Bundle) getArguments.invoke(null);
// Say you're in your BaseJavaModule.getConstants() implementation:
return Collections.<String, Object>singletonMap("isDetox", null != argumentsBundle.getString("detoxServer"));
And on iOS, something like (don't have an Objective-C compiler ATM):
return @{@"isDetox": [[[NSProcessInfo processInfo] arguments] containsObject: @"-detoxServer"]}
Note it's also possible to get detox to add your own arguments with:
detox.init(config, { launchApp: false });
device.launchApp({ newInstance: true, launchArgs: {
myCustomArg: value,
...,
} });
It would be great to get this polished up to a module at some point.