Skip to content

Commit

Permalink
fix(inline-snapshots): detect linebreaks
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 4, 2022
1 parent 1561836 commit 87013b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/vitest/src/integrations/snapshot/port/utils.ts
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions test/core/test/snapshot-inline.test.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 87013b5

Please sign in to comment.