Skip to content

Commit

Permalink
feat(inline-snapshot): support comment (#2077)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaleta committed Oct 7, 2022
1 parent dd7c80b commit 53d8f3b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
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\\",
},
}
\`)
"
`)
})
})

0 comments on commit 53d8f3b

Please sign in to comment.