Understanding snapshot testing
Snapshot testing is a useful way to test that there are no unwanted changes in your user interface (UI). Jest generates snapshot files when snapshot tests are executed. The next time tests are executed, the new snapshot is compared to the previous one. If there are changes between the content of the files, the test case fails, and an error message is shown in the terminal.
To start snapshot testing, perform the following steps:
- Install the
react-test-rendererpackage. The--save-devparameter means that this dependency is saved to thepackage.jsonfile'sdevDependenciespart, and it is only used for development purposes. If you type thenpm install --productioncommand in the installation phase, dependencies in thedevDependenciespart are not installed. So, all dependencies that are only required in the development phase should be installed using the--save-devparameter, like this:npm install react-test-renderer --save-dev
- After...