Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshot): preserve white space of toMatchFileSnapshot #7156

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ export class SnapshotClient {
throw createMismatchError(
`Snapshot \`${key || 'unknown'}\` mismatched`,
snapshotState.expand,
actual?.trim(),
expected?.trim(),
rawSnapshot ? actual : actual?.trim(),
rawSnapshot ? expected : expected?.trim(),
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ export default class SnapshotState {
: rawSnapshot
? rawSnapshot.content
: this._snapshotData[key]
const expectedTrimmed = prepareExpected(expected)
const pass = expectedTrimmed === prepareExpected(receivedSerialized)
const expectedTrimmed = rawSnapshot ? expected : prepareExpected(expected)
const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized))
const hasSnapshot = expected !== undefined
const snapshotIsPersisted
= isInline
Expand Down Expand Up @@ -390,11 +390,11 @@ export default class SnapshotState {
if (!pass) {
this.unmatched.increment(testId)
return {
actual: removeExtraLineBreaks(receivedSerialized),
actual: rawSnapshot ? receivedSerialized : removeExtraLineBreaks(receivedSerialized),
count,
expected:
expectedTrimmed !== undefined
? removeExtraLineBreaks(expectedTrimmed)
? rawSnapshot ? expectedTrimmed : removeExtraLineBreaks(expectedTrimmed)
: undefined,
key,
pass: false,
Expand Down
2 changes: 2 additions & 0 deletions test/core/test/snapshot-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

white space
10 changes: 10 additions & 0 deletions test/core/test/snapshot-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
example: |
{
echo "hello"
}
some:
nesting:
- "hello world"
even:
more:
nesting: true
28 changes: 28 additions & 0 deletions test/snapshots/test/file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { join } from 'node:path'
import { expect, test } from 'vitest'
import { editFile, runVitest } from '../../test-utils'

test('white space sensitive', async () => {
const root = join(import.meta.dirname, 'fixtures/file')

// check correct snapshot
let vitest = await runVitest({ root })
expect(vitest.exitCode).toBe(0)

// check diff of wrong snapshot
editFile(join(root, 'snapshot-1.txt'), s => s.trim())
editFile(join(root, 'snapshot-2.txt'), s => s.replace('echo', 'ECHO'))
vitest = await runVitest({ root })
expect(vitest.stderr).toContain(`
- white space
+
+
+ white space
+
`)
expect(vitest.stderr).toContain(`
- ECHO "hello"
+ echo "hello"
`)
expect(vitest.exitCode).toBe(1)
})
25 changes: 25 additions & 0 deletions test/snapshots/test/fixtures/file/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test, expect } from "vitest"

// pnpm -C test/snapshots test:fixtures --root test/fixtures/file

test('white space', async () => {
await expect(`
white space
`).toMatchFileSnapshot('snapshot-1.txt')
})

test('indent', async () => {
await expect(`\
example: |
{
echo "hello"
}
some:
nesting:
- "hello world"
even:
more:
nesting: true
`).toMatchFileSnapshot('snapshot-2.txt')
})
3 changes: 3 additions & 0 deletions test/snapshots/test/fixtures/file/snapshot-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


white space
10 changes: 10 additions & 0 deletions test/snapshots/test/fixtures/file/snapshot-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
example: |
{
echo "hello"
}
some:
nesting:
- "hello world"
even:
more:
nesting: true
Loading