Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviceInfo breaks Detox Tests #335

Closed
blumendorf opened this issue Feb 23, 2018 · 16 comments
Closed

DeviceInfo breaks Detox Tests #335

blumendorf opened this issue Feb 23, 2018 · 16 comments

Comments

@blumendorf
Copy link

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.

@machour
Copy link
Contributor

machour commented Feb 23, 2018

Hi @blumendorf, shouldn't you mock react-native-device-info in your tests?

Here's how I do that for jest:

jest.mock('react-native-device-info', () => {
  return {
    getModel: jest.fn(),
  };
});

@machour machour added the docs label Feb 23, 2018
@blumendorf
Copy link
Author

Fair point. Will give that a try. Thanks!

I would still be interested to understand why it works when I run the app myself, but not when detox runs the app.

@machour
Copy link
Contributor

machour commented Feb 23, 2018

Because it contains native code, something detox/jest can't make up

@machour
Copy link
Contributor

machour commented Feb 23, 2018

Just documented it in the README file: https://github.com/rebeccahughes/react-native-device-info#troubleshooting

Feel free to reopen if you still facing a problem with tests :)

@machour machour closed this as completed Feb 23, 2018
@blumendorf
Copy link
Author

Not sure that I get it. The same bundle runs in the Android simulator and works fine when I "click the buttons". Jest runs the same code and connects to "click the buttons" for me. The exact same code and test (not the linked repository) works perfectly fine in iOS simulator. No mocking required.

Thanks for your help and patience.

@machour
Copy link
Contributor

machour commented Feb 23, 2018

From what I understand from https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock , jest comes with some default mocks, like buttons, which is how it works for you.

For other libraries, like this one, you need to manually mock the objects.

@blumendorf
Copy link
Author

Hello @machour,

thank you very much for your help here. I really appreciate it!
I have tried your suggestion and it does not seem to change anything. I have been working with mobile app development and react-native for a couple of weeks only, so I might be missing something trivial here.

Let me try to explain again what I am doing:
We are running e2e test for our app using detox and jest in iOS simulator and Android emulator running locally and on our build system. We are not mocking anything in our tests (they work the same both with mocha and jest), because we are actually testing the full functionality. Our app works fine on iOS and Android, our e2e test run fine on iOS (locally and on circle ci). Running our e2e tests on the Android emulator, we only see the splash screen and then the app hangs until the test times out. Basically this happens "inside" detox.init(config). I took me some time to narrow this down to wether or not we are importing the 'react-native-device-info' module (I am not saying we don't have more issues once that is fixed though).

I added the iOS code to https://github.com/ewyso/react-native-detox-e-android and this seems to be the minimum implementation to demonstrate the issue. Once the import (line 17 in App.js) is in, ios.sim.debug still runs fine while android.emu.debug hangs with no apparent error.

Since it is the module import that makes the difference, I am not sure where to look next ...
I am happy to dig deeper into the issue (it might also be a detox/jest problem), but would appreciate any pointer where to look next.

@machour machour reopened this Feb 23, 2018
@machour
Copy link
Contributor

machour commented Feb 23, 2018

My bad, I didn't really get that you were doing e2e tests 😄

I just cloned your project and first tried on iOS.
After building the app and running yarn e2e:ios:debug I get this and nothing happens (no logs, no test result, nothing in the simulator)

screen shot 2018-02-24 at 12 22 17 am

Shouldn't it be passing? (the line in App.js is still commented)

@blumendorf
Copy link
Author

blumendorf commented Feb 24, 2018

Should have been more clear about the e2e part. Sorry.

I am running it with detox build --configuration android.emu.debug && detox test --configuration android.emu.debug -l verbose and detox build --configuration ios.sim.debug && detox test --configuration ios.sim.debug -l verbose.

I fixed the yarn command in the repo.

@machour
Copy link
Contributor

machour commented Feb 24, 2018

I was unable to run detox with iOS, but it worked for Android :)

I've successfully reproduced your problem and pinpointed it to this particular line.

Could you try commenting it in your node_modules/ copy and re run your test suite?

@machour machour added bug and removed docs labels Feb 24, 2018
@machour
Copy link
Contributor

machour commented Feb 24, 2018

Someone seems to have faced the same problem here: wix/Detox#395 (comment)

@blumendorf
Copy link
Author

Removing this line brings me past the splash screen in both the react-native-detox-e-android and our project. Thanks for taking the time to look into it. I am very curious about the solution now.

@machour
Copy link
Contributor

machour commented Feb 25, 2018

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

@ghsdh3409
Copy link
Contributor

ghsdh3409 commented May 8, 2018

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);
}

@kormang
Copy link

kormang commented May 16, 2018

@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 :) )

@machour
Copy link
Contributor

machour commented May 17, 2018

Many thanks to both of you!
@blumendorf, could you try the proposed solutions and update us here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants