Skip to content

Commit 69ac92c

Browse files
authored
fix(browser): resolve FS commands relative to the project root (#7896)
1 parent 6e8d937 commit 69ac92c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

docs/guide/browser/commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Command is a function that invokes another function on the server and passes dow
1111

1212
### Files Handling
1313

14-
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.
14+
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.
1515

1616
By default, Vitest uses `utf-8` encoding but you can override it with options.
1717

packages/browser/src/node/commands/fs.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function assertFileAccess(path: string, project: TestProject) {
1818

1919
export const readFile: BrowserCommand<
2020
Parameters<BrowserCommands['readFile']>
21-
> = async ({ project, testPath = process.cwd() }, path, options = {}) => {
22-
const filepath = resolve(dirname(testPath), path)
21+
> = async ({ project }, path, options = {}) => {
22+
const filepath = resolve(project.config.root, path)
2323
assertFileAccess(filepath, project)
2424
// never return a Buffer
2525
if (typeof options === 'object' && !options.encoding) {
@@ -30,8 +30,8 @@ export const readFile: BrowserCommand<
3030

3131
export const writeFile: BrowserCommand<
3232
Parameters<BrowserCommands['writeFile']>
33-
> = async ({ project, testPath = process.cwd() }, path, data, options) => {
34-
const filepath = resolve(dirname(testPath), path)
33+
> = async ({ project }, path, data, options) => {
34+
const filepath = resolve(project.config.root, path)
3535
assertFileAccess(filepath, project)
3636
const dir = dirname(filepath)
3737
if (!fs.existsSync(dir)) {
@@ -42,14 +42,14 @@ export const writeFile: BrowserCommand<
4242

4343
export const removeFile: BrowserCommand<
4444
Parameters<BrowserCommands['removeFile']>
45-
> = async ({ project, testPath = process.cwd() }, path) => {
46-
const filepath = resolve(dirname(testPath), path)
45+
> = async ({ project }, path) => {
46+
const filepath = resolve(project.config.root, path)
4747
assertFileAccess(filepath, project)
4848
await fsp.rm(filepath)
4949
}
5050

51-
export const _fileInfo: BrowserCommand<[path: string, encoding: BufferEncoding]> = async ({ project, testPath = process.cwd() }, path, encoding) => {
52-
const filepath = resolve(dirname(testPath), path)
51+
export const _fileInfo: BrowserCommand<[path: string, encoding: BufferEncoding]> = async ({ project }, path, encoding) => {
52+
const filepath = resolve(project.config.root, path)
5353
assertFileAccess(filepath, project)
5454
const content = await fsp.readFile(filepath, encoding || 'base64')
5555
return {

0 commit comments

Comments
 (0)