Skip to content

Commit

Permalink
fix(vitest): show error when calling API on files that user has no ac…
Browse files Browse the repository at this point in the history
…cess to
  • Loading branch information
sheremet-va committed Sep 7, 2023
1 parent 8b46ee8 commit 20a23ac
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/vitest/src/api/setup.ts
Expand Up @@ -71,29 +71,28 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit
},
async readSnapshotFile(snapshotPath) {
if (!ctx.snapshot.resolvedPaths.has(snapshotPath) || !existsSync(snapshotPath))
return null
throw new Error(`Snapshot file "${snapshotPath}" does not exist.`)
return fs.readFile(snapshotPath, 'utf-8')
},
async readTestFile(id) {
if (!ctx.state.filesMap.has(id) || !existsSync(id))
return null
throw new Error(`Test file "${id}" was not registered, so it cannot be read using the API.`)
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) {
Expand Down

0 comments on commit 20a23ac

Please sign in to comment.