From 874467aeda86fe87fc6588dd74631f4f53ae4d9a Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 2 Jan 2025 11:17:03 +0900 Subject: [PATCH 1/5] fix(snapshot): preserve white spaces of `toMatchFileSnapshot` --- packages/snapshot/src/port/state.ts | 5 +++-- test/core/test/snapshot-1.txt | 2 ++ test/core/test/snapshot-2.txt | 10 ++++++++++ test/core/test/snapshot.test.ts | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/core/test/snapshot-1.txt create mode 100644 test/core/test/snapshot-2.txt diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index 89e777e4155f..9be77b016c93 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -290,8 +290,9 @@ 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 receivedSerializedTrimmed = rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized) + const pass = expectedTrimmed === receivedSerializedTrimmed const hasSnapshot = expected !== undefined const snapshotIsPersisted = isInline 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/core/test/snapshot.test.ts b/test/core/test/snapshot.test.ts index 5f763dbe5319..76060cb4731b 100644 --- a/test/core/test/snapshot.test.ts +++ b/test/core/test/snapshot.test.ts @@ -161,3 +161,21 @@ my string " `) }) + +test('toMatchFileSnapshot preserves white spaces', async () => { + await expect(` + white space +`).toMatchFileSnapshot('snapshot-1.txt') + await expect(`\ +example: | + { + echo "hello" + } +some: + nesting: + - "hello world" +even: + more: + nesting: true +`).toMatchFileSnapshot('snapshot-2.txt') +}) From fb9596cd99bd4351c1c035eab92e493435504ba4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 2 Jan 2025 11:29:07 +0900 Subject: [PATCH 2/5] fix: preserve for diff --- packages/snapshot/src/client.ts | 4 ++-- packages/snapshot/src/port/state.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 9be77b016c93..ffc7dbdcad2f 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -391,11 +391,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, From 7d408e7608eabe12223b55dff682f1a938472302 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 2 Jan 2025 11:57:18 +0900 Subject: [PATCH 3/5] test: more test --- 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 +++++++ 4 files changed, 66 insertions(+) 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/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 From a663121ed0185fe070d15e5bcf4a58a5bac058d9 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 2 Jan 2025 11:57:36 +0900 Subject: [PATCH 4/5] test: cleanup --- test/core/test/snapshot.test.ts | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/test/core/test/snapshot.test.ts b/test/core/test/snapshot.test.ts index 76060cb4731b..5f763dbe5319 100644 --- a/test/core/test/snapshot.test.ts +++ b/test/core/test/snapshot.test.ts @@ -161,21 +161,3 @@ my string " `) }) - -test('toMatchFileSnapshot preserves white spaces', async () => { - await expect(` - white space -`).toMatchFileSnapshot('snapshot-1.txt') - await expect(`\ -example: | - { - echo "hello" - } -some: - nesting: - - "hello world" -even: - more: - nesting: true -`).toMatchFileSnapshot('snapshot-2.txt') -}) From f9fc73ae149a74536417bf343db26943f4fc02da Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 2 Jan 2025 11:58:07 +0900 Subject: [PATCH 5/5] chore: cleanup --- packages/snapshot/src/port/state.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index ffc7dbdcad2f..293f16ca0440 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -291,8 +291,7 @@ export default class SnapshotState { ? rawSnapshot.content : this._snapshotData[key] const expectedTrimmed = rawSnapshot ? expected : prepareExpected(expected) - const receivedSerializedTrimmed = rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized) - const pass = expectedTrimmed === receivedSerializedTrimmed + const pass = expectedTrimmed === (rawSnapshot ? receivedSerialized : prepareExpected(receivedSerialized)) const hasSnapshot = expected !== undefined const snapshotIsPersisted = isInline