7
votes

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?

2

2 Answers

13
votes

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

0
votes

You got this message because of line №217 in /node_modules/react-native/Libraries/Components/WebView/WebView.ios.js

decelerationRate: ScrollView.propTypes.decelerationRate

Because ScrollView is mocked ScrollView.propTypes === undefined

I solved this issue by adding:

import {PropTypes} from 'react';
ScrollView.propTypes = { decelerationRate: PropTypes.number };

to setup script file (file which set by setupTestFrameworkScriptFile property in jest section of package.json);