Jest Snapshot testing vs unit testing?

后端 未结 3 647
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 15:59

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 te

相关标签:
3条回答
  • 2021-02-12 16:29

    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.

    0 讨论(0)
  • 2021-02-12 16:30

    Thanks to everyone on this thread. I'm fairly new to React, but just started looking at unit testing it, and was wondering the same thing. Like, if I have an address component where the only real logic in it would be whether to render the address inline, or postal style. I decided on Snapshot testing here. But when it comes to validating interactivity and state change, then I guess that's where enzyme comes in, along with more standard, non-ui testing.

    0 讨论(0)
  • 2021-02-12 16:42

    You should write both snapshot and unit tests. Snapshot tests will check for changes when rendering, like if className, inline styles, some conditional rendering or loops are not working as they worked before. You should still write unit tests for any functionality that can't be checked with simple rendering (if onClick is called when clicked and so on).

    0 讨论(0)
提交回复
热议问题