From 21c62182449a5b89f083808040ffddd46ab20608 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 5 Dec 2023 12:42:15 +0900 Subject: [PATCH 1/2] test: add repro --- test/core/test/snapshot-inline.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/core/test/snapshot-inline.test.ts b/test/core/test/snapshot-inline.test.ts index 657e7a243940..7c23b8d30923 100644 --- a/test/core/test/snapshot-inline.test.ts +++ b/test/core/test/snapshot-inline.test.ts @@ -100,6 +100,15 @@ test('throwing inline snapshots', async () => { newlines" `) + expect(() => { + throw new Error(['Inline', 'snapshot', 'with', 'newlines'].join('\n')) + }).toThrowErrorMatchingInlineSnapshot(` + [Error: Inline + snapshot + with + newlines] + `) + await expect(async () => { throw new Error('omega') }).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: omega]`) From 4a8714e00c83adba1f509861f031f5489e82dfb0 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 5 Dec 2023 12:53:48 +0900 Subject: [PATCH 2/2] fix: apply `stripSnapshotIndentation` for thrown snapshot --- packages/vitest/src/integrations/snapshot/chai.ts | 3 +++ test/core/test/snapshot-inline.test.ts | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/packages/vitest/src/integrations/snapshot/chai.ts b/packages/vitest/src/integrations/snapshot/chai.ts index 4f86d0b0668f..6ee0b0e62d6e 100644 --- a/packages/vitest/src/integrations/snapshot/chai.ts +++ b/packages/vitest/src/integrations/snapshot/chai.ts @@ -154,6 +154,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { const promise = utils.flag(this, 'promise') as string | undefined const errorMessage = utils.flag(this, 'message') + if (inlineSnapshot) + inlineSnapshot = stripSnapshotIndentation(inlineSnapshot) + getSnapshotClient().assert({ received: getError(expected, promise), message, diff --git a/test/core/test/snapshot-inline.test.ts b/test/core/test/snapshot-inline.test.ts index 7c23b8d30923..fa91f4f74873 100644 --- a/test/core/test/snapshot-inline.test.ts +++ b/test/core/test/snapshot-inline.test.ts @@ -109,6 +109,13 @@ test('throwing inline snapshots', async () => { newlines] `) + expect(new Error(['Inline', 'snapshot', 'with', 'newlines'].join('\n'))).toMatchInlineSnapshot(` + [Error: Inline + snapshot + with + newlines] + `) + await expect(async () => { throw new Error('omega') }).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: omega]`)