Skip to content

Commit e4718cb

Browse files
vjeuxcpojer
authored andcommitted
[Breaking change] Properly escape snapshots (#2482)
* [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
1 parent 8ec19cd commit e4718cb

22 files changed

Lines changed: 392 additions & 366 deletions

File tree

integration_tests/__tests__/__snapshots__/failures-test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ Object {
33
"rest": " FAIL __tests__/throw-number-test.js
44
Test suite failed to run
55
6-
Expected an Error, but \"1\" was thrown
6+
Expected an Error, but \\"1\\" was thrown
77
88
",
99
"summary": "Test Suites: 1 failed, 1 total
1010
Tests: 0 total
1111
Snapshots: 0 total
1212
Time: <<REPLACED>>
13-
Ran all test suites matching \"throw-number-test.js\".
13+
Ran all test suites matching \\"throw-number-test.js\\".
1414
",
1515
}
1616
`;
@@ -28,7 +28,7 @@ Object {
2828
Tests: 0 total
2929
Snapshots: 0 total
3030
Time: <<REPLACED>>
31-
Ran all test suites matching \"throw-string-test.js\".
31+
Ran all test suites matching \\"throw-string-test.js\\".
3232
",
3333
}
3434
`;
@@ -45,7 +45,7 @@ Object {
4545
Tests: 0 total
4646
Snapshots: 0 total
4747
Time: <<REPLACED>>
48-
Ran all test suites matching \"throw-object-test.js\".
48+
Ran all test suites matching \\"throw-object-test.js\\".
4949
",
5050
}
5151
`;
@@ -97,7 +97,7 @@ Object {
9797
Tests: 3 failed, 3 total
9898
Snapshots: 0 total
9999
Time: <<REPLACED>>
100-
Ran all test suites matching \"assertion-count-test.js\".
100+
Ran all test suites matching \\"assertion-count-test.js\\".
101101
",
102102
}
103103
`;

integration_tests/__tests__/__snapshots__/snapshot-serializers-test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ exports[`Snapshot serializers renders snapshot 1`] = `
22
Object {
33
"snapshot serializers works with default serializers 1": "
44
<div
5-
id="foo" />
5+
id=\\"foo\\" />
66
",
77
"snapshot serializers works with first plugin 1": "foo: 1",
88
"snapshot serializers works with nested serializable objects 1": "foo: bar: 2",
99
"snapshot serializers works with prepended plugins and default serializers 1": "
1010
<div
1111
aProp={
1212
Object {
13-
"a": 6,
13+
\\"a\\": 6,
1414
}
1515
}
1616
bProp={foo: 8} />

integration_tests/__tests__/__snapshots__/snapshot-test.js.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ exports[`Snapshot works with escaped characters 1`] = `
6666
Tests: 1 passed, 1 total
6767
Snapshots: 1 added, 1 total
6868
Time: <<REPLACED>>
69-
Ran all test suites matching \"snapshot-test.js\".
69+
Ran all test suites matching \\"snapshot-test.js\\".
7070
"
7171
`;
7272
@@ -75,7 +75,7 @@ exports[`Snapshot works with escaped characters 2`] = `
7575
Tests: 2 passed, 2 total
7676
Snapshots: 1 added, 1 passed, 2 total
7777
Time: <<REPLACED>>
78-
Ran all test suites matching \"snapshot-test.js\".
78+
Ran all test suites matching \\"snapshot-test.js\\".
7979
"
8080
`;
8181
@@ -84,7 +84,7 @@ exports[`Snapshot works with escaped characters 3`] = `
8484
Tests: 2 passed, 2 total
8585
Snapshots: 2 passed, 2 total
8686
Time: <<REPLACED>>
87-
Ran all test suites matching \"snapshot-test.js\".
87+
Ran all test suites matching \\"snapshot-test.js\\".
8888
"
8989
`;
9090
@@ -93,7 +93,7 @@ exports[`Snapshot works with escaped regex 1`] = `
9393
Tests: 2 passed, 2 total
9494
Snapshots: 2 added, 2 total
9595
Time: <<REPLACED>>
96-
Ran all test suites matching \"snapshot-escape-regex.js\".
96+
Ran all test suites matching \\"snapshot-escape-regex.js\\".
9797
"
9898
`;
9999
@@ -102,7 +102,7 @@ exports[`Snapshot works with escaped regex 2`] = `
102102
Tests: 2 passed, 2 total
103103
Snapshots: 2 passed, 2 total
104104
Time: <<REPLACED>>
105-
Ran all test suites matching \"snapshot-escape-regex.js\".
105+
Ran all test suites matching \\"snapshot-escape-regex.js\\".
106106
"
107107
`;
108108
@@ -111,7 +111,7 @@ exports[`Snapshot works with template literal subsitutions 1`] = `
111111
Tests: 1 passed, 1 total
112112
Snapshots: 1 added, 1 total
113113
Time: <<REPLACED>>
114-
Ran all test suites matching \"snapshot-escape-substitution-test.js\".
114+
Ran all test suites matching \\"snapshot-escape-substitution-test.js\\".
115115
"
116116
`;
117117
@@ -120,6 +120,6 @@ exports[`Snapshot works with template literal subsitutions 2`] = `
120120
Tests: 1 passed, 1 total
121121
Snapshots: 1 passed, 1 total
122122
Time: <<REPLACED>>
123-
Ran all test suites matching \"snapshot-escape-substitution-test.js\".
123+
Ran all test suites matching \\"snapshot-escape-substitution-test.js\\".
124124
"
125125
`;

integration_tests/__tests__/__snapshots__/stack_trace-test.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ exports[`Stack Trace does not print a stack trace for errors when --noStackTrace
33
Tests: 3 failed, 3 total
44
Snapshots: 0 total
55
Time: <<REPLACED>>
6-
Ran all test suites matching \"test-error-test.js\".
6+
Ran all test suites matching \\"test-error-test.js\\".
77
"
88
`;
99
@@ -12,7 +12,7 @@ exports[`Stack Trace does not print a stack trace for matching errors when --noS
1212
Tests: 1 failed, 1 total
1313
Snapshots: 0 total
1414
Time: <<REPLACED>>
15-
Ran all test suites matching \"stack-trace-test.js\".
15+
Ran all test suites matching \\"stack-trace-test.js\\".
1616
"
1717
`;
1818
@@ -21,7 +21,7 @@ exports[`Stack Trace does not print a stack trace for runtime errors when --noSt
2121
Tests: 0 total
2222
Snapshots: 0 total
2323
Time: <<REPLACED>>
24-
Ran all test suites matching \"runtime-error-test.js\".
24+
Ran all test suites matching \\"runtime-error-test.js\\".
2525
"
2626
`;
2727
@@ -30,7 +30,7 @@ exports[`Stack Trace prints a stack trace for errors 1`] = `
3030
Tests: 3 failed, 3 total
3131
Snapshots: 0 total
3232
Time: <<REPLACED>>
33-
Ran all test suites matching \"test-error-test.js\".
33+
Ran all test suites matching \\"test-error-test.js\\".
3434
"
3535
`;
3636
@@ -39,7 +39,7 @@ exports[`Stack Trace prints a stack trace for matching errors 1`] = `
3939
Tests: 1 failed, 1 total
4040
Snapshots: 0 total
4141
Time: <<REPLACED>>
42-
Ran all test suites matching \"stack-trace-test.js\".
42+
Ran all test suites matching \\"stack-trace-test.js\\".
4343
"
4444
`;
4545
@@ -48,6 +48,6 @@ exports[`Stack Trace prints a stack trace for runtime errors 1`] = `
4848
Tests: 0 total
4949
Snapshots: 0 total
5050
Time: <<REPLACED>>
51-
Ran all test suites matching \"runtime-error-test.js\".
51+
Ran all test suites matching \\"runtime-error-test.js\\".
5252
"
5353
`;

integration_tests/__tests__/__snapshots__/testNamePattern-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ exports[`test testNamePattern 1`] = `
33
Tests: 2 skipped, 2 passed, 4 total
44
Snapshots: 0 total
55
Time: <<REPLACED>>
6-
Ran all test suites with tests matching \"should match\".
6+
Ran all test suites with tests matching \\"should match\\".
77
"
88
`;

packages/jest-config/src/__tests__/__snapshots__/normalize-test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`preprocessorIgnorePatterns\` are used 1`] = `
22
"● Deprecation Warning:
33
4-
Option [1m\"preprocessorIgnorePatterns\"[22m was replaced by [1m\"transformIgnorePatterns\"[22m, which support multiple preprocessors.
4+
Option [1m\\"preprocessorIgnorePatterns\\"[22m was replaced by [1m\\"transformIgnorePatterns\\"[22m, which support multiple preprocessors.
55
66
Jest now treats your current configuration as:
77
{
8-
[1m\"transformIgnorePatterns\"[22m: [1m[\"bar/baz\", \"qux/quux\"][22m
8+
[1m\\"transformIgnorePatterns\\"[22m: [1m[\\"bar/baz\\", \\"qux/quux\\"][22m
99
}
1010
1111
Please update your configuration.

packages/jest-diff/src/__tests__/__snapshots__/diff-test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Printing internal object structure without calling \`toJSON\` instead.
2323
+ Received
2424
2525
  Object {
26-
[32m-[39m [32m \"line\": 1,[39m
27-
[31m+[39m [31m \"line\": 2,[39m
28-
[2m [22m [2m \"toJSON\": [Function toJSON],[22m
26+
[32m-[39m [32m \\"line\\": 1,[39m
27+
[31m+[39m [31m \\"line\\": 2,[39m
28+
[2m [22m [2m \\"toJSON\\": [Function toJSON],[22m
2929
  }"
3030
`;
3131
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
exports[`.stringify() reduces maxDepth if stringifying very large objects 1`] = `"{\"a\": 1, \"b\": [Object]}"`;
1+
exports[`.stringify() reduces maxDepth if stringifying very large objects 1`] = `"{\\"a\\": 1, \\"b\\": [Object]}"`;
22

3-
exports[`.stringify() reduces maxDepth if stringifying very large objects 2`] = `"{\"a\": 1, \"b\": {\"0\": \"test\", \"1\": \"test\", \"2\": \"test\", \"3\": \"test\", \"4\": \"test\", \"5\": \"test\", \"6\": \"test\", \"7\": \"test\", \"8\": \"test\", \"9\": \"test\"}}"`;
3+
exports[`.stringify() reduces maxDepth if stringifying very large objects 2`] = `"{\\"a\\": 1, \\"b\\": {\\"0\\": \\"test\\", \\"1\\": \\"test\\", \\"2\\": \\"test\\", \\"3\\": \\"test\\", \\"4\\": \\"test\\", \\"5\\": \\"test\\", \\"6\\": \\"test\\", \\"7\\": \\"test\\", \\"8\\": \\"test\\", \\"9\\": \\"test\\"}}"`;
44

55
exports[`.stringify() toJSON errors when comparing two objects 1`] = `
66
"expect(received).toEqual(expected)
77
88
Expected value to equal:
9-
[32m{\"b\": 1, \"toJSON\": [Function toJSON]}[39m
9+
[32m{\\"b\\": 1, \\"toJSON\\": [Function toJSON]}[39m
1010
Received:
11-
[31m{\"a\": 1, \"toJSON\": [Function toJSON]}[39m
11+
[31m{\\"a\\": 1, \\"toJSON\\": [Function toJSON]}[39m
1212
1313
Difference:
1414
1515
- Expected
1616
+ Received
1717
1818
 Object {
19-
[32m- \"b\": 1,[39m
20-
[31m+ \"a\": 1,[39m
21-
[2m \"toJSON\": [Function toJSON],[22m
19+
[32m- \\"b\\": 1,[39m
20+
[31m+ \\"a\\": 1,[39m
21+
[2m \\"toJSON\\": [Function toJSON],[22m
2222
 }"
2323
`;

0 commit comments

Comments
 (0)