Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/snapshot/src/port/inlineSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function saveInlineSnapshots(
}

const defaultStartObjectRegex
= /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\s\S]*\*\/\s*|\/\/.*(?:[\n\r\u2028\u2029]\s*|[\t\v\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF]))*\{/
= /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\s\S]*?\*\/\s*|\/\/.*(?:[\n\r\u2028\u2029]\s*|[\t\v\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF]))*\{/

function escapeRegExp(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
Expand Down Expand Up @@ -160,7 +160,7 @@ function getCodeStartingAtIndex(code: string, index: number, methodNames: string
}

const defaultStartRegex
= /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\s\S]*\*\/\s*|\/\/.*(?:[\n\r\u2028\u2029]\s*|[\t\v\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF]))*[\w$]*(['"`)])/
= /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\s\S]*?\*\/\s*|\/\/.*(?:[\n\r\u2028\u2029]\s*|[\t\v\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF]))*[\w$]*(['"`)])/

const buildStartRegex = memo((assertionName: string) => {
const replaced = defaultStartRegex.source.replace(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

test('block-comment directives, multiple calls', () => {
expect('alpha').toMatchInlineSnapshot(/* HTML */`"alpha"`)
expect('beta').toMatchInlineSnapshot(/* HTML */`"beta"`)
expect('gamma').toMatchInlineSnapshot(/* HTML */`"gamma"`)
})
33 changes: 33 additions & 0 deletions test/e2e/snapshots/inline-comment-directive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'node:fs'
import { join } from 'pathe'
import { expect, test } from 'vitest'
import { editFile, runVitest } from '../../test-utils'

// Regression for the greedy comment-skip in the inline-snapshot updater
// regexes (packages/snapshot/src/port/inlineSnapshot.ts). With multiple
// `toMatchInlineSnapshot(/* … */)` calls carrying block-comment directives
// in one file, the greedy `\/\*[\s\S]*\*\/` matched from the FIRST `/*` to
// the LAST `*/`, so every earlier call's write coalesced onto the last
// call's location. On update only the last snapshot was written, mangled
// (`/* HTML */`"alpha"``"beta"``"gamma"`), and the earlier calls were left
// empty — silently. The fix makes the comment-skip lazy (`[\s\S]*?`).

test('multiple block-comment directives each get written on update', async () => {
const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls')
const testFile = join(root, 'comment-directive.test.ts')

// reset: clear each snapshot to the empty `(/* HTML */)` form that triggers
// the bug, keeping its directive
editFile(testFile, s =>
s.replace(/toMatchInlineSnapshot\(\/\* HTML \*\/`[^`]*`\)/g, 'toMatchInlineSnapshot(/* HTML */)'))

const vitest = await runVitest({ root, include: [testFile], update: true })
expect(vitest.stderr).toBe('')

// Each call gets its own snapshot, on its own line. Pre-fix, only the last
// (`gamma`) landed — and mangled — while `alpha`/`beta` stayed empty.
const content = fs.readFileSync(testFile, 'utf-8')
expect(content).toContain('toMatchInlineSnapshot(/* HTML */`"alpha"`)')
expect(content).toContain('toMatchInlineSnapshot(/* HTML */`"beta"`)')
expect(content).toContain('toMatchInlineSnapshot(/* HTML */`"gamma"`)')
})
Loading