Jest Snapshot 测试与单元测试? [英] Jest Snapshot testing vs unit testing?

查看:29
本文介绍了Jest Snapshot 测试与单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩笑中实现快照测试相当容易,但我对此不太满意,因为感觉我实际上并没有测试任何东西.在单元测试中,我可以轻松地测试我的组件,并对它们的实际行为提出期望,并测试它们应该呈现的内容.但是我所看到的关于快照测试的全部内容是,当您更改某些内容并显示差异时,它会产生收益!我可以使用 git diff app.js 做到这一点!

It's fairly easy to implement snapshot testing in jest but it's something I am not really comfortable with because it feels like I am not actually testing anything. In unit testing I can easily take my components under test and put expectations on the their actual behavior and also test what they should be rendering. But all I can see about snapshot testing is that it yields when you change something and shows you the difference! I can do that using git diff app.js!

推荐答案

您可以将快照测试视为具有自动生成的组件假设的单元测试.

You can think about a snapshot test as a unit test with an auto generated assumption about your component.

优点是您无需编写大量代码即可轻松测试复杂结构,当发生变化时您会收到很好的警告,并且您可以轻松更新此测试.

The advantages are that you can easily test complex structures without writing much code, that you get good warnings when something changed and that you can easily update this test.

缺点是仅仅阅读测试并不总是清楚测试什么以及预期的行为是什么,可能会发生创建的快照非常复杂,以至于您忽略了错误的假设,然后最终成为预期的结果而且更新快照非常容易,错误的东西可能会潜入.

The disadvantages are that from just reading the test it is not always clear what is tested and what the expected behaviour is, that it could happen that the created snapshot is so complex that you overlook wrong assumptions that then end up as expected result and that it is so easy to update snapshots that wrong stuff can sneak in.

因此,当使用快照测试时,使它们更加细化非常重要,因此并不总是测试整个组件,而是进行一些较小的测试来测试它的一部分,并拥有良好的代码审查文化,以发现快照中的错误.

So when using snapshot test its really important to make them more granular, so not always testing the whole component, but have some smaller test that test parts of it and to have good code review culture, to spot bugs in the snapshot.

这篇关于Jest Snapshot 测试与单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆