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

Created on 24 Sep 2016  路  20Comments  路  Source: facebook/jest

We're constantly seeing jest switch its snapshot output between escaping apostrophes and not. I can't tell if it's related to certain peoples computers or what.

the-link 2016-09-23 16-19-04

Bug Confirmed

Most helpful comment

This will be fixed in Jest 19 thanks to @vjeux. It's a breaking change for snapshot and we'll try to message it appropriately.

All 20 comments

Hmm.. this is very odd. Jest should always escape quotes. Would you mind letting me know which systems all your engineers are using?

@cpojer we're all on macs. Why would it escape single quotes in a backquoted string? That seems unnecessary.

Quotes always spell trouble. @kyldvs noticed the issues first. Does this happen in files where there are test failures or snapshot failures? I believe it is possible that this might happen when a test is thrown, yet we try to write the snapshot data back into the test file anyway.

Is everyone on latest version of jest? (check global jest and local one) If someone is ~month old it will mess up quotes

@kyldvs what is "messed up"? escaping single quotes in backtick quoted strings? I'll ask people to check, there may be someone running jest watch w/ an old globally installed jest

The escapes are necessary to handle some edge cases. By mess up I just mean it will alternate adding/removing escapes. That's what an old version did. A single person could be on the old version and it would never be in a consistent state due to fun string escaping/parsing/serialization edge cases.

If everyone is on a recent version and it's alternating escapes still that would be a bug.

@kyldvs I'm on 15.1.1 and it just stripped escapes on my last run.

Also, I think the run that caused it did have errors.

Can confirm I'm also experiencing this issue on 15.1.1

I haven't seen this since we rewrote snapshots in 16. Please try with Jest 17 and report back if this is still an issue.

I think I finally found the issue. It's a combination between renaming and disabling tests.

I did it! Here is the repro:

  • Have three snapshots with quotes in a file, store them all.
  • Delete one of the tests.
  • Use test.only for the second one.
  • Run with -u.
  • This will remove the obsolete snapshot, write the one in test.only and use the existing value of the third snapshot because the test wasn't run; that third one will not use the proper escaping mechanism.

wow, quite the repro. Nice work.

Unfortunately since the way we encode/decode strings currently, I cannot think of a good way to fix this :( cc @dmitriiabramov had an idea.

The problem is that we rely on pretty-format to escape quotes in strings, like "foo\'bar" but these go away when we require the file as JS. When we write a snapshot file, we use the data from pretty-format of the real value (passed to expect) so everything is escaped properly. When we write a snapshot file with data we read from a snapshot because we don't run the test that generates it, we don't have the data from pretty-format.

This is also affecting @pieterv and myself if that's of any use :)

@vjeux it's not, but thanks! Can you fix it? :)

When we write a snapshot file with data we read from a snapshot because we don't run the test that generates it

When does this happen? I'm curious. That means if I remove all of my snapshots and re-generate them, it should never happen again right? How did unescaped snapshots get created in the first place when there wasn't a snapshot to read from?

Thanks for explaining!

@jlongster when a snapshot file is written (either through -u or a new toMatchSnapshot call), we take all of the values passed to expect and call pretty-format on them and write a completely new snapshot file. When a test is disabled, we don't have the generated value so we use pretty-printed string from the snapshot file, which is what is causing the problem here. @vjeux is actually investigating a fix right now :) Seems like I might a bad call with regards to escaping.

This will be fixed in Jest 19 thanks to @vjeux. It's a breaking change for snapshot and we'll try to message it appropriately.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gustavjf picture gustavjf  路  3Comments

withinboredom picture withinboredom  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

ianp picture ianp  路  3Comments

hramos picture hramos  路  3Comments