Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest Snapshot testing vs unit testing?

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 components I want to test and write expectations on 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... similar to what git diff does.

So when should I use snapshot testing over unit testing?

like image 292
bigfanjs Avatar asked May 03 '17 23:05

bigfanjs


People also ask

When should I use snapshot jest?

Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly. A typical snapshot test case renders a UI component, takes a snapshot, then compares it to a reference snapshot file stored alongside the test.

What is a jest snapshot?

Jest is a popular framework for testing JavaScript, and Jest snapshot is a method for creating an HTML entity map for regression tests. Ideally, a developer can create a jest snapshot for each output\component and create a test that easily compares it in each run. If any change is detected, the test fails.

What is snapshot testing used for in React?

- [Instructor] Snapshot tests are useful for making sure that the UI will not change unexpectedly. An additional Facebook package needs to be added to the project via npm to help us do that. It's called react-test-renderer, and it allows us to render React components as pure JavaScript objects.

How would you do snapshot testing with jest?

When writing snapshot tests for a React component, you first need to have code in a working state. Then, generate a snapshot of its expected output given certain data. The snapshot tests are committed alongside the component. Jest, a testing framework, will compare the snapshot to the rendered output for the test.


2 Answers

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.

like image 72
Andreas Köberle Avatar answered Sep 30 '22 01:09

Andreas Köberle


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).

like image 40
Nesha Zoric Avatar answered Sep 30 '22 01:09

Nesha Zoric