react-native: Cannot test WebView - Cannot read property 'decelerationRate' of undefined

Description

I updated from react-native 0.39 to 0.41.2. Now, WebView tests fail.

Reproduction

My example is more complicated (I shallow render a component containing a WebView) but I can reproduce it with the following snippet:

import React from 'react';
import { shallow } from 'enzyme'
import { WebView } from 'react-native';

describe('Test', () => {
    it('test', () => {
        shallow(<WebView />);
    });
});
TypeError: Cannot read property 'decelerationRate' of undefined

      at Object.<anonymous> (node_modules\react-native\Libraries\Components\WebView\WebView.ios.js:541:3254)
      at Object.get WebView [as WebView] (node_modules\react-native\Libraries\react-native\react-native.js:69:22)
      at Object.<anonymous> 

I’m sure it’s not an enzyme bug as I can reproduce it with ‘react-test-renderer’ as well.

Solution

?

Additional Information

  • React Native version: 0.41.2 (React: 15.4.2)
  • Platform: neither
  • Operating System: At least Windows and CentOS

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 23
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I was able to get around the error by calling jest.unmock('ScrollView') before my test

It has something to do with Jest automatically mocking ScrollView, which WebView needs to validate propTypes in WebView.ios.js: decelerationRate: ScrollView.propTypes.decelerationRate,

I’m still getting a console.error of Warning: Stateless function components cannot be given refs (See ref "webview" in StatelessComponent created by WebView). Attempts to access this ref will fail. even though my component is not stateless, but my tests are passing.

Have you tried jest.mock('WebView', () => 'WebView')? That worked for us.

this should be reopend because the bug is still there

Just want to add that this still occurs in 0.42 and also affects MacOS.