Skip to content

Commit 8299709

Browse files
authored
fix(snapshot): fix indent normalization (#7400)
1 parent f342065 commit 8299709

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed

packages/snapshot/src/port/state.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
getSnapshotData,
2929
keyToTestName,
3030
normalizeNewlines,
31-
prepareExpected,
3231
removeExtraLineBreaks,
3332
saveSnapshotFile,
3433
serialize,
@@ -310,8 +309,8 @@ export default class SnapshotState {
310309
: rawSnapshot
311310
? rawSnapshot.content
312311
: this._snapshotData[key]
313-
const expectedTrimmed = rawSnapshot ? expected : prepareExpected(expected)
314-
const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized))
312+
const expectedTrimmed = rawSnapshot ? expected : expected?.trim()
313+
const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : receivedSerialized.trim())
315314
const hasSnapshot = expected !== undefined
316315
const snapshotIsPersisted
317316
= isInline

packages/snapshot/src/port/utils.ts

-30
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,6 @@ export async function saveSnapshotFileRaw(
175175
await environment.saveSnapshotFile(snapshotPath, content)
176176
}
177177

178-
export function prepareExpected(expected?: string): string | undefined {
179-
function findStartIndent() {
180-
// Attempts to find indentation for objects.
181-
// Matches the ending tag of the object.
182-
const matchObject = /^( +)\}\s+$/m.exec(expected || '')
183-
const objectIndent = matchObject?.[1]?.length
184-
185-
if (objectIndent) {
186-
return objectIndent
187-
}
188-
189-
// Attempts to find indentation for texts.
190-
// Matches the quote of first line.
191-
const matchText = /^\n( +)"/.exec(expected || '')
192-
return matchText?.[1]?.length || 0
193-
}
194-
195-
const startIndent = findStartIndent()
196-
197-
let expectedTrimmed = expected?.trim()
198-
199-
if (startIndent) {
200-
expectedTrimmed = expectedTrimmed
201-
?.replace(new RegExp(`^${' '.repeat(startIndent)}`, 'gm'), '')
202-
.replace(/ +\}$/, '}')
203-
}
204-
205-
return expectedTrimmed
206-
}
207-
208178
function deepMergeArray(target: any[] = [], source: any[] = []) {
209179
const mergedOutput = Array.from(target)
210180

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`toMatchSnapshot string 1`] = `
4+
"
5+
1111
6+
xxxx {
7+
}
8+
9+
"
10+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test, expect } from "vitest"
2+
3+
// pnpm -C test/snapshots test:fixtures --root test/fixtures/indent
4+
5+
test('toMatchSnapshot string', () => {
6+
expect(`
7+
1111
8+
xxxx {
9+
}
10+
11+
`).toMatchSnapshot()
12+
})
13+
14+
test('toMatchInlineSnapshot string', () => {
15+
expect(`
16+
2222
17+
yyyy {
18+
}
19+
20+
`).toMatchInlineSnapshot(`
21+
"
22+
2222
23+
yyyy {
24+
}
25+
26+
"
27+
`)
28+
})

test/snapshots/test/indent.test.ts

+28
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/indent')
7+
8+
// ensure correct snapshot
9+
let vitest = await runVitest({ root, update: true })
10+
expect(vitest.exitCode).toBe(0)
11+
12+
// check diff of wrong snapshot
13+
editFile(join(root, 'basic.test.ts'), s => s.replace('1111', 'aaaa').replace('2222', 'bbbb'))
14+
vitest = await runVitest({ root })
15+
expect(vitest.stderr).toContain(`
16+
- 1111
17+
+ aaaa
18+
xxxx {
19+
}
20+
`)
21+
expect(vitest.stderr).toContain(`
22+
- 2222
23+
+ bbbb
24+
yyyy {
25+
}
26+
`)
27+
expect(vitest.exitCode).toBe(1)
28+
})

0 commit comments

Comments
 (0)