开玩笑的快照测试还是单元测试? [英] Jest Snapshot testing vs unit testing?

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

问题描述

开玩笑地进行快照测试相当容易,但是我对此并不满意,因为感觉好像我实际上并没有进行任何测试.在单元测试中,我可以轻松地对组件进行测试,并期望它们的实际行为,还可以测试它们应呈现的内容.但是我能看到的有关快照测试的所有内容是,当您更改某些内容并显示出它们之间的差异时,它就会产生收益!我可以使用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.

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

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