Something in Jest seems weird to me. Is that normal that this line:
expect(`"Hello"`).toMatchSnapshot();
Gives me the following snapshot:
exports[`Item renders and matches the snapshot 1`] = `"\\"Hello\\""`;
I would expect the snapshot to be just "Hello"
and not "\\"Hello\\""
. Is that an issue or is there something behind that I don't understand?
Snapshot tests are useful when 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.
Yes, all snapshot files should be committed alongside the modules they are covering and their tests. They should be considered part of a test, similar to the value of any other assertion in Jest.
You should then update the snapshot tests. Note: Alternatively, if you have Jest installed globally, you can run jest --updateSnapshot or jest -u . This will update the snapshots to match the updates you made, and your tests will pass.
snapshots are based on JSON.stringify, if you run your browser devtools:
JSON.stringify("hello"); // outputs: '"hello"'
as you can see, we have the extra single quote to wrap the double quote
jest uses the same approach, but since it uses double quote wrapping for results of snapshots, it needs to escape your value to be able to wrap it with double quotes, hence "\\"Hello\\"".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With