diff --git a/packages/ui/client/components/views/ViewEditor.vue b/packages/ui/client/components/views/ViewEditor.vue index f82f0c306dae..957fde0f2161 100644 --- a/packages/ui/client/components/views/ViewEditor.vue +++ b/packages/ui/client/components/views/ViewEditor.vue @@ -116,7 +116,7 @@ watch([cm, failed], ([cmValue]) => { async function onSave(content: string) { hasBeenEdited.value = true - await client.rpc.writeFile(props.file!.filepath, content) + await client.rpc.saveTestFile(props.file!.filepath, content) serverCode.value = content draft.value = false } diff --git a/packages/vitest/src/api/setup.ts b/packages/vitest/src/api/setup.ts index 3740e092c74e..df830dda504b 100644 --- a/packages/vitest/src/api/setup.ts +++ b/packages/vitest/src/api/setup.ts @@ -79,6 +79,12 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit return null return fs.readFile(id, 'utf-8') }, + async saveTestFile(id, content) { + // can save only already existing test file + if (!ctx.state.filesMap.has(id) || !existsSync(id)) + return + return fs.writeFile(id, content, 'utf-8') + }, async saveSnapshotFile(id, content) { if (!ctx.snapshot.resolvedPaths.has(id)) return diff --git a/packages/vitest/src/api/types.ts b/packages/vitest/src/api/types.ts index 27efe1996aa8..cf7e7d1a744e 100644 --- a/packages/vitest/src/api/types.ts +++ b/packages/vitest/src/api/types.ts @@ -23,6 +23,7 @@ export interface WebSocketHandlers { getTransformResult(id: string): Promise readSnapshotFile(id: string): Promise readTestFile(id: string): Promise + saveTestFile(id: string, content: string): Promise saveSnapshotFile(id: string, content: string): Promise removeSnapshotFile(id: string): Promise snapshotSaved(snapshot: SnapshotResult): void