Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock react-native module (not 3rd party module) with jest

I am trying to mock a module that ships with react-native (not 3rd party modules), such as LayoutAnimation:

import * as RN from 'react-native'

RN.LayoutAnimation = jest.fn()

But the test fails with:

TypeError: Cannot read property 'decelerationRate' of undefined

  at Object.<anonymous> (node_modules/react-native/Libraries/Components/WebView/WebView.ios.js:555:3254)
  at Object.get WebView [as WebView] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:73:22)

Is there any other way to mock/stub out a RN module such as LayoutAnimation or any other react-native (not 3rd party) module?

like image 954
rcorrie Avatar asked Jul 05 '17 16:07

rcorrie


People also ask

How do you test React Native components using Jest?

Setup​ Run yarn test to run tests with Jest. If you are upgrading your react-native application and previously used the jest-react-native preset, remove the dependency from your package. json file and change the preset to react-native instead.


1 Answers

Try to simply do jest.mock('LayoutAnimation');

like image 135
Antoine Grandchamp Avatar answered Nov 09 '22 21:11

Antoine Grandchamp