【发布时间】:2019-12-19 20:50:52
【问题描述】:
我是 Jest 和 Enzyme 测试的新手,我想知道为什么 find 函数不适用于 id。
//来自react的html,就是id增量在哪里的代码
<div className="App-body">
<div className="changePage">
<button className="Selections" onClick={this.decrementPage}>Previous</button>
<p className="changePageNumber">{this.state.page}</p>
<button className="Selections" id="increment" onClick={this.incrementPage}>Next</button>
</div>
</div>
//测试
it('next page', () => {
const wrapper = shallow(<Home />)
const incrementPage = wrapper.find('#increment')
incrementPage.simulate('click')
const countState = wrapper.state().page
expect(countState).toEqual(2)
})
Method “simulate” is meant to be run on 1 node. 0 found instead.
25 | //const text = wrapper.find('p').text()
26 | const incrementPage = wrapper.find('#increment')
> 27| incrementPage.simulate('click')
| ^
【问题讨论】:
-
当我遇到 find 与元素不匹配的问题时,我会使用诸如 console.log(wrapper.debug()) 之类的东西来准确查看 Enzyme 安装了什么以及它正在做什么。它可能会给你一些线索。如果不是,也许将其输出添加到您的问题中。
-
除了console.log之外,webstorm和visual studio代码让你调试npm测试设置断点和检查变量内容,在调试控制台中使用不同的查找组合等。设置适当的调试环境可能会有所帮助。