Jest: React Native snapshots fail with "TypeError: Cannot read property '_tag' of undefined" for components containing <TextInput>

Created on 6 Aug 2016  ·  3Comments  ·  Source: facebook/jest

To reproduce:

  1. Modify https://github.com/facebook/jest/blob/master/examples/react-native/Intro.js to add a <TextInput /> in render()
  2. npm test

Result:

FAIL tests/Intro-test.js (0.459s)
● Intro › it renders correctly

  • TypeError: Cannot read property '_tag' of undefined
    at ReactNativeBaseComponent.Mixin.mountComponent (node_modules/react/lib/ReactNativeBaseComponent.js:179:45)
    at Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:47:35)
    at ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:397:34)
    at ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:262:21)
    at Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:47:35)
    at ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:397:34)
    at ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:262:21)
    at Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:47:35)
    at ReactMultiChild.Mixin.mountChildren (node_modules/react/lib/ReactMultiChild.js:242:44)
    at ReactTestComponent.mountComponent (node_modules/react/lib/ReactTestRenderer.js:53:8)

Most helpful comment

Per https://facebook.github.io/jest/docs/tutorial-react-native.html#mock-native-modules-using-jest-mock, the workaround is to add

jest.mock('TextInput', () => 'TextInput');

to the beginning of your test.

All 3 comments

Per https://facebook.github.io/jest/docs/tutorial-react-native.html#mock-native-modules-using-jest-mock, the workaround is to add

jest.mock('TextInput', () => 'TextInput');

to the beginning of your test.

We should provide a default mock for TextInput.

Was this page helpful?
0 / 5 - 0 ratings