jest快照测试:如何在jest测试结果中忽略部分快照文件
问题:忽略.snap文件测试结果的某些部分
Problem: ignore some part of the .snap file test results
这里的问题:我的测试中有一些组件具有随机值,我并不真的在乎测试它们.有什么办法可以忽略我的X.snap文件的一部分?因此,当我将来运行测试时,它不会给我测试失败的结果.
the question here: there are some components in my test that have a random values and i don't really care about testing them. is there any way to ignore part of my X.snap file? so when i run tests in the future it won't give me test fail results.
实际上,您需要模拟运动部件.
Actually, you need to mock the moving parts.
最好的文档中所述>:
您的测试应该是确定性的.也就是说,在未更改的组件上多次运行相同的测试应该每次都产生相同的结果.您有责任确保所生成的快照不包括特定于平台的数据或其他不确定的数据.
Your tests should be deterministic. That is, running the same tests multiple times on a component that has not changed should produce the same results every time. You're responsible for making sure your generated snapshots do not include platform specific or other non-deterministic data.
如果与时间有关,可以使用
If it's something related to time, you could use
Date.now = jest.fn(() => 1482363367071);