reactjs - Cover Material-UI MakeStyles with Jest
问题描述
Every time I test my Material UI files, Jest complains because I mock makeStyles
and says that I didn't cover the entire file.
So, it shows me on the coverage file:
My test file has this:
jest.mock('@material-ui/styles', () => ({
...jest.requireActual('@material-ui/styles'),
makeStyles: () => () => ({}),
}))
How can I mock makeStyles
in a way that Jest stops complaining about that and making my coverage reports "false negatives"?
解决方案
(What exactly do you mean with "Jest complains"? If I understand you right, it's all about the coverage report, right? I guess that the tests itself are working.)
Your production code uses callbacks like theme => ({ cardContent, divider, spacer })
as argument to makeStyles()
, but your mockup does ignore those arguments and that way hides the callbacks from every test - i.e. there will be no test which actually does something with the callbacks and their results.
The coverage report is right - you're missing parts of the tested files. The following might increase the rating of your code coverage, but without any more logic you could end up with some "false positives", I guess...
jest.mock('@material-ui/styles', () => ({
...jest.requireActual('@material-ui/styles'),
makeStyles: cb => () => cb({ spacing: () => 0 }),
}))
推荐阅读
- npm - NPM 注册中心需要多长时间才能在全球传播?
- swift - 在 Swift 中随机播放字符串中的字符
- arrays - 如何在 Swift 中删除与 myindexes 数组中的索引匹配的所有项目
- azure - 如何在 Azure 中创建 RSS 源?
- express - 如何检查对象中的键是否具有特定值?
- python - 我正在使用 Redis 替换 ROS,这很奇怪吗?
- json - Angular:带有空 200/201 响应的 HttpClient 错误(始终调用 JSON.parse(""))
- javascript - 在 Node.js (React) 中更改为不同的页面/组件时如何运行不同的 SQL 函数?
- awk - awk:根据两列的连接值添加新列
- r - 如何使用ggmap在谷歌地图上绘制一个小标题?