镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 4c468aa

Browse files
authored
fix: handle line endings in snapshot (#11522)
1 parent 662e8a3 commit 4c468aa

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎packages/snapshot/src/port/utils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export function serialize(
9090
}
9191

9292
function escapeBacktickString(str: string): string {
93-
return str.replace(/`|\\|\$\{/g, '\\$&')
93+
// a raw CR inside a template literal is read back as LF
94+
return str.replace(/`|\\|\$\{/g, '\\$&').replace(/\r/g, '\\r')
9495
}
9596

9697
function printBacktickString(str: string): string {

‎test/e2e/snapshots/snapshots.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,52 @@ test.fails('soft', () => {
111111
}
112112
`)
113113
})
114+
115+
test('test names with line endings match their snapshots on the next run', async () => {
116+
const result = await runInlineTests(
117+
{
118+
'basic.test.ts': `
119+
import { expect, test } from 'vitest'
120+
121+
test.for(['a\\nb', 'a\\r\\nb', 'a\\rb'])('%s', (input) => {
122+
expect(input.length).toMatchSnapshot()
123+
})
124+
`,
125+
},
126+
{ update: 'new' },
127+
)
128+
expect(result.stderr).toBe('')
129+
expect(result.fs.readFile('__snapshots__/basic.test.ts.snap')).toMatchInlineSnapshot(`
130+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
131+
132+
exports[\`a
133+
b 1\`] = \`3\`;
134+
135+
exports[\`a\\r
136+
b 1\`] = \`4\`;
137+
138+
exports[\`a\\rb 1\`] = \`3\`;
139+
"
140+
`)
141+
142+
const rerun = await runVitest({ root: result.root, update: 'none' })
143+
expect(rerun.stderr).toBe('')
144+
expect(rerun.ctx?.snapshot.summary).toMatchInlineSnapshot(`
145+
{
146+
"added": 0,
147+
"didUpdate": false,
148+
"failure": false,
149+
"filesAdded": 0,
150+
"filesRemoved": 0,
151+
"filesRemovedList": [],
152+
"filesUnmatched": 0,
153+
"filesUpdated": 0,
154+
"matched": 3,
155+
"total": 3,
156+
"unchecked": 0,
157+
"uncheckedKeysByFile": [],
158+
"unmatched": 0,
159+
"updated": 0,
160+
}
161+
`)
162+
})

0 commit comments

Comments
 (0)