[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(snapshot): fix indent normalization (#7400)

authored by

Hiroshi Ogawa and committed by
GitHub
(Mar 28, 2025, 1:46 PM +0100) 8299709d f3420653

+68 -33
+28
test/snapshots/test/indent.test.ts
··· 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 + })
+2 -3
packages/snapshot/src/port/state.ts
··· 28 28 getSnapshotData, 29 29 keyToTestName, 30 30 normalizeNewlines, 31 - prepareExpected, 32 31 removeExtraLineBreaks, 33 32 saveSnapshotFile, 34 33 serialize, ··· 310 309 : rawSnapshot 311 310 ? rawSnapshot.content 312 311 : 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()) 315 314 const hasSnapshot = expected !== undefined 316 315 const snapshotIsPersisted 317 316 = isInline
-30
packages/snapshot/src/port/utils.ts
··· 175 175 await environment.saveSnapshotFile(snapshotPath, content) 176 176 } 177 177 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 - 208 178 function deepMergeArray(target: any[] = [], source: any[] = []) { 209 179 const mergedOutput = Array.from(target) 210 180
+28
test/snapshots/test/fixtures/indent/basic.test.ts
··· 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 + })
+10
test/snapshots/test/fixtures/indent/__snapshots__/basic.test.ts.snap
··· 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 + `;