Skip to content

Commit

Permalink
fix(snapshot): normalize EOL for toMatchFileSnapshot (#3164)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 11, 2023
1 parent 905ec05 commit df3f2b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/snapshot/src/port/state.ts
Expand Up @@ -18,6 +18,7 @@ import {
addExtraLineBreaks,
getSnapshotData,
keyToTestName,
normalizeNewlines,
prepareExpected,
removeExtraLineBreaks,
saveSnapshotFile,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/snapshot/src/port/utils.ts
Expand Up @@ -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')
}

Expand Down

0 comments on commit df3f2b5

Please sign in to comment.