From b65674aebf022870cdd702391b8fe335dfa9e32f Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 7 Oct 2026 15:45:04 +0200 Subject: [PATCH] fix: handle line endings in snapshot --- packages/snapshot/src/port/utils.ts | 3 +- test/e2e/snapshots/snapshots.test.ts | 49 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/snapshot/src/port/utils.ts b/packages/snapshot/src/port/utils.ts index f28926ce0..04cffc498 100644 --- a/packages/snapshot/src/port/utils.ts +++ b/packages/snapshot/src/port/utils.ts @@ -90,7 +90,8 @@ export function serialize( } function escapeBacktickString(str: string): string { - return str.replace(/`|\\|\$\{/g, '\\$&') + // a raw CR inside a template literal is read back as LF + return str.replace(/`|\\|\$\{/g, '\\$&').replace(/\r/g, '\\r') } function printBacktickString(str: string): string { diff --git a/test/e2e/snapshots/snapshots.test.ts b/test/e2e/snapshots/snapshots.test.ts index 7b7f4afaf..e5c425b58 100644 --- a/test/e2e/snapshots/snapshots.test.ts +++ b/test/e2e/snapshots/snapshots.test.ts @@ -111,3 +111,52 @@ test.fails('soft', () => { } `) }) + +test('test names with line endings match their snapshots on the next run', async () => { + const result = await runInlineTests( + { + 'basic.test.ts': ` +import { expect, test } from 'vitest' + +test.for(['a\\nb', 'a\\r\\nb', 'a\\rb'])('%s', (input) => { + expect(input.length).toMatchSnapshot() +}) +`, + }, + { update: 'new' }, + ) + expect(result.stderr).toBe('') + expect(result.fs.readFile('__snapshots__/basic.test.ts.snap')).toMatchInlineSnapshot(` + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + + exports[\`a + b 1\`] = \`3\`; + + exports[\`a\\r + b 1\`] = \`4\`; + + exports[\`a\\rb 1\`] = \`3\`; + " + `) + + const rerun = await runVitest({ root: result.root, update: 'none' }) + expect(rerun.stderr).toBe('') + expect(rerun.ctx?.snapshot.summary).toMatchInlineSnapshot(` + { + "added": 0, + "didUpdate": false, + "failure": false, + "filesAdded": 0, + "filesRemoved": 0, + "filesRemovedList": [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 3, + "total": 3, + "unchecked": 0, + "uncheckedKeysByFile": [], + "unmatched": 0, + "updated": 0, + } + `) +}) -- 2.51.2