Skip to content

[Breaking change] Properly escape snapshots #2482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2017
Merged

Conversation

vjeux
Copy link
Contributor

@vjeux vjeux commented Dec 30, 2016

There were many issues with snapshot test escaping:

  • When escaping backtick, it didn't escape backslash
  • Double quote escaping was overzealous and also escaped single quotes
  • JSX text didn't use HTML escaping
  • Regex escaping didn't escape all the control characters that should be

Because of the first one, you couldn't do eval(snapshot) and get the correct value, in order to make it kind of work, an unescape function was created that did ... something. Now that escaping is done properly we don't need it anymore.

This is a breaking change as all the double quotes now need to be escaped which triggers a ton of failures.

Fixes #1789

@jlongster
Copy link

jlongster commented Dec 30, 2016

Since this is going to affect existing snapshots, would it be easier to try and make it so it doesn't escape the single/double quotes? It doesn't need to because they are inside a string template. Shouldn't it only have to escape the backtick?

I think all existing snapshots will be less affected, if the above is possible.

@vjeux
Copy link
Contributor Author

vjeux commented Dec 30, 2016

I actually tried making the quotes be backticks but then it looks really weird.

<div className=`my-class` />

or

{key: `some string`}

But, now that you mention it, I'm not really sure why everything is wrapped in a backtick string. If the value is a string, we could just write that string instead of writing a string that contains a string and pay the cost of escaping twice.

@jlongster
Copy link

But, now that you mention it, I'm not really sure why everything is wrapped in a backtick string. If the value is a string, we could just write that string instead of writing a string that contains a string and pay the cost of escaping twice.

Yeah, that could work. But if we keep it wrapped in a backtick, why do we need to escape at all? This is valid:

` "

' " " `

Single or double quotes within a template don't need any escaping... might be missing something though.

@vjeux
Copy link
Contributor Author

vjeux commented Dec 30, 2016

Right now, .toMatchSnapshot('this " is a quote ') generates

exports['key'] = `
"this \" is a quote"
`;

What I'm suggesting is to instead output

exports['key'] = 
`this " is a quote`;

@jlongster
Copy link

Oh, yeah that looks great and should have minimal impact on existing snapshots.

@cpojer
Copy link
Member

cpojer commented Dec 30, 2016 via email

vjeux added 2 commits January 27, 2017 14:05
There were many issues with snapshot test escaping:
- When escaping backtick, it didn't escape backslash
- Double quote escaping was overzealous and also escaped single quotes
- JSX text didn't use HTML escaping
- Regex escaping didn't escape all the control characters that should be

Because of the first one, you couldn't do eval(snapshot) and get the correct value, in order to make it kind of work, an unescape function was created that did ... something. Now that escaping is done properly we don't need it anymore.

This is a breaking change as all the double quotes now need to be escaped which triggers a ton of failures.
@cpojer cpojer merged commit e4718cb into jestjs:master Jan 27, 2017
skovhus pushed a commit to skovhus/jest that referenced this pull request Apr 29, 2017
* [Breaking change] Properly escape snapshots

There were many issues with snapshot test escaping:
- When escaping backtick, it didn't escape backslash
- Double quote escaping was overzealous and also escaped single quotes
- JSX text didn't use HTML escaping
- Regex escaping didn't escape all the control characters that should be

Because of the first one, you couldn't do eval(snapshot) and get the correct value, in order to make it kind of work, an unescape function was created that did ... something. Now that escaping is done properly we don't need it anymore.

This is a breaking change as all the double quotes now need to be escaped which triggers a ton of failures.

* Update snapshots
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* [Breaking change] Properly escape snapshots

There were many issues with snapshot test escaping:
- When escaping backtick, it didn't escape backslash
- Double quote escaping was overzealous and also escaped single quotes
- JSX text didn't use HTML escaping
- Regex escaping didn't escape all the control characters that should be

Because of the first one, you couldn't do eval(snapshot) and get the correct value, in order to make it kind of work, an unescape function was created that did ... something. Now that escaping is done properly we don't need it anymore.

This is a breaking change as all the double quotes now need to be escaped which triggers a ton of failures.

* Update snapshots
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Jest seems to not be able to make up its mind between ' and \'
4 participants