简体   繁体   English

Jest酶浅测试不会渲染React组件的所有元素

[英]Jest enzyme shallow test is not rendering all elements of the React component

I am writing a jest enzyme test as below: 我正在写一个开玩笑酶测试如下:

import React from 'react';
import ManageDrugTermPage from '../js/manageDrugTermPage.jsx';
import toJson from 'enzyme-to-json';
describe('manage drug term page test suite', () => {

 it('snapshot test', () => {
    const setRouteLeaveHook =jest.fn();
    let wrapper = shallow(
        <ManageDrugTermPage params={{id : 25, router: setRouteLeaveHook}}/>
    );
    expect(toJson(wrapper)).toMatchSnapshot();
})
})

I want to see the details of ManageDrugTermPage in the snapshot, but snapshot is only displaying: 我想在快照中看到ManageDrugTermPage的详细信息,但快照仅显示:

 exports[`manage drug term page test suites snapshot test 1`] = `
    <ManageDrugTermPage
      params={
        Object {
          "id": 25,
          "router": [Function],
        }
      }
    />
    `;

How can I render the ManageDrugTermPage in the snapshot? 如何在快照中呈现ManageDrugTermPage? I dont want to use renderer.create but want to do it through enzyme. 我不想使用renderer.create但是想通过酶来做。

The problem is that shallow only renders one level deep, so every child component that is used in your component will be rendered but not the child components of them. 问题是shallow只渲染一层深度,因此组件中使用的每个子组件都将被渲染,但不会渲染它们的子组件。 There are two ways to make enzyme render the content of the childs as well. 有两种方法可以使酶呈现孩子的内容。 First there is mount which will force all components to render its child until they reach a DOM element. 首先是mount ,它将强制所有组件呈现其子项,直到它们到达DOM元素。 The problem with this that it can lead to very large and hard to read snapshots. 这样的问题是它可能导致非常大且难以读取的快照。

The other solution would be to use dive to force on child component to render its content. 另一种解决方案是使用dive强制子组件呈现其内容。 This is especially useful if you work with higher order components like connect from redux. 如果您使用更高阶的组件(例如来自redux的connect ,这将特别有用。 Cause in this case your rendered component would be just the wrapped one and shallow would not render the content that you would expect. 在这种情况下,您的渲染组件将只是包装的组件, shallow将不会呈现您期望的内容。 With dive you can just force the wrapped component to render its child which is what you really want to see in the snapshot. 通过dive您可以强制包裹的组件渲染其子项,这是您真正想要在快照中看到的内容。

The only strange thing with your example is that even the first level childs are not rendered. 你的例子唯一奇怪的是即使是第一级孩子也不会被渲染。 So maybe you can post the component code as well. 所以也许你也可以发布组件代码。

In case if someone is still looking for the cause and fix. 如果有人仍在寻找原因并修复。

This is not a fix for child components not rendering. 这不是修复子组件无法渲染的问题。 For that issue, refer the answer already posted by @Andreas Köberle. 对于该问题,请参阅@AndreasKöberle已经发布的答案。

Disclaimer: Since the actual component code is not shared, I am not entirely certain if the cause of the issue is same as explained below. 免责声明: 由于实际组件代码未共享,我不完全确定问题的原因是否与下面解释的相同。

I had similar issue of shallow not rendering Component Under Test and instead returning <MyComponent ... /> and it turned out the cause was injectSheet() decorator. 我有类似的问题shallow不渲染组件测试,而是返回<MyComponent ... /> ,结果原因是injectSheet()装饰器。

import injectSheet from 'react-jss'
...
class MyComponent extends Component {
   ...
}
export default injectSheet(combinedStyle)(MyComponent)

Perhaps, shallow doesn't call render on the styled component. 也许, shallow不会在样式化组件上调用渲染。

As a workaround I export unstyled version along with the default styled version 作为一种解决方法,我导出无样式版本以及默认样式版本

export default injectSheet(Styles)(MyComponent)
export { MyComponent as UnstyledMyComponent }

That's because you're using the Enzyme shallow render. 那是因为你正在使用Enzyme shallow渲染。 You want to instead use Full DOM Rendering ( mount ). 您想要使用Full DOM Rendering( mount )。

https://github.com/airbnb/enzyme/blob/master/docs/api/mount.md https://github.com/airbnb/enzyme/blob/master/docs/api/mount.md

it('snapshot test', () => {
    const setRouteLeaveHook =jest.fn();
    let wrapper = mount(
        <ManageDrugTermPage params={{id : 25, router: setRouteLeaveHook}}/>
    );
    expect(toJson(wrapper)).toMatchSnapshot();
});

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

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