用Tohavestyle进行测试'期望失败时通过
我正在学习测试和反应。我遇到了一些麻烦,理解为什么测试在不应该时通过:
从app.js
<button style={{backgroundColor: 'gray'}}>
Gray button
</button>
来看,来自app.test.js的
expect(colorButton).toHaveStyle( {backgroundColor: 'gray' }); // passes ok
expect(colorButton).toHaveStyle( {backgroundColor: 'fay' }); // passes but should not
expect(colorButton).toHaveStyle( {backgroundColor: 'dfhdhsdh' }); // passes but should not
expect(colorButton).toHaveStyle( {backgroundColor: 'red' }); // expected error
expect(colorButton).toHaveStyle( {backgroundColor: 'MidnightBlue' }); // expected error
更多有关该项目的信息:
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
请有人帮助我吗?
I'm learning about testing and react. I'm having a little trouble understanding why a test is passing when it should not:
From App.js
<button style={{backgroundColor: 'gray'}}>
Gray button
</button>
From App.test.js
expect(colorButton).toHaveStyle( {backgroundColor: 'gray' }); // passes ok
expect(colorButton).toHaveStyle( {backgroundColor: 'fay' }); // passes but should not
expect(colorButton).toHaveStyle( {backgroundColor: 'dfhdhsdh' }); // passes but should not
expect(colorButton).toHaveStyle( {backgroundColor: 'red' }); // expected error
expect(colorButton).toHaveStyle( {backgroundColor: 'MidnightBlue' }); // expected error
More info about the project:
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Could anyone help me, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,用
对象
测试tohavestyle
正在引起错误,我真的不知道为什么。您可能应该在测试library/jest-dom github 时,您可能应该打开一个问题。但是目前,如果您使用一个简单的字符串来测试背景应该有效,那么
完整的测试:
您可以检查
tohavestyle的其他选项
Looks like testing
toHaveStyle
with anObject
in this case is causing an error and i really don´t know why. You should probably open an issue in testing-library/jest-dom github.But for now, if you use a simple string to test the background should work, just like that:
and the complete test:
You can check other options of
toHaveStyle
here.