From c5b7a72d25b0aea327eb399b40c812504998a9f0 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 31 May 2022 08:56:00 +0300 Subject: [PATCH] fix: don't escape $ in snapshots, when not needed (#1401) * fix: don't replace $ in snapshots, when not needed * test: remove \ in test --- .../src/integrations/snapshot/port/inlineSnapshot.ts | 3 +-- test/core/test/snapshot-inline.test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts b/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts index 66fe3192b86b..b9bcdb2b3c0d 100644 --- a/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts +++ b/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts @@ -61,7 +61,6 @@ function prepareSnapString(snap: string, source: string, index: number) { const lines = snap .trim() .replace(/\\/g, '\\\\') - .replace(/\$/g, '\\$') .split(/\n/g) const isOneline = lines.length <= 1 @@ -69,7 +68,7 @@ function prepareSnapString(snap: string, source: string, index: number) { if (isOneline) return `'${lines.join('\n').replace(/'/g, '\\\'')}'` else - return `${quote}\n${lines.map(i => i ? indentNext + i : '').join('\n').replace(/`/g, '\\`')}\n${indent}${quote}` + return `${quote}\n${lines.map(i => i ? indentNext + i : '').join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}\n${indent}${quote}` } const startRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*[\w_$]*(['"`\)])/m diff --git a/test/core/test/snapshot-inline.test.ts b/test/core/test/snapshot-inline.test.ts index 4ce0aac94be6..fda2031e98a2 100644 --- a/test/core/test/snapshot-inline.test.ts +++ b/test/core/test/snapshot-inline.test.ts @@ -19,6 +19,16 @@ test('object', () => { test('single line', () => { expect('inline string').toMatchInlineSnapshot('"inline string"') + expect('inline $ string').toMatchInlineSnapshot('"inline $ string"') + expect('inline multiline\n $string').toMatchInlineSnapshot(` + "inline multiline + $string" + `) + // eslint-disable-next-line no-template-curly-in-string + expect('inline multiline\n ${string}').toMatchInlineSnapshot(` + "inline multiline + \${string}" + `) }) test('multiline', () => {