Skip to content

Commit

Permalink
Unescape serialized snapshots for diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Feb 9, 2017
1 parent 4fd40cc commit cea766b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/jest-snapshot/src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
keyToTestName,
serialize,
testNameToKey,
unescape,
} = require('./utils');
const fileExists = require('jest-file-exists');
const fs = require('fs');
Expand Down Expand Up @@ -158,9 +159,9 @@ class SnapshotState {
if (!pass) {
this.unmatched++;
return {
actual: receivedSerialized,
actual: unescape(receivedSerialized),
count,
expected,
expected: unescape(expected),
pass: false,
};
} else {
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-snapshot/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ const serialize = (data: any): string => {
}));
};

const unescape = (data: any): string => {
return data
.replace(/\\(")/g, '$1') // unescape double quotes
.replace(/\\([\\^$*+?.()|[\]{}])/g, '$1'); // then unescape RegExp
};

const printBacktickString = (str: string) => {
return '`' + str.replace(/`|\\|\${/g, '\\$&') + '`';
};
Expand Down Expand Up @@ -102,4 +108,5 @@ module.exports = {
saveSnapshotFile,
serialize,
testNameToKey,
unescape,
};

0 comments on commit cea766b

Please sign in to comment.