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.

Jest - TypeError: Cannot read property 'Commands' of undefined

See original GitHub issue

Do you want to request a feature or report a bug?

bug - related to jest

What is the current behavior?

The error TypeError: Cannot read property 'Commands' of undefined is thrown when i trigger a function that references a <ListView/> and calls the scrollTo method of the <ListView/> like below:

// This is a react-native app
// MyComponentTest.js
it('test scrollTo', () => {
  const tree = renderer.create(<MyComponent/>).toJSON()
  tree.children[0].props.onPress()
})

// MyComponent.js
class MyComponent extends Component {
  _scrollToTop = () => {
    this.refs.listView.scrollTo({x:0,y:0})
  }

  render() {
    return (
      <View>
        <TouchableHighlight onPress={this._scrollToTop}/>
        <ListView 
          ref="listView"
          {...moreProps}
        />
      </View>
    )
  }
}

When i run the tests, the stack trace ends at ‘Component.scrollResponderScrollTo (node_modules/react-native/Libraries/Components/ScrollResponder.js:380:24)’

The error is caused by the line ‘this.refs.listView.scrollTo({x:0,y:0})’…

Package Versions

I run the react-native app with the following package configuration: “react”: “15.4.1” “react-native”: “0.39.2” “babel-jest”: “17.0.2” “jest”: “17.0.3” “react-test-renderer”: “15.4.1” “babel-preset-react-native”: “1.9.0”

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
fasibiocommented, Jan 5, 2017

A Workaround to fix the Problem with srcollTo:

jest.mock('ListView', () => {
  const RealComponent = require.requireActual('ListView')
  class ListView extends RealComponent {
    constructor(props){
      super(props)
    }
    scrollTo = () => {

    }
  }
  return ListView
})

0reactions
GokulDharumarcommented, Jun 12, 2018

+1 same issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'commands' of undefiened (discord.js)
I am trying to code in / commands into my discord bot. But I keep on getting this error: Cannot read property 'commands'...
Read more >
typeerror: cannot read properties of undefined ... - You.com
Try logging the command.data object to see if anything pops up. That's probably the error. Open side panel.
Read more >
Jest: Cannot read properties of undefined (reading ...
If the error persists, make sure that the testEnvironment property is set to jsdom in your package.json or jest.config.js or jest.config.ts file.
Read more >
Unit tests - Mattermost Developers
Jest and Enzyme are the main framework and testing utilities used in testing components and ... TypeError: Cannot read property 'filter' of undefined....
Read more >
M220 Error:Cannot read property 'close' of undefined - M220JS
... for testing with jest it return a test Suites : failed and type Error : Cannot read property 'close' of undefined please...
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