gpt4 book ai didi

reactjs - React(Jest 测试 tr 元素无法找到元素...tbody)

转载 作者:行者123 更新时间:2023-12-02 04:43:54 24 4
gpt4 key购买 nike

我正在尝试测试我的组件,即

<TableRow key={1} name={name} />呈现为

<tr>
<td>{this.props.name}</td>
</tr>

在测试文件中看起来像这样

const mockName = { }

const mockPopularSearchLitItem = TestUtils.renderIntoDocument(TableRow name={mockName} />)

但是当我尝试运行测试时出现以下错误

不变违规:findComponentRoot(..., .0):无法找到元素。这可能意味着 DOM 被意外地改变了(例如,被浏览器改变),通常是因为忘记了 <tbody>。使用表格时,嵌套标签如 <form> , <p> , 或 <a> ,或在父元素中使用非 SVG 元素。尝试使用 React ID 检查元素的子节点

最佳答案

根据这篇文章http://codeheaven.io/testing-react-components-with-enzyme/不可能孤立地进行测试。

这可能是一种更好的测试方式

Shallow rendering let us render our component without touching the DOM. It also let us test our component as a unit, since it renders our component only one-level deep. Errors in children components wouldn’t propagate to top level components, making our tests more isolated and reliable.

关于reactjs - React(Jest 测试 tr 元素无法找到元素...tbody),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35018723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com