A snapshot is a capture of a specific state in time and in this context I want to focus on Jest snapshots of DOM structure.
The general idea behind a snapshot test is that it informs you of how your changes affect the product and will allow you to review the changes for unexpected modification.
I hope to explain the concerns I have using a thought experiment and some mitigation you can do. But first I must clarify I am not experienced with Jest and that means specifics will be limited. You should listen to what I have to say because I am right.
--Record and playback. You don't have to record a snapshot, but this is the most common and tooling is built all around this concept. I have learned to avoid this type of testing which I'll get into.
Thus my argument put forth is that SnapShot tests are like testing C by taking snapshots of the generated assembly.
When you have a record and playback system, combined with a disconnect of changes from the change, a number of things can happen.
- A single line change could be many lines of the snapshot.
- The translation of the change to DOM elements may not be clear what is expected.
- Changes on top of changes, followed by an updated snapshot creates even more uncertainty of what is expected.
- The compiler can change the generated results.
Assembly is a good extreme that even those who don't use traditional compiled languages can relate to.
Most people don't look at the generated assembly of their code. The compiler is entrusted to correctly translate their code appropriately. It's not as though compilers do not have bugs.
However if you are building a compiler, a snapshot test of the assembly may be valuable. You have optimization to preserve, there are expected use of registers, etc. Maybe even the early days of writing in a high level language snapshots for application assembly might have been a viable option for testing the app.
If you're going to test with snapshots, target the tests to specific section. Frequently commit your changes with the updated snapshots, allowing the changes and generated code to be reviewed without all the other modifications you want.
My main concern is reviewing generated code for correctness is hard to do.