diff --git a/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts b/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts index b9bcdb2b3c0d..1480c7030912 100644 --- a/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts +++ b/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts @@ -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) @@ -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) diff --git a/test/core/test/inline-snap.test.ts b/test/core/test/inline-snap.test.ts index 46b25abfc0e9..d8aa13ea731e 100644 --- a/test/core/test/inline-snap.test.ts +++ b/test/core/test/inline-snap.test.ts @@ -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\\", + }, + } + \`) + " + `) + }) })