Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshot): toMatchFileSnapshot ensure dir exists #3155

Merged
merged 2 commits into from
Apr 10, 2023
Merged
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
6 changes: 3 additions & 3 deletions packages/browser/src/client/snapshot.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 })
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
await fs.writeFile(filepath, snapshot, 'utf-8')
}

Expand Down
2 changes: 1 addition & 1 deletion packages/snapshot/src/types/environment.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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