expo / ex-navigation

Route-centric navigation for React Native
997 stars 201 forks source link

Trouble with jest and snapshot tests / mocking #425

Open Dashue opened 7 years ago

Dashue commented 7 years ago

Hitting this error.

TypeError: Cannot read property 'pushListener' of undefined

  at ExNavigationAndroidBackButtonBehavior.enable (node_modules\@exponent\ex-navigation\src\ExNavigationAndroidBackButtonBehavior.js:55:14)
  at ExNavigationAndroidBackButtonBehavior.componentDidMount (node_modules\@exponent\ex-navigation\src\ExNavigationAndroidBackButtonBehavior.js:29:6)
  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)

So far I have this in my test

jest.mock("bugsnag-react-native", () => 'BugSnag')
jest.mock('react-native-intercom', () => 'Intercom')
jest.genMockFromModule('@exponent/ex-navigation')

import 'react-native';
import React from 'react';
import LoginScreen from '../../app/Screens/loginScreen';

import renderer from 'react-test-renderer';

test('renders correctly', () => {
  const tree = renderer.create(
    <LoginScreen />
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

And this in my package.config

"jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|bugsnag-react-native|expo|@expo/*|exponent|@exponent/*)"
    ],

Thanks for any input!

sibelius commented 7 years ago

@Dashue any progress on this?

Dashue commented 7 years ago

Nope, gave up.