如何解决使用 jest 和 @testing-library/dom 测试 vanilla 项目时,TestingLibraryElementError: Found multiple elements发生
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8" />
<title>title</title>
</head>
<body>
<div id="App"></div>
</body>
</html>
App.spec.ts
const html = fs
.readFileSync(path.join(__dirname,'..','index.html'))
.toString()
beforeEach(() => {
document.documentElement.innerHTML = html
})
describe('App test',() => {
it('App test',() => {
const appEl = document.querySelector('#App') as HTMLElement
new App({ el: appEl,floor: 5,elevatorNum: 3 })
expect(appEl).toMatchSnapshot()
})
it('',() => {
const requestFunc = jest.spyOn(Controller.getInstance(),'request')
const appEl = document.querySelector('#App') as HTMLElement
new App({ el: appEl,elevatorNum: 3 })
const secondFloor = getByText(
document.body.querySelector('#App') as HTMLElement,'5층'
)
fireEvent.click(secondFloor)
expect(requestFunc).toBeCalled()
expect(requestFunc).toHaveBeenCalledTimes(1)
})
问题是当 getByText
调用
TestingLibraryElementError: 找到多个带有文本的元素:5층
如何刷新文档??
我在下面尝试过。但它没有用
afterEach(() => {
document.documentElement.innerHTML = ''
})
//and I also tried cleanup from @marko/testing-library
afterEach(cleanup)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。