diff --git a/packages/vitest/src/integrations/snapshot/port/utils.ts b/packages/vitest/src/integrations/snapshot/port/utils.ts index f1c97198af79..26fee21bbbbd 100644 --- a/packages/vitest/src/integrations/snapshot/port/utils.ts +++ b/packages/vitest/src/integrations/snapshot/port/utils.ts @@ -161,8 +161,18 @@ export async function saveSnapshotFile( export function prepareExpected(expected?: string) { function findStartIndent() { - const match = /^( +)}\s+$/m.exec(expected || '') - return match?.[1]?.length || 0 + // Attemps 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() 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', () => {