Content-Length: 398813 | pFad | https://redirect.github.com/vitest-dev/vitest/commit/a437b656

16 fix(snapshot): preserve white space of `toMatchFileSnapshot` (#7156) · vitest-dev/vitest@a437b65 · GitHub
Skip to content

Commit

Permalink
fix(snapshot): preserve white space of toMatchFileSnapshot (#7156)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Jan 3, 2025
1 parent c98b4b1 commit a437b65
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 6 deletions.
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

0 comments on commit a437b65

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://redirect.github.com/vitest-dev/vitest/commit/a437b656

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy