Skip to content

Commit

Permalink
fix: don't escape $ in snapshots, when not needed (#1401)
Browse files Browse the repository at this point in the history
* fix: don't replace $ in snapshots, when not needed

* test: remove \ in test
  • Loading branch information
sheremet-va committed May 31, 2022
1 parent b7bfc09 commit c5b7a72
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Expand Up @@ -61,15 +61,14 @@ function prepareSnapString(snap: string, source: string, index: number) {
const lines = snap
.trim()
.replace(/\\/g, '\\\\')
.replace(/\$/g, '\\$')
.split(/\n/g)

const isOneline = lines.length <= 1
const quote = isOneline ? '\'' : '`'
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
Expand Down
10 changes: 10 additions & 0 deletions test/core/test/snapshot-inline.test.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit c5b7a72

Please sign in to comment.