Skip to content

Commit cea766b

Browse files
committed
Unescape serialized snapshots for diffing
1 parent 4fd40cc commit cea766b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/jest-snapshot/src/State.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
keyToTestName,
2020
serialize,
2121
testNameToKey,
22+
unescape,
2223
} = require('./utils');
2324
const fileExists = require('jest-file-exists');
2425
const fs = require('fs');
@@ -158,9 +159,9 @@ class SnapshotState {
158159
if (!pass) {
159160
this.unmatched++;
160161
return {
161-
actual: receivedSerialized,
162+
actual: unescape(receivedSerialized),
162163
count,
163-
expected,
164+
expected: unescape(expected),
164165
pass: false,
165166
};
166167
} else {

packages/jest-snapshot/src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ const serialize = (data: any): string => {
6666
}));
6767
};
6868

69+
const unescape = (data: any): string => {
70+
return data
71+
.replace(/\\(")/g, '$1') // unescape double quotes
72+
.replace(/\\([\\^$*+?.()|[\]{}])/g, '$1'); // then unescape RegExp
73+
};
74+
6975
const printBacktickString = (str: string) => {
7076
return '`' + str.replace(/`|\\|\${/g, '\\$&') + '`';
7177
};
@@ -102,4 +108,5 @@ module.exports = {
102108
saveSnapshotFile,
103109
serialize,
104110
testNameToKey,
111+
unescape,
105112
};

0 commit comments

Comments
 (0)