question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug running Jest with ScrollView dependant components

See original GitHub issue

Description

When running npm run test, the default tests index.ios.js and index.android.js fail due to the following error:

    TypeError: _this._scrollView.scrollTo is not a function

      at TabViewPagerScroll._this._scrollTo (node_modules/react-native-tab-view/src/TabViewPagerScroll.js:68:19)
      at TabViewPagerScroll.componentDidMount (node_modules/react-native-tab-view/src/TabViewPagerScroll.js:123:197)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:265:25
      at measureLifeCyclePerf (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:75:12)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:264:11
      at CallbackQueue.notifyAll (node_modules/react-test-renderer/lib/CallbackQueue.js:76:22)
      at ReactTestReconcileTransaction.close (node_modules/react-test-renderer/lib/ReactTestReconcileTransaction.js:36:26)
      at ReactTestReconcileTransaction.closeAll (node_modules/react-test-renderer/lib/Transaction.js:206:25)
      at ReactTestReconcileTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:153:16)
      at batchedMountComponentIntoNode (node_modules/react-test-renderer/lib/ReactTestMount.js:69:27)

Reproduction

$ react-native init Test
$ cd Test/
$ yarn add react-navigation
$ npm run test

Solution

I’m open to send a PR with a fix if somebody can guide me through this. Could be here https://github.com/facebook/react-native/blob/dbe555ba78d7b0f79e482c62787db2fc3d897848/Libraries/Components/ScrollView/__mocks__/ScrollViewMock.js?

Additional Information

  • React Native version: 0.42.0
  • Platform: both
  • Operating System: macOS Sierra
  • Dev tools: iOS 10.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
jurassixcommented, Apr 6, 2017

I ran into this issue and was able to mock this simply:

jest.mock('ScrollView', () => jest.genMockFromModule('ScrollView'));

Also, I’m mocking the Linking module as well from here that @alvaromb provided:

jest.mock('Linking', () => ({
  addEventListener: jest.fn(),
  removeEventListener: jest.fn(),
  openURL: jest.genMockFn().mockReturnValue(Promise.resolve()),
  canOpenURL: jest.genMockFn().mockReturnValue(Promise.resolve()),
  getInitialURL: jest.genMockFn().mockReturnValue(Promise.resolve()),
}))

Note, I’ve configured this to be run as part of Jests setupFiles configuration.

5reactions
sam-vdpcommented, Nov 8, 2017

@minhhai2209 Great solution! One small improvement, your code broke all my existing snapshot tests, because the ScrollView mock shipped with react-native renders slightly differently. This worked for me:

jest.mock('ScrollView', () => {
  const MockScrollView = require.requireMock('ScrollViewMock');
  const React = require('React');  
  const RealScrollView = require.requireActual('ScrollView');
  class ScrollView extends React.Component {
    scrollTo() {
    }

    render() {
      return (
        <MockScrollView {...this.props} />
      );
    }
  }
  ScrollView.propTypes = RealScrollView.propTypes;
  return ScrollView;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is ScrollView hiding its children when I examine it using ...
Whenever I try to mount something with a ScrollView component, I can't seem to see below it in the component tree. Here is...
Read more >
jest-worker | Yarn - Package Manager
jest -worker ... Module for executing heavy tasks under forked processes in parallel, by providing a Promise based interface, minimum overhead, and bound...
Read more >
react-native-swiper - npm
Start using react-native-swiper in your project by running `npm i react-native-swiper`. ... The best Swiper component for React Native.
Read more >
Common bugs in React Native ScrollView and how to fix them
React Native's ScrollView component is ubiquitous, but its implementation can sometimes lead to mistakes. Learn what to look out for here.
Read more >
20+ Perfect React Native Scroll Component Examples - Morioh
A ScrollView-like component that: Has a parallax background; Has a parallax foreground; Has a fixed or sticky header; Can be nested within other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found