Skip to content

Commit b9a312a

Browse files
authored
fix(browser): resolve upload files relative to the project root (#8042)
1 parent c23b0f7 commit b9a312a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

docs/guide/browser/interactivity-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ test('can upload a file', async () => {
469469
// or you can access it directly on the locator
470470
await input.upload(file)
471471
472-
// you can also use file paths relative to the test file
473-
await userEvent.upload(input, '../fixtures/file.png')
472+
// you can also use file paths relative to the root of the project
473+
await userEvent.upload(input, './fixtures/file.png')
474474
})
475475
```
476476

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { UserEventUploadOptions } from '@vitest/browser/context'
22
import type { UserEventCommand } from './utils'
3-
import { dirname, resolve } from 'pathe'
3+
import { resolve } from 'pathe'
44
import { PlaywrightBrowserProvider } from '../providers/playwright'
55
import { WebdriverBrowserProvider } from '../providers/webdriver'
66

@@ -18,13 +18,13 @@ export const upload: UserEventCommand<(element: string, files: Array<string | {
1818
if (!testPath) {
1919
throw new Error(`Cannot upload files outside of a test`)
2020
}
21-
const testDir = dirname(testPath)
21+
const root = context.project.config.root
2222

2323
if (context.provider instanceof PlaywrightBrowserProvider) {
2424
const { iframe } = context
2525
const playwrightFiles = files.map((file) => {
2626
if (typeof file === 'string') {
27-
return resolve(testDir, file)
27+
return resolve(root, file)
2828
}
2929
return {
3030
name: file.name,
@@ -44,7 +44,7 @@ export const upload: UserEventCommand<(element: string, files: Array<string | {
4444
const element = context.browser.$(selector)
4545

4646
for (const file of files) {
47-
const filepath = resolve(testDir, file as string)
47+
const filepath = resolve(root, file as string)
4848
const remoteFilePath = await context.browser.uploadFile(filepath)
4949
await element.addValue(remoteFilePath)
5050
}

test/browser/test/userEvent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ describe('uploading files', async () => {
913913
input.id = 'file'
914914
input.type = 'file'
915915
document.body.appendChild(input)
916-
await userEvent.upload(input, '../src/button.css')
916+
await userEvent.upload(input, './src/button.css')
917917
await expect.poll(() => input.files.length).toBe(1)
918918

919919
const uploadedFile = input.files[0]
@@ -929,7 +929,7 @@ describe('uploading files', async () => {
929929
input.type = 'file'
930930
input.multiple = true
931931
document.body.appendChild(input)
932-
await userEvent.upload(input, ['../src/button.css', '../package.json'])
932+
await userEvent.upload(input, ['./src/button.css', './package.json'])
933933
await expect.poll(() => input.files.length).toBe(2)
934934

935935
const uploadedFile1 = input.files[0]

0 commit comments

Comments
 (0)