From a5e422f941bf9fbef96c6477704ccde6667e90e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 4 May 2022 19:04:01 +0000 Subject: [PATCH] fix(inline-snapshots): detect linebreaks --- packages/vitest/src/integrations/snapshot/port/utils.ts | 4 ++-- test/core/test/snapshot-inline.test.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/integrations/snapshot/port/utils.ts b/packages/vitest/src/integrations/snapshot/port/utils.ts index f1c97198af79..049beeb4eb16 100644 --- a/packages/vitest/src/integrations/snapshot/port/utils.ts +++ b/packages/vitest/src/integrations/snapshot/port/utils.ts @@ -161,7 +161,7 @@ export async function saveSnapshotFile( export function prepareExpected(expected?: string) { function findStartIndent() { - const match = /^( +)}\s+$/m.exec(expected || '') + const match = /^( +)[}|"]/m.exec(expected || '') return match?.[1]?.length || 0 } @@ -171,7 +171,7 @@ export function prepareExpected(expected?: string) { if (startIndent) { expectedTrimmed = expectedTrimmed - ?.replace(new RegExp(`^${' '.repeat(startIndent)}`, 'gm'), '').replace(/ +}$/, '}') + ?.replace(new RegExp(`^${' '.repeat(startIndent)}`, 'gm'), '').replace(/ +[}|"]$/, '}') } return expectedTrimmed diff --git a/test/core/test/snapshot-inline.test.ts b/test/core/test/snapshot-inline.test.ts index 9b99cf072b18..4ce0aac94be6 100644 --- a/test/core/test/snapshot-inline.test.ts +++ b/test/core/test/snapshot-inline.test.ts @@ -80,6 +80,15 @@ test('throwing inline snapshots', () => { }, } `) + + expect(() => { + throw ['Inline', 'snapshot', 'with', 'newlines'].join('\n') + }).toThrowErrorMatchingInlineSnapshot(` + "Inline + snapshot + with + newlines" + `) }) test('properties inline snapshot', () => {