Skip to content

Commit a437b65

Browse files
authored
fix(snapshot): preserve white space of toMatchFileSnapshot (#7156)
1 parent c98b4b1 commit a437b65

File tree

8 files changed

+84
-6
lines changed

8 files changed

+84
-6
lines changed

packages/snapshot/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ export class SnapshotClient {
164164
throw createMismatchError(
165165
`Snapshot \`${key || 'unknown'}\` mismatched`,
166166
snapshotState.expand,
167-
actual?.trim(),
168-
expected?.trim(),
167+
rawSnapshot ? actual : actual?.trim(),
168+
rawSnapshot ? expected : expected?.trim(),
169169
)
170170
}
171171
}

packages/snapshot/src/port/state.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ export default class SnapshotState {
290290
: rawSnapshot
291291
? rawSnapshot.content
292292
: this._snapshotData[key]
293-
const expectedTrimmed = prepareExpected(expected)
294-
const pass = expectedTrimmed === prepareExpected(receivedSerialized)
293+
const expectedTrimmed = rawSnapshot ? expected : prepareExpected(expected)
294+
const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized))
295295
const hasSnapshot = expected !== undefined
296296
const snapshotIsPersisted
297297
= isInline
@@ -390,11 +390,11 @@ export default class SnapshotState {
390390
if (!pass) {
391391
this.unmatched.increment(testId)
392392
return {
393-
actual: removeExtraLineBreaks(receivedSerialized),
393+
actual: rawSnapshot ? receivedSerialized : removeExtraLineBreaks(receivedSerialized),
394394
count,
395395
expected:
396396
expectedTrimmed !== undefined
397-
? removeExtraLineBreaks(expectedTrimmed)
397+
? rawSnapshot ? expectedTrimmed : removeExtraLineBreaks(expectedTrimmed)
398398
: undefined,
399399
key,
400400
pass: false,

test/core/test/snapshot-1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
white space

test/core/test/snapshot-2.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
example: |
2+
{
3+
echo "hello"
4+
}
5+
some:
6+
nesting:
7+
- "hello world"
8+
even:
9+
more:
10+
nesting: true

test/snapshots/test/file.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { join } from 'node:path'
2+
import { expect, test } from 'vitest'
3+
import { editFile, runVitest } from '../../test-utils'
4+
5+
test('white space sensitive', async () => {
6+
const root = join(import.meta.dirname, 'fixtures/file')
7+
8+
// check correct snapshot
9+
let vitest = await runVitest({ root })
10+
expect(vitest.exitCode).toBe(0)
11+
12+
// check diff of wrong snapshot
13+
editFile(join(root, 'snapshot-1.txt'), s => s.trim())
14+
editFile(join(root, 'snapshot-2.txt'), s => s.replace('echo', 'ECHO'))
15+
vitest = await runVitest({ root })
16+
expect(vitest.stderr).toContain(`
17+
- white space
18+
+
19+
+
20+
+ white space
21+
+
22+
`)
23+
expect(vitest.stderr).toContain(`
24+
- ECHO "hello"
25+
+ echo "hello"
26+
`)
27+
expect(vitest.exitCode).toBe(1)
28+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test, expect } from "vitest"
2+
3+
// pnpm -C test/snapshots test:fixtures --root test/fixtures/file
4+
5+
test('white space', async () => {
6+
await expect(`
7+
8+
white space
9+
`).toMatchFileSnapshot('snapshot-1.txt')
10+
})
11+
12+
test('indent', async () => {
13+
await expect(`\
14+
example: |
15+
{
16+
echo "hello"
17+
}
18+
some:
19+
nesting:
20+
- "hello world"
21+
even:
22+
more:
23+
nesting: true
24+
`).toMatchFileSnapshot('snapshot-2.txt')
25+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
white space
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
example: |
2+
{
3+
echo "hello"
4+
}
5+
some:
6+
nesting:
7+
- "hello world"
8+
even:
9+
more:
10+
nesting: true

0 commit comments

Comments
 (0)