javascript 类型错误:无法在玩笑测试中读取未定义的属性“原型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52279913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:54:11  来源:igfitidea点击:

TypeError: Cannot read property 'prototype' of undefined in jest test

javascriptreactjsreact-nativejestjsenzyme

提问by Ackman

**************************** EDITED AND UPDATED************************

**************************** 编辑和更新******************* *****

I have a Additional Info component which uses react navigation:

我有一个使用反应导航的附加信息组件:

export class AdditionalInfo extends NavigationPureComponent {
  static navigationOptions = ({ navigation }) => ({
    headerLeft: <Button icon="close" onPress={() => navigation.goBack(null)} />,
  })

  buildNavigator = () => {
    const { extendedDescriptions } = this.nav.params
    const tabs = {}

    extendedDescriptions.forEach(({ caption, description }, index) => {
      tabs[`Tab${index}`] = {
        screen: () => (
          <ScrollView style={{ backgroundColor: color('white') }}>
            <Wrapper style={{ paddingTop: spacing() }}>
              <SafeAreaView>
                <Html html={description} />
              </SafeAreaView>
            </Wrapper>
          </ScrollView>
        ),
        navigationOptions: {
          title: caption,
        },
      }
    })

    return createMaterialTopTabNavigator(tabs, {
      backBehavior: 'none',
      lazy: true,
      tabBarOptions: {
        activeTintColor: color('b'),
        inactiveTintColor: color('b'),
        indicatorStyle: {
          backgroundColor: color('b'),
        },
        scrollEnabled: extendedDescriptions.length > 3,
        style: {
          backgroundColor: color('white'),
        },
      },
    })
  }

  render () {
    const AdditionalInfoNavigator = this.buildNavigator()

    return <AdditionalInfoNavigator />
  }

My additionalInfo.test.jsx file looks like:

我的 additionalInfo.test.jsx 文件看起来像:

describe('Additional Info', () => {
  test('Additional info component Exists', () => {
    const length = 4
    const extendedDescriptions = Array.from({ length }).map((value, index) => ({
      caption: `Tab ${index}`,
      description: `${lorem}`,
    }))
    const obj = shallow(<AdditionalInfo navigation={{ extendedDescriptions }} />)
  })
})

I am trying to write a test to check existence of this AdditionalInfo component and maybe a few more however, I am getting a weird error stating

我正在尝试编写一个测试来检查这个 AdditionalInfo 组件是否存在,也许还有一些,但是我收到一个奇怪的错误说明

TypeError: Cannot read property 'prototype' of undefined

      15 | 
      16 |     console.debug(extendedDescriptions)
    > 17 |     const obj = shallow(<AdditionalInfo navigation={{ extendedDescriptions }} />)

I feel am I not providing everything the test instance of AdditionalInfo needs? Or am I not using shallow correctly?

我觉得我没有提供 AdditionalInfo 测试实例所需的一切吗?还是我没有正确使用浅?

I am using the NavigationPureComponent which is defined as:

我使用的 NavigationPureComponent 定义为:

-----> export const NavigationPureComponent = navMixin(PureComponent)

-----> 导出 const NavigationPureComponent = navMixin(PureComponent)

const navMixin = (CurrentComponent) => {
  class Nav extends CurrentComponent {
    get nav () {
      const value = new Navigation(this)
      // reset `this.nav` to always be value, this way the this
      // get nav function only gets called the first time it's accessed
      Object.defineProperty(this, 'nav', {
        value,
        writable: false,
        configurable: false,
      })
      return value
    }
  }

  Nav.propTypes = {
    navigation: PropTypes.shape({}).isRequired,
  }

  return Nav
}

回答by zero_cool

How are you importing the Component into your test?

您如何将组件导入到您的测试中?

You haven't described it above so I imagine you don't see it as an issue.

你没有在上面描述它,所以我想你不认为它是一个问题。

I've seen this error before. When exporting a component as a class, you have to import the component into your test as an object.

我以前见过这个错误。将组件作为类导出时,您必须将组件作为对象导入到测试中。

Try this:

试试这个:

export class AdditionalInfo extends NavigationPureComponent {}

When you're importing into your test:

当您导入测试时:

import { AdditionalInfo } from '../pathToYourComponent'

回答by Yogendra Porwal

answering just if someone find it as me.

只要有人像我一样找到它就回答。

//if exporting as 
export class AdditionalInfo extends NavigationPureComponent {}
//then import
import { AdditionalInfo } from '../pathToYourComponent'
//if exporting as 
class AdditionalInfo extends NavigationPureComponent {}
export default AdditionalInfo
//then import
import AdditionalInfo from '../pathToYourComponent'