How to get CSS style in snapshot with Jest

只谈情不闲聊 提交于 2020-06-27 10:35:34

问题


I have the following question, I don't know if its possible to get the result using Jest + snapshot feature.

I have a React components and I'm using Jest to test it.

I want the snapshot to include css style not className.

Currently my snapshot is like:

<div id="main" className="myClass"></div>

I would like to have it like:

<div id="main" style={Object {"float": "right"}}></div>

My App its using webpack(sass-loader) for parsing scss, so over there there are no issues.

It is possible to get this kind of result?

Thanks


回答1:


it is not possible with Jest/Enzyme since style rules can be anywhere: set up explicitly or goes through inheritance from parent element.

Alternatives are using styled-components so you could make snapshots of styled block alongside HTML with jest-styled-components.

Or you can use some visual regression tool that will make and compare screenshots instead of text snapshots.

PS I'm wondering if this is valid thing for automatic testing if a lot of different tests are treated as failed just because we have tuned up theming. Is it worth efforts to analyze and update tests/snapshots/screenshots?



来源:https://stackoverflow.com/questions/47550435/how-to-get-css-style-in-snapshot-with-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!