Search code examples
react-nativemockingjestjsreact-native-router-flux

How to mock react native I18nManager


I am trying to run jest cases in react native. I am using react-native-router-flux for navigation. My test cases are being failed with this error

Test suite failed to run

    TypeError: Cannot read property 'isRTL' of undefined

      at Object.<anonymous> (node_modules/react-native-router-flux/dist/NavBar.js:286:44)
      at Object.<anonymous> (node_modules/react-native-router-flux/dist/navigationStore.js:7:13)
      at Object.<anonymous> (node_modules/react-native-router-flux/dist/Reducer.js:74:36)

Exact line is transform:[{scaleX:_reactNative.I18nManager.isRTL?-1:1}]})

I tried different ways to mock but could not get it

One method is

import {I18nManager} from 'react-native'

I18nManager = {
    isRTL : false
}

I put this snippet in jest initial configuration file but I got error like I18nManager is readonly


Solution

  • Actually I am using an explicit module factory that is being run instead of using Jest's automocking feature which is react-native-mock-render .

    Like this

    jest.mock('react-native', () => require('react-native-mock-render'), {
      virtual: true
    })
    

    When I go through this lib code I could not found I18nManager in the mocks. So I forked that repo and added I18nManager file myself.