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

feat(inline-snapshot): support comment #2077

Merged
merged 1 commit into from Oct 7, 2022
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
Expand Up @@ -34,7 +34,7 @@ export async function saveInlineSnapshots(
}))
}

const startObjectRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*({)/m
const startObjectRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\S\s]*\*\/\s*|\/\/.*\s+)*\s*({)/m

function replaceObjectSnap(code: string, s: MagicString, index: number, newSnap: string) {
code = code.slice(index)
Expand Down Expand Up @@ -71,7 +71,7 @@ function prepareSnapString(snap: string, source: string, index: number) {
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
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)
Expand Down
101 changes: 101 additions & 0 deletions test/core/test/inline-snap.test.ts
Expand Up @@ -45,4 +45,105 @@ ${indent}}\`)
"
`)
})

it('replaceInlineSnap(string) with block comment(in same line)', async () => {
const code = `
expect('foo').toMatchInlineSnapshot(/* comment1 */'"foo"')
`
const s = new MagicString(code)
replaceInlineSnap(code, s, 0, '"bar"')
expect(s.toString()).toMatchInlineSnapshot(`
"
expect('foo').toMatchInlineSnapshot(/* comment1 */'\\"bar\\"')
"
`)
})

it('replaceInlineSnap(string) with block comment(new line)', async () => {
const code = `
expect('foo').toMatchInlineSnapshot(
/* comment1
comment2
*/

'"foo"')
`
const s = new MagicString(code)
replaceInlineSnap(code, s, 0, '"bar"')
expect(s.toString()).toMatchInlineSnapshot(`
"
expect('foo').toMatchInlineSnapshot(
/* comment1
comment2
*/

'\\"bar\\"')
"
`)
})

it('replaceInlineSnap(string) with single line comment', async () => {
const code = `
expect('foo').toMatchInlineSnapshot(
// comment1
// comment2
'"foo"')
`
const s = new MagicString(code)
replaceInlineSnap(code, s, 0, '"bar"')
expect(s.toString()).toMatchInlineSnapshot(`
"
expect('foo').toMatchInlineSnapshot(
// comment1
// comment2
'\\"bar\\"')
"
`)
})

it('replaceInlineSnap(object) comments', async () => {
const code = `
expect({}).toMatchInlineSnapshot(
// comment1
// comment2
/*
comment3
comment4
*/
\`{
"foo": {
"map": Map {},
"type": "object",
},
}\`)
`
const s = new MagicString(code)
replaceInlineSnap(code, s, 0, `
{
"bar": {
"map2": Map {},
"type": "object1",
},
}
`)
expect(s.toString()).toMatchInlineSnapshot(`
"
expect({}).toMatchInlineSnapshot(
// comment1
// comment2
/*
comment3
comment4
*/
\`
{
\\"bar\\": {
\\"map2\\": Map {},
\\"type\\": \\"object1\\",
},
}
\`)
"
`)
})
})