Skip to content

Commit bc5c3c7

Browse files
pedrottimarkSimenB
authored andcommitted
jest-snapshot: Remove only the added newlines in multiline snapshots (#8859)
1 parent d523fa8 commit bc5c3c7

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389))
88
- `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764))
9+
- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859))
910

1011
### Chore & Maintenance
1112

packages/jest-snapshot/src/__tests__/utils.test.ts

+74
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ import {
1919
SNAPSHOT_GUIDE_LINK,
2020
SNAPSHOT_VERSION,
2121
SNAPSHOT_VERSION_WARNING,
22+
addExtraLineBreaks,
2223
deepMerge,
2324
getSnapshotData,
2425
keyToTestName,
26+
removeExtraLineBreaks,
2527
saveSnapshotFile,
2628
serialize,
2729
testNameToKey,
@@ -192,6 +194,78 @@ test('serialize handles \\r\\n', () => {
192194
expect(serializedData).toBe('\n"<div>\n</div>"\n');
193195
});
194196

197+
describe('ExtraLineBreaks', () => {
198+
test('0 empty string', () => {
199+
const expected = '';
200+
201+
const added = addExtraLineBreaks(expected);
202+
const removed = removeExtraLineBreaks(added);
203+
204+
expect(added).toBe(expected);
205+
expect(removed).toBe(expected);
206+
});
207+
208+
test('1 line has double quote marks at edges', () => {
209+
const expected = '" one line "';
210+
211+
const added = addExtraLineBreaks(expected);
212+
const removed = removeExtraLineBreaks(added);
213+
214+
expect(added).toBe(expected);
215+
expect(removed).toBe(expected);
216+
});
217+
218+
test('1 line has spaces at edges', () => {
219+
const expected = ' one line ';
220+
221+
const added = addExtraLineBreaks(expected);
222+
const removed = removeExtraLineBreaks(added);
223+
224+
expect(added).toBe(expected);
225+
expect(removed).toBe(expected);
226+
});
227+
228+
test('2 lines both are blank', () => {
229+
const expected = '\n';
230+
231+
const added = addExtraLineBreaks(expected);
232+
const removed = removeExtraLineBreaks(added);
233+
234+
expect(added).toBe('\n' + expected + '\n');
235+
expect(removed).toBe(expected);
236+
});
237+
238+
test('2 lines have double quote marks at edges', () => {
239+
const expected = '"\n"';
240+
241+
const added = addExtraLineBreaks(expected);
242+
const removed = removeExtraLineBreaks(added);
243+
244+
expect(added).toBe('\n' + expected + '\n');
245+
expect(removed).toBe(expected);
246+
});
247+
248+
test('2 lines first is blank', () => {
249+
const expected = '\nsecond line ';
250+
251+
const added = addExtraLineBreaks(expected);
252+
const removed = removeExtraLineBreaks(added);
253+
254+
expect(added).toBe('\n' + expected + '\n');
255+
expect(removed).toBe(expected);
256+
});
257+
258+
test('2 lines last is blank', () => {
259+
const expected = ' first line\n';
260+
261+
const added = addExtraLineBreaks(expected);
262+
const removed = removeExtraLineBreaks(added);
263+
264+
expect(added).toBe('\n' + expected + '\n');
265+
expect(removed).toBe(expected);
266+
});
267+
});
268+
195269
describe('DeepMerge with property matchers', () => {
196270
const matcher = expect.any(String);
197271

packages/jest-snapshot/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ const _toMatchSnapshot = ({
349349
`${RECEIVED_COLOR('Received value')} ` +
350350
`${actual}`;
351351
} else {
352-
expected = (expected || '').trim();
353-
actual = (actual || '').trim();
352+
expected = utils.removeExtraLineBreaks(expected || '');
353+
actual = utils.removeExtraLineBreaks(actual || '');
354354

355355
// Assign to local variable because of declaration let expected:
356356
// TypeScript thinks it could change before report function is called.

packages/jest-snapshot/src/utils.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,19 @@ export const getSnapshotData = (
123123
return {data, dirty};
124124
};
125125

126-
// Extra line breaks at the beginning and at the end of the snapshot are useful
127-
// to make the content of the snapshot easier to read
128-
const addExtraLineBreaks = (string: string): string =>
126+
// Add extra line breaks at beginning and end of multiline snapshot
127+
// to make the content easier to read.
128+
export const addExtraLineBreaks = (string: string): string =>
129129
string.includes('\n') ? `\n${string}\n` : string;
130130

131+
// Remove extra line breaks at beginning and end of multiline snapshot.
132+
// Instead of trim, which can remove additional newlines or spaces
133+
// at beginning or end of the content from a custom serializer.
134+
export const removeExtraLineBreaks = (string: string): string =>
135+
string.length > 2 && string.startsWith('\n') && string.endsWith('\n')
136+
? string.slice(1, -1)
137+
: string;
138+
131139
export const serialize = (data: string): string =>
132140
addExtraLineBreaks(
133141
normalizeNewlines(

0 commit comments

Comments
 (0)