From ba85bade1eafed1a7844a4c389f46c8d214c3e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 4 May 2022 18:59:26 +0000 Subject: [PATCH 1/3] style: fix typo --- packages/vitest/src/integrations/snapshot/port/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vitest/src/integrations/snapshot/port/utils.ts b/packages/vitest/src/integrations/snapshot/port/utils.ts index a16f9091f0c1..f1c97198af79 100644 --- a/packages/vitest/src/integrations/snapshot/port/utils.ts +++ b/packages/vitest/src/integrations/snapshot/port/utils.ts @@ -165,13 +165,13 @@ export function prepareExpected(expected?: string) { return match?.[1]?.length || 0 } - const startIdent = findStartIndent() + const startIndent = findStartIndent() let expectedTrimmed = expected?.trim() - if (startIdent) { + if (startIndent) { expectedTrimmed = expectedTrimmed - ?.replace(new RegExp(`^${' '.repeat(startIdent)}`, 'gm'), '').replace(/ +}$/, '}') + ?.replace(new RegExp(`^${' '.repeat(startIndent)}`, 'gm'), '').replace(/ +}$/, '}') } return expectedTrimmed From 1561836390ef1a4da85d10271cd2177614f47253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 4 May 2022 19:00:10 +0000 Subject: [PATCH 2/3] test(inline-snapshot): add test for nested object --- test/core/test/snapshot-inline.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/core/test/snapshot-inline.test.ts b/test/core/test/snapshot-inline.test.ts index 5c357d9c37f7..9b99cf072b18 100644 --- a/test/core/test/snapshot-inline.test.ts +++ b/test/core/test/snapshot-inline.test.ts @@ -67,6 +67,19 @@ test('throwing inline snapshots', () => { "error": "omega", } `) + + expect(() => { + // eslint-disable-next-line no-throw-literal + throw { some: { nested: { error: 'object' } } } + }).toThrowErrorMatchingInlineSnapshot(` + { + "some": { + "nested": { + "error": "object", + }, + }, + } + `) }) test('properties inline snapshot', () => { From 87013b5178b759722dda6ac2afd23e3aed7e387d 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 3/3] fix(inline-snapshots): detect linebreaks --- .../vitest/src/integrations/snapshot/port/utils.ts | 14 ++++++++++++-- test/core/test/snapshot-inline.test.ts | 9 +++++++++ 2 files changed, 21 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..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', () => {