Ishan Gupta
Ishan Gupta

Reputation: 21

How to mock @react-native-community/geolocation with detox?

Describe the Bug

Detox test crashes when used with Geolocation module

Error

Error: @react-native-community/geolocation: NativeModule.RNCGeolocation is null. To fix this issue try these steps:
• Run `react-native link @react-native-community/geolocation` in the project root.
• Rebuild and re-run the app.
• If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods.
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-geolocation

<global>
    nativeInterface.js:17:8
loadModuleImplementation
    require.js:322:6
<global>
    implementation.native.js:11
loadModuleImplementation
    require.js:322:6
<global>
    index.js:11
loadModuleImplementation
    require.js:322:6
<global>
    TravelTimeContainer.js:3
loadModuleImplementation
    require.js:322:6
<global>
    index.js:1
loadModuleImplementation
    require.js:322:6
<global>
    MyDay.js:5
loadModuleImplementation
    require.js:322:6
<global>
    BottomTabNavigator.js:14
loadModuleImplementation
    require.js:322:6
<global>
    AppNavigator.js:16
loadModuleImplementation
    require.js:322:6
<global>
    Setup.js:8
loadModuleImplementation
    require.js:322:6
<global>
    App.js:2
loadModuleImplementation
    require.js:322:6
<global>
    index.js:6
loadModuleImplementation
    require.js:322:6
guardedLoadModule
    require.js:201:45
global code
    index.bundle?platform=ios&dev=true&minify=false:195242:4

Environment (please complete the following information):

Note

Please provide a solution to the same.

Upvotes: 2

Views: 3260

Answers (1)

dazza5000
dazza5000

Reputation: 7628

There was a solution that worked for me that was posted on github here:

https://github.com/react-native-community/react-native-geolocation/issues/44#issuecomment-512720306

Hey @SergejDK , you need to mock the RNCGeolocation native module. Create a folder __mocks__ on the root level. Inside already created folder create another folder @react-native-community and within that folder create a file called geolocation.js, where you have to specify mocks:

export default {
  addListener: jest.fn(),
  getCurrentPosition: jest.fn(),
  removeListeners: jest.fn(),
  requestAuthorization: jest.fn(),
  setConfiguration: jest.fn(),
  startObserving: jest.fn(),
  stopObserving: jest.fn()
};

Folders structure should look like:

.
├── __mocks__/
│   │── @react-native-community/ 
│       | -- geolocation.js

Upvotes: 4

Related Questions