React tree Snapshot Testing
Now that you have seen a real-world testing example you may think that writing so many tests for a single component is a time-consuming task and not worth it.
Checking for each variation of text, value, and class name is laborious and it requires much code to cover all instances. However, most of the time, whenever we test components, the most important thing for us is that the output is correct and that it does not change unexpectedly. There is a new feature introduced in Jest that helps with this problem and it's called Snapshot Testing.
Snapshots are pictures of the component with some props at a given point in time. Every time we run the tests, Jest creates new pictures and it compares them with the previous ones to check if something has changed.
The content of the snapshot is the output of the render
method of a package called react-test-renderer
, which has to be installed with the following command:
npm install --save-dev react-test-renderer
Once the test...