reactjs - Jest 14 -> 15 upgrade: Element type is invalid: expected a string or a class/function but got: undefined -


after upgrading react-native 0.30 app jest 14.1.0 15.1.0, started getting following error in (but not all) of tests:

invariant violation: element type invalid: expected string (for built-in components) or class/function (for composite components) got: undefined.                                                                                                                    @ invariant (node_modules/fbjs/lib/invariant.js:38:15)                                                                                                                                                                                                                      @ instantiatereactcomponent [as _instantiatereactcomponent] (node_modules/react/lib/instantiatereactcomponent.js:86:134)                                                                                                                                                    @ reactcompositecomponentmixin.performinitialmount (node_modules/react/lib/reactcompositecomponent.js:358:22)                                                                                                                                                               @ reactcompositecomponentmixin.mountcomponent (node_modules/react/lib/reactcompositecomponent.js:241:21)                                                                                                                                                                    @ object.reactreconciler.mountcomponent (node_modules/react/lib/reactreconciler.js:49:35)                                                                                                                                                                                   @ reactcompositecomponentmixin.performinitialmount (node_modules/react/lib/reactcompositecomponent.js:367:34)                                                                                                                                                               @ reactcompositecomponentmixin.mountcomponent (node_modules/react/lib/reactcompositecomponent.js:241:21)                                                                                                                                                                    @ object.reactreconciler.mountcomponent (node_modules/react/lib/reactreconciler.js:49:35)                                                                                                                                                                                   @ reactcompositecomponentmixin.performinitialmount (node_modules/react/lib/reactcompositecomponent.js:367:34)                                                                                                                                                               @ reactcompositecomponentmixin.mountcomponent (node_modules/react/lib/reactcompositecomponent.js:241:21)                                                                                                                                                                    @ object.reactreconciler.mountcomponent (node_modules/react/lib/reactreconciler.js:49:35)                                                                                                                                                                                   @ mountcomponentintonode (node_modules/react/lib/reacttestmount.js:47:31)                                                                                                                                                                                                   @ reacttestreconciletransaction.mixin.perform (node_modules/react/lib/transaction.js:138:20)                                                                                                                                                                                @ batchedmountcomponentintonode (node_modules/react/lib/reacttestmount.js:61:27)                                                                                                                                                                                            @ reactdefaultbatchingstrategytransaction.mixin.perform (node_modules/react/lib/transaction.js:138:20)                                                                                                                                                                      @ object.reactdefaultbatchingstrategy.batchedupdates (node_modules/react/lib/reactdefaultbatchingstrategy.js:63:19)  

these tests using react-test-renderer , following:

import 'react-native' import react 'react' import foo '../foo'  // note: test renderer must required after react-native. import renderer 'react-test-renderer'  describe('<foo />', () => {   it('renders correctly', () => {     const tree = renderer.create(       <foo loading={false} dispatch={() => {}} />     ).tojson()     expect(tree).tomatchsnapshot()   }) }) 

this exact same test file works 1 of components, fails if try of other components. of these tests work under jest 14.1.0.

the error message not descriptive. know might going on?

the problem turned out our use of babel-plugin-rewire. solution ensure enabled rewire mocha , not jest.

in our .babelrc, changed test mochatest:

{   "presets": ["react-native"],   "env": {     "mochatest": {       "plugins": ["babel-plugin-rewire"]     }   } } 

in our test runner, make sure set babel_env mochatest before invoke mocha , not jest.

export babel_env="mochatest" && mocha && export babel_env="jesttest" && jest 

Comments