From 311682a888b8ef69a632a40354f7b33e334ac4d5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 10 Apr 2023 10:39:24 +0200 Subject: [PATCH] fix(snapshot): `toMatchFileSnapshot` ensure dir exists (#3155) --- packages/browser/src/client/snapshot.ts | 6 +++--- packages/snapshot/src/env/node.ts | 5 +++-- packages/snapshot/src/types/environment.ts | 2 +- packages/vitest/src/api/setup.ts | 7 +++++-- packages/vitest/src/api/types.ts | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/browser/src/client/snapshot.ts b/packages/browser/src/client/snapshot.ts index d1ca90262163..d37eadf635a3 100644 --- a/packages/browser/src/client/snapshot.ts +++ b/packages/browser/src/client/snapshot.ts @@ -15,7 +15,7 @@ export class BrowserSnapshotEnvironment implements SnapshotEnvironment { } saveSnapshotFile(filepath: string, snapshot: string): Promise { - return rpc().writeFile(filepath, snapshot) + return rpc().writeFile(filepath, snapshot, true) } resolvePath(filepath: string): Promise { @@ -30,7 +30,7 @@ export class BrowserSnapshotEnvironment implements SnapshotEnvironment { return rpc().removeFile(filepath) } - async prepareDirectory(filepath: string): Promise { - await rpc().createDirectory(filepath) + async prepareDirectory(dirPath: string): Promise { + await rpc().createDirectory(dirPath) } } diff --git a/packages/snapshot/src/env/node.ts b/packages/snapshot/src/env/node.ts index 8774dd78a55e..ac1ef2636d6b 100644 --- a/packages/snapshot/src/env/node.ts +++ b/packages/snapshot/src/env/node.ts @@ -25,11 +25,12 @@ export class NodeSnapshotEnvironment implements SnapshotEnvironment { ) } - async prepareDirectory(filepath: string): Promise { - await fs.mkdir(filepath, { recursive: true }) + async prepareDirectory(dirPath: string): Promise { + await fs.mkdir(dirPath, { recursive: true }) } async saveSnapshotFile(filepath: string, snapshot: string): Promise { + await fs.mkdir(dirname(filepath), { recursive: true }) await fs.writeFile(filepath, snapshot, 'utf-8') } diff --git a/packages/snapshot/src/types/environment.ts b/packages/snapshot/src/types/environment.ts index 044f633dfbb3..c2dc09e0714d 100644 --- a/packages/snapshot/src/types/environment.ts +++ b/packages/snapshot/src/types/environment.ts @@ -3,7 +3,7 @@ export interface SnapshotEnvironment { getHeader(): string resolvePath(filepath: string): Promise resolveRawPath(testPath: string, rawPath: string): Promise - prepareDirectory(filepath: string): Promise + prepareDirectory(dirPath: string): Promise saveSnapshotFile(filepath: string, snapshot: string): Promise readSnapshotFile(filepath: string): Promise removeSnapshotFile(filepath: string): Promise diff --git a/packages/vitest/src/api/setup.ts b/packages/vitest/src/api/setup.ts index afff7a4bc439..b3f76efc9c1d 100644 --- a/packages/vitest/src/api/setup.ts +++ b/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' @@ -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) diff --git a/packages/vitest/src/api/types.ts b/packages/vitest/src/api/types.ts index feb11a392707..1178b66152c9 100644 --- a/packages/vitest/src/api/types.ts +++ b/packages/vitest/src/api/types.ts @@ -19,7 +19,7 @@ export interface WebSocketHandlers { getModuleGraph(id: string): Promise getTransformResult(id: string): Promise readFile(id: string): Promise - writeFile(id: string, content: string): Promise + writeFile(id: string, content: string, ensureDir?: boolean): Promise removeFile(id: string): Promise createDirectory(id: string): Promise snapshotSaved(snapshot: SnapshotResult): void