Skip to content

Commit 311682a

Browse files
authored
fix(snapshot): toMatchFileSnapshot ensure dir exists (#3155)
1 parent 0ce8364 commit 311682a

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

packages/browser/src/client/snapshot.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class BrowserSnapshotEnvironment implements SnapshotEnvironment {
1515
}
1616

1717
saveSnapshotFile(filepath: string, snapshot: string): Promise<void> {
18-
return rpc().writeFile(filepath, snapshot)
18+
return rpc().writeFile(filepath, snapshot, true)
1919
}
2020

2121
resolvePath(filepath: string): Promise<string> {
@@ -30,7 +30,7 @@ export class BrowserSnapshotEnvironment implements SnapshotEnvironment {
3030
return rpc().removeFile(filepath)
3131
}
3232

33-
async prepareDirectory(filepath: string): Promise<void> {
34-
await rpc().createDirectory(filepath)
33+
async prepareDirectory(dirPath: string): Promise<void> {
34+
await rpc().createDirectory(dirPath)
3535
}
3636
}

packages/snapshot/src/env/node.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ export class NodeSnapshotEnvironment implements SnapshotEnvironment {
2525
)
2626
}
2727

28-
async prepareDirectory(filepath: string): Promise<void> {
29-
await fs.mkdir(filepath, { recursive: true })
28+
async prepareDirectory(dirPath: string): Promise<void> {
29+
await fs.mkdir(dirPath, { recursive: true })
3030
}
3131

3232
async saveSnapshotFile(filepath: string, snapshot: string): Promise<void> {
33+
await fs.mkdir(dirname(filepath), { recursive: true })
3334
await fs.writeFile(filepath, snapshot, 'utf-8')
3435
}
3536

packages/snapshot/src/types/environment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface SnapshotEnvironment {
33
getHeader(): string
44
resolvePath(filepath: string): Promise<string>
55
resolveRawPath(testPath: string, rawPath: string): Promise<string>
6-
prepareDirectory(filepath: string): Promise<void>
6+
prepareDirectory(dirPath: string): Promise<void>
77
saveSnapshotFile(filepath: string, snapshot: string): Promise<void>
88
readSnapshotFile(filepath: string): Promise<string | null>
99
removeSnapshotFile(filepath: string): Promise<void>

packages/vitest/src/api/setup.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { existsSync, promises as fs } from 'node:fs'
22

3+
import { dirname } from 'pathe'
34
import type { BirpcReturn } from 'birpc'
45
import { createBirpc } from 'birpc'
56
import { parse, stringify } from 'flatted'
@@ -81,8 +82,10 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit
8182
snapshotSaved(snapshot) {
8283
ctx.snapshot.add(snapshot)
8384
},
84-
writeFile(id, content) {
85-
return fs.writeFile(id, content, 'utf-8')
85+
async writeFile(id, content, ensureDir) {
86+
if (ensureDir)
87+
await fs.mkdir(dirname(id), { recursive: true })
88+
return await fs.writeFile(id, content, 'utf-8')
8689
},
8790
async rerun(files) {
8891
await ctx.rerunFiles(files)

packages/vitest/src/api/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface WebSocketHandlers {
1919
getModuleGraph(id: string): Promise<ModuleGraphData>
2020
getTransformResult(id: string): Promise<TransformResultWithSource | undefined>
2121
readFile(id: string): Promise<string | null>
22-
writeFile(id: string, content: string): Promise<void>
22+
writeFile(id: string, content: string, ensureDir?: boolean): Promise<void>
2323
removeFile(id: string): Promise<void>
2424
createDirectory(id: string): Promise<string | undefined>
2525
snapshotSaved(snapshot: SnapshotResult): void

0 commit comments

Comments
 (0)