Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to test shallow rendering with React Native #2365

Open
2 of 13 tasks
hoangpham95 opened this issue Apr 6, 2020 · 3 comments
Open
2 of 13 tasks

Unable to test shallow rendering with React Native #2365

hoangpham95 opened this issue Apr 6, 2020 · 3 comments

Comments

@hoangpham95
Copy link

hoangpham95 commented Apr 6, 2020

Current behavior

I use enzyme 3.10 version to snapshot test with React Native. However, I encounter this error:

this._backHandlerListener = BackHandler.addEventListener("hardwareBackPress", this.handleBackRequested);
                                                               ^

TypeError: Cannot read property 'addEventListener' of undefined
    at SearchBar.componentDidMount SearchBar.tsx:30:49)
    at componentDidMount (/node_modules/enzyme/src/ShallowWrapper.js:429:22)
    at Object.fn [as batchedUpdates] (/node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:713:16)
    at new batchedUpdates (/node_modules/enzyme/src/ShallowWrapper.js:428:26)
    at Object.shallow (/node_modules/enzyme/src/shallow.js:10:10)

This is the code that cause the crash:

const search = (
    <SearchBar
          cancelSearch={cancelSearchSpy}
          searchApps={searchAppsSpy} />
);
 const wrapper = shallow(search)

Expected behavior

This line of code shouldn't crash the test suite.

Your environment

  • macOS Catalina Version 10.15.1
  • MacBook Pro (16-inch 2019)
  • Processor 2.6 GHz Intel Core i7

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.11.0
react 16.8.6
react-dom N/A
react-test-renderer N/A
adapter (below) 1.14.0

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )
@ljharb
Copy link
Member

ljharb commented Apr 6, 2020

To test React Native properly, you'd need a React Native adapter (enzyme-adapter-react-16 is for react-dom).

Track #1436 for that.

Specific to your OP, however, the implication is that BackHandler is undefined. Is this something RN provides? If so, you'd have to mock it.

@hoangpham95
Copy link
Author

RN is providing this. However I suspect this is a native module. I manage to overcome this bug by mocking BackHandler:

BackHandler.addEventListener = jest.fn(// your function here)

@ljharb
Copy link
Member

ljharb commented Apr 9, 2020

To test with RN, you'd have to ensure all the RN things are available in node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants