From 7c872eeb3c1ea71b058bb27d062c5a13aec43fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 10 Feb 2017 09:52:20 +0100 Subject: [PATCH] Unescape serialized snapshots for diffing (#2852) * Unescape serialized snapshots for diffing * Normalize \r same way as \r\n by @rubenmoya * Don't unescape regex for now --- packages/jest-snapshot/src/State.js | 5 +++-- packages/jest-snapshot/src/__tests__/utils-test.js | 13 ++++++++++++- packages/jest-snapshot/src/utils.js | 6 +++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/jest-snapshot/src/State.js b/packages/jest-snapshot/src/State.js index 519b6aac63c7..5424a410092e 100644 --- a/packages/jest-snapshot/src/State.js +++ b/packages/jest-snapshot/src/State.js @@ -19,6 +19,7 @@ const { keyToTestName, serialize, testNameToKey, + unescape, } = require('./utils'); const fileExists = require('jest-file-exists'); const fs = require('fs'); @@ -158,9 +159,9 @@ class SnapshotState { if (!pass) { this.unmatched++; return { - actual: receivedSerialized, + actual: unescape(receivedSerialized), count, - expected, + expected: unescape(expected), pass: false, }; } else { diff --git a/packages/jest-snapshot/src/__tests__/utils-test.js b/packages/jest-snapshot/src/__tests__/utils-test.js index 1abba6a7d4ef..5870d5a846dc 100644 --- a/packages/jest-snapshot/src/__tests__/utils-test.js +++ b/packages/jest-snapshot/src/__tests__/utils-test.js @@ -41,7 +41,7 @@ test('getSnapshotPath()', () => { ); }); -test('saveSnapshotFile()', () => { +test('saveSnapshotFile() works with \r\n', () => { const filename = path.join(__dirname, 'remove-newlines.snap'); const data = { myKey: '
\r\n
', @@ -52,6 +52,17 @@ test('saveSnapshotFile()', () => { .toBeCalledWith(filename, 'exports[`myKey`] = `
\n
`;\n'); }); +test('saveSnapshotFile() works with \r', () => { + const filename = path.join(__dirname, 'remove-newlines.snap'); + const data = { + myKey: '
\r
', + }; + + saveSnapshotFile(data, filename); + expect(fs.writeFileSync) + .toBeCalledWith(filename, 'exports[`myKey`] = `
\n
`;\n'); +}); + test('escaping', () => { const filename = path.join(__dirname, 'escaping.snap'); const data = '"\'\\'; diff --git a/packages/jest-snapshot/src/utils.js b/packages/jest-snapshot/src/utils.js index 61c7d5c02b21..e6f22a45cca9 100644 --- a/packages/jest-snapshot/src/utils.js +++ b/packages/jest-snapshot/src/utils.js @@ -66,6 +66,9 @@ const serialize = (data: any): string => { })); }; +const unescape = (data: any): string => + data.replace(/\\(")/g, '$1'); // unescape double quotes + const printBacktickString = (str: string) => { return '`' + str.replace(/`|\\|\${/g, '\\$&') + '`'; }; @@ -77,7 +80,7 @@ const ensureDirectoryExists = (filePath: Path) => { }; const normalizeNewlines = - string => string.replace(/\r\n/g, '\n'); + string => string.replace(/\r\n|\r/g, '\n'); const saveSnapshotFile = ( snapshotData: {[key: string]: string}, @@ -102,4 +105,5 @@ module.exports = { saveSnapshotFile, serialize, testNameToKey, + unescape, };