From 69ac92cb7d81ee6cff2e9dc7c9b588c77f8dfb13 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 5 May 2025 16:14:10 +0200 Subject: [PATCH] fix(browser): resolve FS commands relative to the project root (#7896) --- docs/guide/browser/commands.md | 2 +- packages/browser/src/node/commands/fs.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/guide/browser/commands.md b/docs/guide/browser/commands.md index 2350a3bde..01478320c 100644 --- a/docs/guide/browser/commands.md +++ b/docs/guide/browser/commands.md @@ -11,7 +11,7 @@ Command is a function that invokes another function on the server and passes dow ### Files Handling -You can use `readFile`, `writeFile` and `removeFile` API to handle files inside your browser tests. All paths are resolved relative to the test file even if they are called in a helper function located in another file. +You can use the `readFile`, `writeFile`, and `removeFile` APIs to handle files in your browser tests. Since Vitest 3.2, all paths are resolved relative to the [project](/guide/workspace) root (which is `process.cwd()`, unless overriden manually). Previously, paths were resolved relative to the test file. By default, Vitest uses `utf-8` encoding but you can override it with options. diff --git a/packages/browser/src/node/commands/fs.ts b/packages/browser/src/node/commands/fs.ts index fba82a384..bba622517 100644 --- a/packages/browser/src/node/commands/fs.ts +++ b/packages/browser/src/node/commands/fs.ts @@ -18,8 +18,8 @@ function assertFileAccess(path: string, project: TestProject) { export const readFile: BrowserCommand< Parameters -> = async ({ project, testPath = process.cwd() }, path, options = {}) => { - const filepath = resolve(dirname(testPath), path) +> = async ({ project }, path, options = {}) => { + const filepath = resolve(project.config.root, path) assertFileAccess(filepath, project) // never return a Buffer if (typeof options === 'object' && !options.encoding) { @@ -30,8 +30,8 @@ export const readFile: BrowserCommand< export const writeFile: BrowserCommand< Parameters -> = async ({ project, testPath = process.cwd() }, path, data, options) => { - const filepath = resolve(dirname(testPath), path) +> = async ({ project }, path, data, options) => { + const filepath = resolve(project.config.root, path) assertFileAccess(filepath, project) const dir = dirname(filepath) if (!fs.existsSync(dir)) { @@ -42,14 +42,14 @@ export const writeFile: BrowserCommand< export const removeFile: BrowserCommand< Parameters -> = async ({ project, testPath = process.cwd() }, path) => { - const filepath = resolve(dirname(testPath), path) +> = async ({ project }, path) => { + const filepath = resolve(project.config.root, path) assertFileAccess(filepath, project) await fsp.rm(filepath) } -export const _fileInfo: BrowserCommand<[path: string, encoding: BufferEncoding]> = async ({ project, testPath = process.cwd() }, path, encoding) => { - const filepath = resolve(dirname(testPath), path) +export const _fileInfo: BrowserCommand<[path: string, encoding: BufferEncoding]> = async ({ project }, path, encoding) => { + const filepath = resolve(project.config.root, path) assertFileAccess(filepath, project) const content = await fsp.readFile(filepath, encoding || 'base64') return { -- 2.51.2