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__)
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"
}
}
}
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.
Tests/production code that has knowledge of the environment is messy IMO.
The way I recommend doing it is by creating different app flavour for testing.
If you use React Native, check out react-native-repackager's instructions.
Alternatively, Detox docs have that section as well.
If you write Java code for Android, use gradle build flavours to create a flavours for testing.
You can find more on how we mock in our E2E suites here.
Related
I'm using the new expo-dev-client and EAS workflow to attempt development with expo, while also including native dependencies (react-native-firebase in my case).
I've managed to build and install a preview of my app with EAS:
eas build --platform ios --profile staging
Where my "staging" profile is setup in eas.json like so:
{
"cli": {
"version": ">= 0.35.0"
},
"build": {
"production": {
"releaseChannel": "prod-v0",
"node": "14.17.4",
"ios": {
"cocoapods": "1.11.2",
"autoIncrement": "buildNumber"
}
},
"staging": {
"extends": "production",
"releaseChannel": "staging",
"distribution": "internal"
},
...
},
...
}
Note the releaseChannel is set to "staging" too.
So far so good. The app is building and I can install it on my device and confirm the releaseChannel using the expo-updates module with Updates.releaseChannel
Now I'd like to publish a pure javascript change, so I run...
expo publish --release-channel staging
But this gives the following warning (which I assume is why I don't see the update on my device):
ios: Expo.plist key: "EXUpdatesReleaseChannel": The value passed to the
--release-channel flag is to "staging", but it is set to "default".
So what/where is "Expo.plist", why isn't it in-sync, and how can I update it?
Update:
So I deleted the iOS folder from a previous expo run:ios build. The warning is now gone when running expo publish --release-channel staging. But still nothing seems to happen on my app - it still doesn't update.
I need to build the same app to different applicationIds so that I can publish it on the Play Store / App Store as private applications for some of the customers of my company.
I decided to use react-native-config, as it should allow me to change applicationId and some env variables easily.
I have created some .env.${variant} files, that is, in the following examples, .env.customer1.
I have set the needed buildTypes as follows:
...
buildTypes {
debug {
...
}
customer1 {
initWith debug
applicationIdSuffix "customer1"
}
}
I forced react.gradle not to bundle when building with these variants
project.ext.react [
bundleInCustomer1: false,
devDisabledInCustomer1: false
]
Then I use this command line to run on my physical device
copy .env.customer .env && react-native run-android --variant=customer1 --appIdSuffix 'customer1'
The result is that the app is built and launched on my device, but what I see is an old version of the app (probably the last one that I have built using assembleRelease, some weeks ago), metro getting launched but telling me this when I try to force a reload, otherwise telling me nothing
warn No apps connected. Sending "reload" ...
I tried without any success
gradlew clean
npm start --cache-reload
npm cache clean --forced
npm i
Building the app without any variant (thus using default debug) correctly works.
Thanks to this answer, I've succeeded in solving my issue.
Instead of using buildTypes now I'm using flavors.
So,
android {
...
flavorDimensions "standard"
defaultConfig {
applicationId "com.stackoverflow"
...
productFlavors {
customer1 {
applicationId "com.stackoverflow.customer1"
dimension "standard"
}
}
}
and launching via
react-native run-android --variant=customer1Debug --appIdSuffix 'customer1'
I'm hoping to confirm my thought process when trying to test an Android application that's built with Expo's Turtle-CLI tool. I've got iOS running well, just need help getting Android working.
I have a build process that will spit out an .apk to build/android.apk. Here's the command that does that
turtle build:android \
--output $BUILD_DIR/android.apk \
--username $EXPO_USERNAME \
--password $EXPO_PASSWORD \
--config app.config.ts \
--release-channel $RELEASE_CHANNEL \
--type apk \
--mode debug
This successfully outputs the .apk in build.
Here is my detoxrc.json configuration
{
"testRunner": "jest",
"runnerConfig": "e2e/config.json",
"configurations": {
"ios": {
"type": "ios.simulator",
"binaryPath": "build/app-native.app",
"build": "./scripts/build_test_app ios",
"device": {
"type": "iPhone 11"
}
},
"android": {
"type": "android.attached",
"binaryPath": "build/android.apk",
"testBinaryPath": "build/android.apk",
"build": "./scripts/build_test_app android",
"device": {
"adbName": "059aaa47"
}
}
}
}
I then attempt to run my tests with
detox test --configuration android --loglevel trace
Which then yields (more logs at the bottom of this post)
No instrumentation runner found on device 059aaa47 for package com.foobar.mobilern
at ADB.getInstrumentationRunner (../node_modules/detox/src/devices/drivers/android/exec/ADB.js:250:13)
at Instrumentation.launch (../node_modules/detox/src/devices/drivers/android/tools/Instrumentation.js:19:24)
at MonitoredInstrumentation.launch (../node_modules/detox/src/devices/drivers/android/tools/MonitoredInstrumentation.js:18:5)
at AttachedAndroidDriver._launchInstrumentationProcess (../node_modules/detox/src/devices/drivers/android/AndroidDriver.js:284:5)
at AttachedAndroidDriver._launchApp (../node_modules/detox/src/devices/drivers/android/AndroidDriver.js:267:7)
at AttachedAndroidDriver._handleLaunchApp (../node_modules/detox/src/devices/drivers/android/AndroidDriver.js:120:7)
at AttachedAndroidDriver.launchApp (../node_modules/detox/src/devices/drivers/android/AndroidDriver.js:91:12)
at Device._doLaunchApp (../node_modules/detox/src/devices/Device.js:85:19)
at traceCall (../node_modules/detox/src/utils/trace.js:41:20)
If I omit the testBinaryPath key from .detoxrc.json, then I get this result.
Running adb -s 059aaa47 shell pm list instrumentation on the test device yields an empty response, so I'm inclined to believe that some testing setup isn't being completed thoroughly and/or perhaps I'm thinking about this wrong.
I guess the gist of my question is, is it possible to test one .apk without having to make use of some sort of testing harness / binary?
Full Log Output
I have a starter app created with React Native CLI on a Mac, added Detox and am trying to run the sample tests. I get this error (newlines added for easier reading):
$ ./node_modules/.bin/detox -c android test
detox[37289] INFO: [test.js] configuration="android" reportSpecs=true readOnlyEmu=false
useCustomLogger=true forceAdbInstall=false DETOX_START_TIMESTAMP=1591313397594
node_modules/.bin/jest --config e2e/config.json '--testNamePattern=^((?!:ios:).)*$' --
maxWorkers 1 android test
detox[37290] INFO: [DetoxServer.js] server listening on localhost:62332...
detox[37290] ERROR: [DetoxExportWrapper.js/DETOX_INIT_ERROR]
DetoxRuntimeError: Failed to run application on the device
HINT: Most likely, your tests have timed out and called detox.cleanup() while it was
waiting for "ready" message (over WebSocket) from the instrumentation process.
When it runs, the emulator starts but the app does not.
The app runs fine separate from E2E tests, through React Native CLI.
.detoxrc.json
{
"testRunner": "jest",
"runnerConfig": "e2e/config.json",
"configurations": {
"android": {
"type": "android.emulator",
"binaryPath": "android/app/build/outputs/apk/debug/app-debug.apk",
"build": "cd android; ./gradlew assembleDebug assembleAndroidTest -DtestBiuldType=debug; cd -",
"device": {
"avdName": "Pixel_3"
}
}
}
}
Environment:
Detox: 16.7.0
React Native: 0.62.2
Node: v12.17.0
Device: Pixel 3, x86, API 29, Android 10
OS: MacOS 10.15.4
Test-runner: Jest
Android Studio: 3.6.3
My bad. I missed a few steps on the second page of getting started instructions specific to Android.
https://github.com/wix/Detox/blob/master/docs/Introduction.Android.md
I am using detox for testing my RN application
I have a stub for Facebook login in tests like this:
// js/actions/login.e2e.js
function fbAuth() {
console.log('stubbed auth with Facebook');
}
module.exports = { fbAuth };
When I build my app with RN_SRC_EXT=e2e.js react-native run-android and then run the tests detox test -c android.emu.debug it uses file with the stub
When I build my app with react-native run-android and then run the tests it uses non-stub version (real login with Facebook)
My detox config looks as follows
"detox": {
"configurations": {
"android.emu.debug": {
"binaryPath": "android/app/build/outputs/apk/debug/app-debug.apk",
"build": "cd android && RN_SRC_EXT=e2e.js ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && cd ..",
"type": "android.emulator",
"name": "Android_Accelerated_Nougat"
}
}
}
The question is: how can I configure detox to use *e2e.js files in tests without running RN_SRC_EXT=e2e.js react-native run-android before starting the tests?
I've tried
RN_SRC_EXT=e2e.js node_modules/.bin/mocha e2e --opts e2e/mocha.opts --configuration android.emu.debug --grep :ios: --invert
RN_SRC_EXT=e2e.js detox test -c android.emu.debug
but it didn't help
UPDATE:
Actually RN_SRC_EXT=e2e.js react-native run-android doesn't help: I need to stop my Metro process and run RN_SRC_EXT=e2e.js react-native run-android to force RN to use e2e.js files in the test bundle