From e918da39d2d4467f53f32458b300d93d5f582e17 Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Tue, 8 Feb 2022 21:03:38 +0800 Subject: [PATCH 1/2] feat: test for mulitiline strings error --- .../test/__snapshots__/shapshots.test.ts.snap | 12 ++++++++++++ test/snapshots/test/shapshots.test.ts | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/snapshots/test/__snapshots__/shapshots.test.ts.snap diff --git a/test/snapshots/test/__snapshots__/shapshots.test.ts.snap b/test/snapshots/test/__snapshots__/shapshots.test.ts.snap new file mode 100644 index 000000000000..a3bdd937d41c --- /dev/null +++ b/test/snapshots/test/__snapshots__/shapshots.test.ts.snap @@ -0,0 +1,12 @@ +// Vitest Snapshot v1 + +exports[`multiline strings 1`] = ` +" +export default function () { + function Foo() { + } + + return Foo; +} +" +`; diff --git a/test/snapshots/test/shapshots.test.ts b/test/snapshots/test/shapshots.test.ts index 220512dbb146..b2243a113038 100644 --- a/test/snapshots/test/shapshots.test.ts +++ b/test/snapshots/test/shapshots.test.ts @@ -1,3 +1,16 @@ +const println = () => { + const message = ` +export default function () { + function Foo() { + } + + return Foo; +} +`; + return message; +} + + test('non default snapshot format', () => { expect({ foo: ['bar'] }).toMatchInlineSnapshot(` Object { @@ -7,3 +20,7 @@ test('non default snapshot format', () => { } `) }) + +test('multiline strings ', () => { + expect(println()).toMatchSnapshot(); +}); From 28c5ea44d6ac2b15570f3f35e0bad2363b587fb3 Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Wed, 9 Feb 2022 16:25:33 +0800 Subject: [PATCH 2/2] fix: prepareExpected receivedSerialized --- packages/vitest/src/integrations/snapshot/port/state.ts | 2 +- test/snapshots/test/shapshots.test.ts | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/integrations/snapshot/port/state.ts b/packages/vitest/src/integrations/snapshot/port/state.ts index f07f19a77e1f..7025cd90bba5 100644 --- a/packages/vitest/src/integrations/snapshot/port/state.ts +++ b/packages/vitest/src/integrations/snapshot/port/state.ts @@ -193,7 +193,7 @@ export default class SnapshotState { const receivedSerialized = addExtraLineBreaks(serialize(received, undefined, this._snapshotFormat)) const expected = isInline ? inlineSnapshot : this._snapshotData[key] const expectedTrimmed = prepareExpected(expected) - const pass = expectedTrimmed === receivedSerialized?.trim() + const pass = expectedTrimmed === prepareExpected(receivedSerialized) const hasSnapshot = expected !== undefined const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath) diff --git a/test/snapshots/test/shapshots.test.ts b/test/snapshots/test/shapshots.test.ts index b2243a113038..a10c2b8fdecf 100644 --- a/test/snapshots/test/shapshots.test.ts +++ b/test/snapshots/test/shapshots.test.ts @@ -6,11 +6,10 @@ export default function () { return Foo; } -`; - return message; +` + return message } - test('non default snapshot format', () => { expect({ foo: ['bar'] }).toMatchInlineSnapshot(` Object { @@ -22,5 +21,5 @@ test('non default snapshot format', () => { }) test('multiline strings ', () => { - expect(println()).toMatchSnapshot(); -}); + expect(println()).toMatchSnapshot() +})