From df3f2b50eb92777a8a2b3872fd9eb7512d3f4c94 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 11 Apr 2023 09:54:43 +0200 Subject: [PATCH] fix(snapshot): normalize EOL for `toMatchFileSnapshot` (#3164) --- packages/snapshot/src/port/state.ts | 7 +++++++ packages/snapshot/src/port/utils.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index a872c64bc105..f766f0e52642 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -18,6 +18,7 @@ import { addExtraLineBreaks, getSnapshotData, keyToTestName, + normalizeNewlines, prepareExpected, removeExtraLineBreaks, saveSnapshotFile, @@ -244,6 +245,12 @@ export default class SnapshotState { if (!rawSnapshot) receivedSerialized = addExtraLineBreaks(receivedSerialized) + if (rawSnapshot) { + // normalize EOL when snapshot contains CRLF but received is LF + if (rawSnapshot.content && rawSnapshot.content.match(/\r\n/) && !receivedSerialized.match(/\r\n/)) + rawSnapshot.content = normalizeNewlines(rawSnapshot.content) + } + const expected = isInline ? inlineSnapshot : rawSnapshot diff --git a/packages/snapshot/src/port/utils.ts b/packages/snapshot/src/port/utils.ts index a3f7cff2708e..522f11604866 100644 --- a/packages/snapshot/src/port/utils.ts +++ b/packages/snapshot/src/port/utils.ts @@ -135,7 +135,7 @@ export async function ensureDirectoryExists(environment: SnapshotEnvironment, fi catch { } } -function normalizeNewlines(string: string) { +export function normalizeNewlines(string: string) { return string.replace(/\r\n|\r/g, '\n') }