Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inline-snapshot): support mix of normal/with placeholders snapshots #4118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/snapshot/src/port/inlineSnapshot.ts
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
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
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
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"
}
}
}
`)
})
})