首页 > 解决方案 > react-testing-library 渲染功能有什么问题?它在某些对象上返回错误

问题描述

使用:“@testing-library/react”:“^9.1.4”,

如果我打电话:

渲染({组件})

组件在哪里:

{ '$$typeof': Symbol(react.element),
        type:
         { mapStateToProps: [Function],
           mapDispatchToProps: [Function: mapDispatchToProps],
           reactComponent: { [Function: WithHandlers] displayName: 'withHandlers(lifecycle(Component))' },
           mockDispatch:
            { [Function: mockConstructor]
              _isMockFunction: true,
              getMockImplementation: [Function],
              mock: [Getter/Setter],
              mockClear: [Function],
              mockReset: [Function],
              mockReturnValueOnce: [Function],
              mockReturnValue: [Function],
              mockImplementationOnce: [Function],
              mockImplementation: [Function],
              mockReturnThis: [Function],
              mockRestore: [Function] } },
        key: null,
        ref: null,
        props: {},
        _owner: null,
        _store: {} }

我收到此错误:

console.error node_modules/react/cjs/react.development.js:172 警告:React.createElement:类型无效——需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:目的。

如果我渲染这个组件没有错误:

{ '$$typeof': Symbol(react.element),
        type: [Function],
        key: null,
        ref: null,
        props: {},
        _owner: null,
        _store: {} }

它们似乎都是 react.elements 和https://testing-library.com/docs/react-testing-library/api暗示这是它所期望的。

我做错了什么?

标签: javascriptreactjsreact-reduxjestjsreact-testing-library

解决方案


您正在调用render({component}),根据文档,输入应该是React.ReactElement<any>

你试过render(component)吗?


推荐阅读