Skip to content

Commit

Permalink
fix(snapshot): support mix of normal/with placeholders snapshots (#4118)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertPechaCZ authored Sep 14, 2023
1 parent d79cb44 commit 01e01bf
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
11 changes: 8 additions & 3 deletions packages/snapshot/src/port/inlineSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ function prepareSnapString(snap: string, source: string, index: number) {

const startRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\S\s]*\*\/\s*|\/\/.*\s+)*\s*[\w_$]*(['"`\)])/m
export function replaceInlineSnap(code: string, s: MagicString, index: number, newSnap: string) {
const startMatch = startRegex.exec(code.slice(index))
if (!startMatch)
const codeStartingAtIndex = code.slice(index)

const startMatch = startRegex.exec(codeStartingAtIndex)

const firstKeywordMatch = /toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot/.exec(codeStartingAtIndex)

if (!startMatch || startMatch.index !== firstKeywordMatch?.index)
return replaceObjectSnap(code, s, index, newSnap)

const quote = startMatch[1]
const startIndex = index + startMatch.index! + startMatch[0].length
const startIndex = index + startMatch.index + startMatch[0].length
const snapString = prepareSnapString(newSnap, code, index)

if (quote === ')') {
Expand Down
30 changes: 28 additions & 2 deletions test/snapshots/test-update/snapshots-inline-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,35 @@ describe('snapshots with properties', () => {
})

test('with snapshot', () => {
expect({ foo: 'bar' }).toMatchInlineSnapshot({ foo: expect.any(String) }, `
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
Object {
"foo": Any<String>,
"first": Object {
"second": Object {
"foo": Any<String>,
},
},
}
`)
})

test('mixed with and without snapshot', () => {
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
Object {
"first": Object {
"second": Object {
"foo": Any<String>,
},
},
}
`)

expect({ first: { second: { foo: 'zed' } } }).toMatchInlineSnapshot(`
Object {
"first": Object {
"second": Object {
"foo": "zed",
},
},
}
`)
})
Expand Down
30 changes: 28 additions & 2 deletions test/snapshots/test/__snapshots__/shapshots.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,35 @@ describe('snapshots with properties', () => {
})
test('with snapshot', () => {
expect({ foo: 'bar' }).toMatchInlineSnapshot({ foo: expect.any(String) }, \`
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, \`
Object {
\\"foo\\": Any<String>,
\\"first\\": Object {
\\"second\\": Object {
\\"foo\\": Any<String>,
},
},
}
\`)
})
test('mixed with and without snapshot', () => {
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, \`
Object {
\\"first\\": Object {
\\"second\\": Object {
\\"foo\\": Any<String>,
},
},
}
\`)
expect({ first: { second: { foo: 'zed' } } }).toMatchInlineSnapshot(\`
Object {
\\"first\\": Object {
\\"second\\": Object {
\\"foo\\": \\"zed\\",
},
},
}
\`)
})
Expand Down
30 changes: 29 additions & 1 deletion test/snapshots/tools/inline-test-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ describe('snapshots with properties', () => {
})

test('with snapshot', () => {
expect({ foo: 'bar' }).toMatchInlineSnapshot({ foo: expect.any(String) }, '')
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
Object {
"first": Object {
"wrong": Any<String>,
"second": null,
}
}
`)
})

test('mixed with and without snapshot', () => {
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
Object {
"first": Object {
"wrong": Any<String>,
"second": null,
}
}
`)

expect({ first: { second: { foo: 'zed' } } }).toMatchInlineSnapshot(`
Object {
"first": Object {
"second": {
"foo": "zed"
}
}
}
`)
})
})

0 comments on commit 01e01bf

Please sign in to comment.