简体   繁体   中英

Why does Jest snapshots contain escaping characters because of double quotes?

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?

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\\\\"".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM