【问题标题】:Jest 14 -> 15 upgrade: Element type is invalid: expected a string or a class/function but got: undefinedJest 14 -> 15 升级:元素类型无效:期望字符串或类/函数,但得到:未定义
【发布时间】:2017-01-09 21:02:44
【问题描述】:

在将我的 react-native 0.30 应用程序从 Jest 14.1.0 升级到 15.1.0 后,我开始在大多数(但不是全部)测试中收到以下错误:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.                                                                                                                

  at invariant (node_modules/fbjs/lib/invariant.js:38:15)                                                                                                                                                                                                                   
  at instantiateReactComponent [as _instantiateReactComponent] (node_modules/react/lib/instantiateReactComponent.js:86:134)                                                                                                                                                 
  at ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:358:22)                                                                                                                                                            
  at ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:241:21)                                                                                                                                                                 
  at Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:49:35)                                                                                                                                                                                
  at ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:367:34)                                                                                                                                                            
  at ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:241:21)                                                                                                                                                                 
  at Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:49:35)                                                                                                                                                                                
  at ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:367:34)                                                                                                                                                            
  at ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:241:21)                                                                                                                                                                 
  at Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:49:35)                                                                                                                                                                                
  at mountComponentIntoNode (node_modules/react/lib/ReactTestMount.js:47:31)                                                                                                                                                                                                
  at ReactTestReconcileTransaction.Mixin.perform (node_modules/react/lib/Transaction.js:138:20)                                                                                                                                                                             
  at batchedMountComponentIntoNode (node_modules/react/lib/ReactTestMount.js:61:27)                                                                                                                                                                                         
  at ReactDefaultBatchingStrategyTransaction.Mixin.perform (node_modules/react/lib/Transaction.js:138:20)                                                                                                                                                                   
  at Object.ReactDefaultBatchingStrategy.batchedUpdates (node_modules/react/lib/ReactDefaultBatchingStrategy.js:63:19) 

这些都是使用react-test-renderer 的测试,通常如下所示:

import 'react-native'
import React from 'react'
import Foo from '../Foo'

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer'

describe('<Foo />', () => {
  it('renders correctly', () => {
    const tree = renderer.create(
      <Foo loading={false} dispatch={() => {}} />
    ).toJSON()
    expect(tree).toMatchSnapshot()
  })
})

这个完全相同的测试文件适用于我的一个组件,但如果我尝试大多数其他组件,则会失败。所有这些测试在 Jest 14.1.0 下都能正常工作。

错误消息的描述性不是很好。你知道会发生什么吗?

【问题讨论】:

    标签: reactjs react-native jestjs


    【解决方案1】:

    问题原来是我们使用了babel-plugin-rewire。解决方案是确保我们只为 mocha 而不是为了开玩笑而启用 rewire。

    在我们的 .babelrc 中,我们将 test 更改为 mochatest

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

    在我们的测试运行程序中,我们确保仅在调用 mocha 而不是 jest 之前将 BABEL_ENV 设置为 mochatest。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-12
      • 2020-03-11
      • 1970-01-01
      • 2021-07-15
      • 2020-11-11
      • 2016-10-03
      • 2020-10-05
      • 2017-12-24
      相关资源
      最近更新 更多