react-native-device-info: DeviceInfo breaks Detox Tests

Summary

We are testing our app with detox and mocha (also tried jest) and noticed that our tests fail on Android. Debugging the failure suggests that importing react-native-device-info (var DeviceInfo = require(‘react-native-device-info’) in any module) gets the test stuck in the splash screen (i.e. the initialisation phase).

Here is an example project: https://github.com/ewyso/react-native-detox-e-android/ Just uncomment https://github.com/ewyso/react-native-detox-e-android/blob/master/App.js#L17

Version 0.15.3 (other seem to have the same issue)
Affected OS Android
OS Version Detox Testing on Nexus_5X_API_27

Current behavior

The app works fine running in the Android simulator. The app gets stuck in the splash screen when run from detox. As soon as import or var DeviceInfo = require('react-native-device-info' are added. There are no visible error messages in react-native log-android or adb logcat. The debugger is not connected when running tests.

Steps to reproduce: git clone https://github.com/ewyso/react-native-detox-e-android.git cd react-native-detox-e-android yarn && detox build --configuration android.emu.debug && detox test --configuration android.emu.debug -l verbose

Compare with line 17 in /App.js added / removed.

Expected behavior

Line 17 should not change the behavior of the test at all.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 16 (1 by maintainers)

Most upvoted comments

I temporary fix it by stubbing WebSettings.getDefaultUserAgent() using mockito-android in my Detox test code.

@Test
public void runDetoxTests() throws InterruptedException {
  Context reactContext = InstrumentationRegistry.getTargetContext().getApplicationContext();
  WebSettings webSettings = spy(WebSettings.class);
  doReturn("getDefaultUserAgentMock").when(webSettings).getDefaultUserAgent(reactContext);

  Detox.runTests(mActivityRule);
}

@ghsdh3409 's answer fixed it for me.

I had to put: androidTestImplementation 'org.mockito:mockito-android:2.7.22' to app/build.gradle or for previous versions of gradle: androidTestCompile 'org.mockito:mockito-android:2.7.22'

These are imports:

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.webkit.WebSettings;
import static org.mockito.Mockito.*;

And at the end, I have put @ghsdh3409 's code in DetoxTest.java from androidTest directory.

(For those lazy like me, so that you don’t have to look for your correct imports yourself 😃 )

No worries, I always wanted to see how e2e tests works 😃 I’ve posted details of the problem to the detox thread I mentioned, we’ll see how it goes from there. I also had a look at the android source for WebSettings but couldn’t find anything obvious explaining why the test hangs