简体   繁体   English

如何用酶的安装方法测试反应成分

[英]How to test React component with mount method of enzyme

I'm trying to use the mount method of the enzyme library to render a custom component but it fails with the error 我正在尝试使用库的mount方法来呈现自定义组件,但由于错误而失败

Invariant Violation: Tester.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

together with some annoying stack traces. 以及一些令人讨厌的堆栈跟踪。

Here is the code snippet 这是代码片段

import React from 'react'
import {shallow, mount, render} from 'enzyme'


describe('Dashboard', () => {
    let testComp;
    beforeEach(()=>{
        testComp = mount(  <UserDashboard />  )
    });

    it('should render self without breaking.', () => {
        expect(testComp).not.toBe(null)
    })

})

But I noticed that the problem is not with the UserDashboard component because I still get the same error when I replace it with a simple div element. 但是我注意到问题不在于UserDashboard组件,因为当我用一个简单的div元素替换它时,仍然出现相同的错误。

testComp = mount(  <div />  )

The shallow and render methods work pretty well but the mount method is being really stubborn. shallowrender方法工作得很好,但是mount方法确实很固执。

Please I'm new to testing react components and any help on why this is happening will be greatly appreciated. 请我是新来测试React组件的人,任何对此原因的帮助将不胜感激。

This was actually a compatibility issue between my react version ( react v15.6.2 ) and the enzyme adapter I was using ( enzyme-adapter-react-16 ). 这实际上是我的react版本( react v15.6.2 )和我正在使用的酶适配器( ases -adapter-react-16 )之间的兼容性问题。 It was normally supposed to be enzyme-adapter-react-15 . 通常认为它是适应反应15

For anyone having this same problem, make sure to visit this link on how to configure and use enzyme with your version of react. 对于有同样问题的任何人,请确保访问此链接 ,了解如何在您的反应版本中配置和使用酶。 Thanks to @nubinub for the link 感谢@nubinub提供的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM