javascript - React-testing-library 渲染函数有什么问题?它对某些对象返回错误

标签 javascript reactjs react-redux jestjs 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 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

如果我渲染此组件,则不会出现错误:

{ '$$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暗示这就是它所期望的。

我做错了什么?

最佳答案

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

你试过吗render(component)

关于javascript - React-testing-library 渲染函数有什么问题?它对某些对象返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57962228/

相关文章:

javascript - 是的, 'shape' 函数有什么作用?

javascript - HTML <select> 元素是否应该调度 `beforeinput` 事件?

javascript - 是否可以在 reactjs 中使用自定义图标?

reactjs - 通过Redux的Reducer更新列表

javascript - 如何在动态加载的按钮中发送新消息?

javascript - 使用字符串的模板语法不是函数错误

javascript - flex 元素是否有可能与它们上方的元素紧密对齐?

css - React 无法导入 CSS 文件,并抛出错误

javascript - React-router isActive 方法不起作用

javascript - Redux reducer 不显示我的控制台日志并返回未定义