From a437b65697db040f5769675d94549c0ffccb01a6 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 3 Jan 2025 13:29:53 +0900 Subject: [PATCH] fix(snapshot): preserve white space of `toMatchFileSnapshot` (#7156) --- packages/snapshot/src/client.ts | 4 +-- packages/snapshot/src/port/state.ts | 8 +++--- test/core/test/snapshot-1.txt | 2 ++ test/core/test/snapshot-2.txt | 10 +++++++ test/snapshots/test/file.test.ts | 28 +++++++++++++++++++ .../test/fixtures/file/basic.test.ts | 25 +++++++++++++++++ .../test/fixtures/file/snapshot-1.txt | 3 ++ .../test/fixtures/file/snapshot-2.txt | 10 +++++++ 8 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 test/core/test/snapshot-1.txt create mode 100644 test/core/test/snapshot-2.txt create mode 100644 test/snapshots/test/file.test.ts create mode 100644 test/snapshots/test/fixtures/file/basic.test.ts create mode 100644 test/snapshots/test/fixtures/file/snapshot-1.txt create mode 100644 test/snapshots/test/fixtures/file/snapshot-2.txt diff --git a/packages/snapshot/src/client.ts b/packages/snapshot/src/client.ts index 39858802feca..00aaf0de1b4e 100644 --- a/packages/snapshot/src/client.ts +++ b/packages/snapshot/src/client.ts @@ -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(), ) } } diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index 89e777e4155f..293f16ca0440 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -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 @@ -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, diff --git a/test/core/test/snapshot-1.txt b/test/core/test/snapshot-1.txt new file mode 100644 index 000000000000..bb84469f463a --- /dev/null +++ b/test/core/test/snapshot-1.txt @@ -0,0 +1,2 @@ + + white space diff --git a/test/core/test/snapshot-2.txt b/test/core/test/snapshot-2.txt new file mode 100644 index 000000000000..d95b0ad80439 --- /dev/null +++ b/test/core/test/snapshot-2.txt @@ -0,0 +1,10 @@ +example: | + { + echo "hello" + } +some: + nesting: + - "hello world" +even: + more: + nesting: true diff --git a/test/snapshots/test/file.test.ts b/test/snapshots/test/file.test.ts new file mode 100644 index 000000000000..0aa5b246b5d6 --- /dev/null +++ b/test/snapshots/test/file.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/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) +}) diff --git a/test/snapshots/test/fixtures/file/basic.test.ts b/test/snapshots/test/fixtures/file/basic.test.ts new file mode 100644 index 000000000000..e03f6e1a5a82 --- /dev/null +++ b/test/snapshots/test/fixtures/file/basic.test.ts @@ -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') +}) diff --git a/test/snapshots/test/fixtures/file/snapshot-1.txt b/test/snapshots/test/fixtures/file/snapshot-1.txt new file mode 100644 index 000000000000..a79023f40904 --- /dev/null +++ b/test/snapshots/test/fixtures/file/snapshot-1.txt @@ -0,0 +1,3 @@ + + + white space diff --git a/test/snapshots/test/fixtures/file/snapshot-2.txt b/test/snapshots/test/fixtures/file/snapshot-2.txt new file mode 100644 index 000000000000..d95b0ad80439 --- /dev/null +++ b/test/snapshots/test/fixtures/file/snapshot-2.txt @@ -0,0 +1,10 @@ +example: | + { + echo "hello" + } +some: + nesting: + - "hello world" +even: + more: + nesting: true