diff --git a/packages/vitest/src/integrations/snapshot/port/utils.ts b/packages/vitest/src/integrations/snapshot/port/utils.ts index f05e0398adbb..a2fc5b2e5a1b 100644 --- a/packages/vitest/src/integrations/snapshot/port/utils.ts +++ b/packages/vitest/src/integrations/snapshot/port/utils.ts @@ -151,10 +151,16 @@ export async function saveSnapshotFile( key => `exports[${printBacktickString(key)}] = ${printBacktickString(normalizeNewlines(snapshotData[key]))};`, ) + const content = `${writeSnapshotVersion()}\n\n${snapshots.join('\n\n')}\n` + const skipWriting = fs.existsSync(snapshotPath) && (await fsp.readFile(snapshotPath, 'utf8')) === content + + if (skipWriting) + return + ensureDirectoryExists(snapshotPath) await fsp.writeFile( snapshotPath, - `${writeSnapshotVersion()}\n\n${snapshots.join('\n\n')}\n`, + content, 'utf-8', ) }