From 8b39c117650bc94182dfbf24ddbe6f0b0258f2db Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 8 Sep 2023 13:31:52 +0300 Subject: [PATCH] fix(vitest): show error when calling API on files that user has no access to (#4085) --- packages/ui/client/components/views/ViewReport.vue | 2 +- packages/vitest/src/api/setup.ts | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/ui/client/components/views/ViewReport.vue b/packages/ui/client/components/views/ViewReport.vue index a175d392ec7b..769e9378742f 100644 --- a/packages/ui/client/components/views/ViewReport.vue +++ b/packages/ui/client/components/views/ViewReport.vue @@ -35,7 +35,7 @@ function escapeHtml(unsafe: string) { function createHtmlError(filter: Convert, error: ErrorWithDiff) { let htmlError = '' - if (error.message.includes('\x1B')) + if (error.message?.includes('\x1B')) htmlError = `${error.nameStr || error.name}: ${filter.toHtml(escapeHtml(error.message))}` const startStrWithX1B = error.stackStr?.includes('\x1B') diff --git a/packages/vitest/src/api/setup.ts b/packages/vitest/src/api/setup.ts index df830dda504b..2f4546de97d0 100644 --- a/packages/vitest/src/api/setup.ts +++ b/packages/vitest/src/api/setup.ts @@ -80,20 +80,19 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit 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 + throw new Error(`Test file "${id}" was not registered, so it cannot be updated using the API.`) return fs.writeFile(id, content, 'utf-8') }, async saveSnapshotFile(id, content) { if (!ctx.snapshot.resolvedPaths.has(id)) - return + throw new Error(`Snapshot file "${id}" does not exist.`) await fs.mkdir(dirname(id), { recursive: true }) return fs.writeFile(id, content, 'utf-8') }, async removeSnapshotFile(id) { if (!ctx.snapshot.resolvedPaths.has(id) || !existsSync(id)) - return + throw new Error(`Snapshot file "${id}" does not exist.`) return fs.unlink(id) }, snapshotSaved(snapshot) {