From a3058a6fff61b267e15d60b370f00eee2cf747ee Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 20 Aug 2019 17:48:10 -0400 Subject: [PATCH 1/5] jest-snapshot: Remove only the added newlines in multiline snapshots --- .../jest-snapshot/src/__tests__/utils.test.ts | 74 +++++++++++++++++++ packages/jest-snapshot/src/index.ts | 4 +- packages/jest-snapshot/src/utils.ts | 10 ++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/utils.test.ts b/packages/jest-snapshot/src/__tests__/utils.test.ts index 85c757fe10d7..50a74b185a8f 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.ts +++ b/packages/jest-snapshot/src/__tests__/utils.test.ts @@ -19,9 +19,11 @@ import { SNAPSHOT_GUIDE_LINK, SNAPSHOT_VERSION, SNAPSHOT_VERSION_WARNING, + addExtraLineBreaks, deepMerge, getSnapshotData, keyToTestName, + removeExtraLineBreaks, saveSnapshotFile, serialize, testNameToKey, @@ -192,6 +194,78 @@ test('serialize handles \\r\\n', () => { expect(serializedData).toBe('\n"
\n
"\n'); }); +describe('ExtraLineBreaks', () => { + test('0 empty string', () => { + const expected = ''; + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe(expected); + expect(removed).toBe(expected); + }); + + test('1 line has double quote marks at edges', () => { + const expected = '" one line "'; + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe(expected); + expect(removed).toBe(expected); + }); + + test('1 line has spaces at edges', () => { + const expected = ' one line '; + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe(expected); + expect(removed).toBe(expected); + }); + + test('2 lines both are blank', () => { + const expected = '\n'; + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe('\n' + expected + '\n'); + expect(removed).toBe(expected); + }); + + test('2 lines have double quote marks at edges', () => { + const expected = '"\n"'; + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe('\n' + expected + '\n'); + expect(removed).toBe(expected); + }); + + test('2 lines first is blank', () => { + const expected = ['', 'second line '].join('\n'); + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe('\n' + expected + '\n'); + expect(removed).toBe(expected); + }); + + test('2 lines last is blank', () => { + const expected = [' first line', ''].join('\n'); + + const added = addExtraLineBreaks(expected); + const removed = removeExtraLineBreaks(added); + + expect(added).toBe('\n' + expected + '\n'); + expect(removed).toBe(expected); + }); +}); + describe('DeepMerge with property matchers', () => { const matcher = expect.any(String); diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 401843374b9f..3526cbb687c6 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -349,8 +349,8 @@ const _toMatchSnapshot = ({ `${RECEIVED_COLOR('Received value')} ` + `${actual}`; } else { - expected = (expected || '').trim(); - actual = (actual || '').trim(); + expected = utils.removeExtraLineBreaks(expected || ''); + actual = utils.removeExtraLineBreaks(actual || ''); // Assign to local variable because of declaration let expected: // TypeScript thinks it could change before report function is called. diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index 6819e67e85f9..965cd29d5b02 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -125,8 +125,16 @@ export const getSnapshotData = ( // Extra line breaks at the beginning and at the end of the snapshot are useful // to make the content of the snapshot easier to read -const addExtraLineBreaks = (string: string): string => +export const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; +// +// Remove extra line breaks at beginning and end of multiline snapshot. +// Instead of trim, which can remove additional newlines or spaces +// at beginning or end of the content from a custom serializer. +export const removeExtraLineBreaks = (string: string): string => + string.length > 2 && string.startsWith('\n') && string.endsWith('\n') + ? string.slice(1, -1) + : string; export const serialize = (data: string): string => addExtraLineBreaks( From edbec81d87516d736ee7a57c1c84169a1541b8f7 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 20 Aug 2019 17:54:07 -0400 Subject: [PATCH 2/5] Rewrite expected in 2 tests as string literal instead of joined array --- packages/jest-snapshot/src/__tests__/utils.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/utils.test.ts b/packages/jest-snapshot/src/__tests__/utils.test.ts index 50a74b185a8f..c96db37cc8cd 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.ts +++ b/packages/jest-snapshot/src/__tests__/utils.test.ts @@ -246,7 +246,7 @@ describe('ExtraLineBreaks', () => { }); test('2 lines first is blank', () => { - const expected = ['', 'second line '].join('\n'); + const expected = '\nsecond line '; const added = addExtraLineBreaks(expected); const removed = removeExtraLineBreaks(added); @@ -256,7 +256,7 @@ describe('ExtraLineBreaks', () => { }); test('2 lines last is blank', () => { - const expected = [' first line', ''].join('\n'); + const expected = ' first line\n'; const added = addExtraLineBreaks(expected); const removed = removeExtraLineBreaks(added); From ffa643d5c59fae1c4d44258991a454e030839e5e Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 20 Aug 2019 17:54:44 -0400 Subject: [PATCH 3/5] Delete stray comment --- packages/jest-snapshot/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index 965cd29d5b02..227d80565a91 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -127,7 +127,7 @@ export const getSnapshotData = ( // to make the content of the snapshot easier to read export const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; -// + // Remove extra line breaks at beginning and end of multiline snapshot. // Instead of trim, which can remove additional newlines or spaces // at beginning or end of the content from a custom serializer. From 25b4e8154bac2e9779658960ce39a91075da0786 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 20 Aug 2019 17:56:24 -0400 Subject: [PATCH 4/5] Edit comment for add to be parallel with remove --- packages/jest-snapshot/src/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index 227d80565a91..254b1e677d59 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -123,8 +123,8 @@ export const getSnapshotData = ( return {data, dirty}; }; -// Extra line breaks at the beginning and at the end of the snapshot are useful -// to make the content of the snapshot easier to read +// Add extra line breaks at beginning and end of multiline snapshot +// to make the content easier to read. export const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; From 1d25837e572fc5e73197c6812423487935b6b6cd Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 20 Aug 2019 17:58:58 -0400 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index accab0fc74f8..085cfca36103 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389)) - `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764)) +- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859)) ### Chore & Maintenance