mirror of
https://github.com/vitest-dev/vitest.git
synced 2025-12-08 18:26:03 +00:00
fix(inline-snapshots): detect linebreaks (#1232)
* style: fix typo * test(inline-snapshot): add test for nested object * fix(inline-snapshots): detect linebreaks
This commit is contained in:
parent
1c7c733b79
commit
4de5da7a56
@ -161,17 +161,27 @@ 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 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
|
||||
|
||||
@ -67,6 +67,28 @@ test('throwing inline snapshots', () => {
|
||||
"error": "omega",
|
||||
}
|
||||
`)
|
||||
|
||||
expect(() => {
|
||||
// eslint-disable-next-line no-throw-literal
|
||||
throw { some: { nested: { error: 'object' } } }
|
||||
}).toThrowErrorMatchingInlineSnapshot(`
|
||||
{
|
||||
"some": {
|
||||
"nested": {
|
||||
"error": "object",
|
||||
},
|
||||
},
|
||||
}
|
||||
`)
|
||||
|
||||
expect(() => {
|
||||
throw ['Inline', 'snapshot', 'with', 'newlines'].join('\n')
|
||||
}).toThrowErrorMatchingInlineSnapshot(`
|
||||
"Inline
|
||||
snapshot
|
||||
with
|
||||
newlines"
|
||||
`)
|
||||
})
|
||||
|
||||
test('properties inline snapshot', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user