Skip to content

Commit

Permalink
fix(snapshot): toMatchFileSnapshot ensure dir exists (#3155)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 10, 2023
1 parent 0ce8364 commit 311682a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/browser/src/client/snapshot.ts
Expand Up @@ -15,7 +15,7 @@ export class BrowserSnapshotEnvironment implements SnapshotEnvironment {
}

saveSnapshotFile(filepath: string, snapshot: string): Promise<void> {
return rpc().writeFile(filepath, snapshot)
return rpc().writeFile(filepath, snapshot, true)
}

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

async prepareDirectory(filepath: string): Promise<void> {
await rpc().createDirectory(filepath)
async prepareDirectory(dirPath: string): Promise<void> {
await rpc().createDirectory(dirPath)
}
}
5 changes: 3 additions & 2 deletions packages/snapshot/src/env/node.ts
Expand Up @@ -25,11 +25,12 @@ export class NodeSnapshotEnvironment implements SnapshotEnvironment {
)
}

async prepareDirectory(filepath: string): Promise<void> {
await fs.mkdir(filepath, { recursive: true })
async prepareDirectory(dirPath: string): Promise<void> {
await fs.mkdir(dirPath, { recursive: true })
}

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

Expand Down
2 changes: 1 addition & 1 deletion packages/snapshot/src/types/environment.ts
Expand Up @@ -3,7 +3,7 @@ export interface SnapshotEnvironment {
getHeader(): string
resolvePath(filepath: string): Promise<string>
resolveRawPath(testPath: string, rawPath: string): Promise<string>
prepareDirectory(filepath: string): Promise<void>
prepareDirectory(dirPath: string): Promise<void>
saveSnapshotFile(filepath: string, snapshot: string): Promise<void>
readSnapshotFile(filepath: string): Promise<string | null>
removeSnapshotFile(filepath: string): Promise<void>
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/api/setup.ts
@@ -1,5 +1,6 @@
import { existsSync, promises as fs } from 'node:fs'

import { dirname } from 'pathe'
import type { BirpcReturn } from 'birpc'
import { createBirpc } from 'birpc'
import { parse, stringify } from 'flatted'
Expand Down Expand Up @@ -81,8 +82,10 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit
snapshotSaved(snapshot) {
ctx.snapshot.add(snapshot)
},
writeFile(id, content) {
return fs.writeFile(id, content, 'utf-8')
async writeFile(id, content, ensureDir) {
if (ensureDir)
await fs.mkdir(dirname(id), { recursive: true })
return await fs.writeFile(id, content, 'utf-8')
},
async rerun(files) {
await ctx.rerunFiles(files)
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/api/types.ts
Expand Up @@ -19,7 +19,7 @@ export interface WebSocketHandlers {
getModuleGraph(id: string): Promise<ModuleGraphData>
getTransformResult(id: string): Promise<TransformResultWithSource | undefined>
readFile(id: string): Promise<string | null>
writeFile(id: string, content: string): Promise<void>
writeFile(id: string, content: string, ensureDir?: boolean): Promise<void>
removeFile(id: string): Promise<void>
createDirectory(id: string): Promise<string | undefined>
snapshotSaved(snapshot: SnapshotResult): void
Expand Down

0 comments on commit 311682a

Please sign in to comment.