diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index c1ad1bd4a..e691822ba 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -28,7 +28,6 @@ import { getSnapshotData, keyToTestName, normalizeNewlines, - prepareExpected, removeExtraLineBreaks, saveSnapshotFile, serialize, @@ -310,8 +309,8 @@ export default class SnapshotState { : rawSnapshot ? rawSnapshot.content : this._snapshotData[key] - const expectedTrimmed = rawSnapshot ? expected : prepareExpected(expected) - const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized)) + const expectedTrimmed = rawSnapshot ? expected : expected?.trim() + const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : receivedSerialized.trim()) const hasSnapshot = expected !== undefined const snapshotIsPersisted = isInline diff --git a/packages/snapshot/src/port/utils.ts b/packages/snapshot/src/port/utils.ts index 5d4696b8d..35fe65b4e 100644 --- a/packages/snapshot/src/port/utils.ts +++ b/packages/snapshot/src/port/utils.ts @@ -175,36 +175,6 @@ export async function saveSnapshotFileRaw( await environment.saveSnapshotFile(snapshotPath, content) } -export function prepareExpected(expected?: string): string | undefined { - function findStartIndent() { - // Attempts to find indentation for objects. - // Matches the ending tag of the object. - const matchObject = /^( +)\}\s+$/m.exec(expected || '') - const objectIndent = matchObject?.[1]?.length - - if (objectIndent) { - return objectIndent - } - - // Attempts to find indentation for texts. - // Matches the quote of first line. - const matchText = /^\n( +)"/.exec(expected || '') - return matchText?.[1]?.length || 0 - } - - const startIndent = findStartIndent() - - let expectedTrimmed = expected?.trim() - - if (startIndent) { - expectedTrimmed = expectedTrimmed - ?.replace(new RegExp(`^${' '.repeat(startIndent)}`, 'gm'), '') - .replace(/ +\}$/, '}') - } - - return expectedTrimmed -} - function deepMergeArray(target: any[] = [], source: any[] = []) { const mergedOutput = Array.from(target) diff --git a/test/snapshots/test/fixtures/indent/__snapshots__/basic.test.ts.snap b/test/snapshots/test/fixtures/indent/__snapshots__/basic.test.ts.snap new file mode 100644 index 000000000..98d273d5d --- /dev/null +++ b/test/snapshots/test/fixtures/indent/__snapshots__/basic.test.ts.snap @@ -0,0 +1,10 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`toMatchSnapshot string 1`] = ` +" +1111 + xxxx { + } + +" +`; diff --git a/test/snapshots/test/fixtures/indent/basic.test.ts b/test/snapshots/test/fixtures/indent/basic.test.ts new file mode 100644 index 000000000..dbc0a6049 --- /dev/null +++ b/test/snapshots/test/fixtures/indent/basic.test.ts @@ -0,0 +1,28 @@ +import { test, expect } from "vitest" + +// pnpm -C test/snapshots test:fixtures --root test/fixtures/indent + +test('toMatchSnapshot string', () => { + expect(` +1111 + xxxx { + } + +`).toMatchSnapshot() +}) + +test('toMatchInlineSnapshot string', () => { + expect(` +2222 + yyyy { + } + +`).toMatchInlineSnapshot(` + " + 2222 + yyyy { + } + + " +`) +}) diff --git a/test/snapshots/test/indent.test.ts b/test/snapshots/test/indent.test.ts new file mode 100644 index 000000000..32c0c3664 --- /dev/null +++ b/test/snapshots/test/indent.test.ts @@ -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/indent') + + // ensure correct snapshot + let vitest = await runVitest({ root, update: true }) + expect(vitest.exitCode).toBe(0) + + // check diff of wrong snapshot + editFile(join(root, 'basic.test.ts'), s => s.replace('1111', 'aaaa').replace('2222', 'bbbb')) + vitest = await runVitest({ root }) + expect(vitest.stderr).toContain(` +- 1111 ++ aaaa + xxxx { + } +`) + expect(vitest.stderr).toContain(` +- 2222 ++ bbbb + yyyy { + } +`) + expect(vitest.exitCode).toBe(1) +})