Files
2026-05-11 17:46:13 +02:00

21 lines
666 B
TypeScript

import fs from 'node:fs'
export function readInlineSnapshots(file: string) {
return extractInlineSnaphsots(fs.readFileSync(file, 'utf-8'))
}
export function extractInlineSnaphsots(code: string) {
const matches = Array.from(
code.matchAll(/\.toMatch(\w*)InlineSnapshot\(\s*`[\s\S]*?`\s*\)/g),
)
const snapshots = matches.map((match) => {
const end = match.index! + match[0].length
const start = code.lastIndexOf('expect', match.index)
if (start === -1) {
throw new Error(`Failed to extract inline snapshot: no expect found for match ${match[0]}`)
}
return code.slice(start, end)
})
return `\n${snapshots.join('\n\n')}\n`
}