Interactivity API
Vitest implements a subset of @testing-library/user-event
APIs using Chrome DevTools Protocol or webdriver APIs instead of faking events which makes the browser behaviour more reliable and consistent with how users interact with a page.
Almost every userEvent
method inherits its provider options. To see all available options in your IDE, add webdriver
or playwright
types to your tsconfig.json
file:
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/playwright"
]
}
}
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/webdriverio"
]
}
}
userEvent.setup
- Type:
() => UserEvent
Creates a new user event instance. This is useful if you need to keep the state of keyboard to press and release buttons correctly.
WARNING
Unlike @testing-library/user-event
, the default userEvent
instance from @vitest/browser/context
is created once, not every time its methods are called! You can see the difference in how it works in this snippet:
import { userEvent as vitestUserEvent } from '@vitest/browser/context'
import { userEvent as originalUserEvent } from '@testing-library/user-event'
await vitestUserEvent.keyboard('{Shift}') // press shift without releasing
await vitestUserEvent.keyboard('{/Shift}') // releases shift
await originalUserEvent.keyboard('{Shift}') // press shift without releasing
await originalUserEvent.keyboard('{/Shift}') // DID NOT release shift because the state is different
This behaviour is more useful because we do not emulate the keyboard, we actually press the Shift, so keeping the original behaviour would cause unexpected issues when typing in the field.
userEvent.click
- Type:
(element: Element, options?: UserEventClickOptions) => Promise<void>
Click on an element. Inherits provider's options. Please refer to your provider's documentation for detailed explanation about how this method works.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('clicks on an element', async () => {
const logo = screen.getByRole('img', { name: /logo/ })
await userEvent.click(logo)
})
References:
userEvent.dblClick
- Type:
(element: Element, options?: UserEventDoubleClickOptions) => Promise<void>
Triggers a double click event on an element.
Please refer to your provider's documentation for detailed explanation about how this method works.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('triggers a double click on an element', async () => {
const logo = screen.getByRole('img', { name: /logo/ })
await userEvent.dblClick(logo)
})
References:
userEvent.tripleClick
- Type:
(element: Element, options?: UserEventTripleClickOptions) => Promise<void>
Triggers a triple click event on an element. Since there is no tripleclick
in browser api, this method will fire three click events in a row, and so you must check click event detail to filter the event: evt.detail === 3
.
Please refer to your provider's documentation for detailed explanation about how this method works.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('triggers a triple click on an element', async () => {
const logo = screen.getByRole('img', { name: /logo/ })
let tripleClickFired = false
logo.addEventListener('click', (evt) => {
if (evt.detail === 3) {
tripleClickFired = true
}
})
await userEvent.tripleClick(logo)
expect(tripleClickFired).toBe(true)
})
References:
- Playwright
locator.click
API: implemented viaclick
withclickCount: 3
. - WebdriverIO
browser.action
API: implemented via actions api withmove
plus threedown + up + pause
events in a row - testing-library
tripleClick
API
userEvent.fill
- Type:
(element: Element, text: string) => Promise<void>
Fill an input/textarea/conteneditable
element with text. This will remove any existing text in the input before typing the new value.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('update input', async () => {
const input = screen.getByRole('input')
await userEvent.fill(input, 'foo') // input.value == foo
await userEvent.fill(input, '{{a[[') // input.value == {{a[[
await userEvent.fill(input, '{Shift}') // input.value == {Shift}
})
TIP
This API is faster than using userEvent.type
or userEvent.keyboard
, but it doesn't support user-event keyboard
syntax (e.g., {Shift}{selectall}
).
We recommend using this API over userEvent.type
in situations when you don't need to enter special characters.
References:
userEvent.keyboard
- Type:
(text: string) => Promise<void>
The userEvent.keyboard
allows you to trigger keyboard strokes. If any input has a focus, it will type characters into that input. Otherwise, it will trigger keyboard events on the currently focused element (document.body
if there are no focused elements).
This API supports user-event keyboard
syntax.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('trigger keystrokes', async () => {
await userEvent.keyboard('foo') // translates to: f, o, o
await userEvent.keyboard('{{a[[') // translates to: {, a, [
await userEvent.keyboard('{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
await userEvent.keyboard('{a>5}') // press a without releasing it and trigger 5 keydown
await userEvent.keyboard('{a>5/}') // press a for 5 keydown and then release it
})
References:
userEvent.tab
- Type:
(options?: UserEventTabOptions) => Promise<void>
Sends a Tab
key event. This is a shorthand for userEvent.keyboard('{tab}')
.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('tab works', async () => {
const [input1, input2] = screen.getAllByRole('input')
expect(input1).toHaveFocus()
await userEvent.tab()
expect(input2).toHaveFocus()
await userEvent.tab({ shift: true })
expect(input1).toHaveFocus()
})
References:
userEvent.type
- Type:
(element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>
WARNING
If you don't rely on special characters (e.g., {shift}
or {selectall}
), it is recommended to use userEvent.fill
instead.
The type
method implements @testing-library/user-event
's type
utility built on top of keyboard
API.
This function allows you to type characters into an input/textarea/conteneditable element. It supports user-event keyboard
syntax.
If you just need to press characters without an input, use userEvent.keyboard
API.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('update input', async () => {
const input = screen.getByRole('input')
await userEvent.type(input, 'foo') // input.value == foo
await userEvent.type(input, '{{a[[') // input.value == foo{a[
await userEvent.type(input, '{Shift}') // input.value == foo{a[
})
References:
userEvent.clear
- Type:
(element: Element) => Promise<void>
This method clears the input element content.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('clears input', async () => {
const input = screen.getByRole('input')
await userEvent.fill(input, 'foo')
expect(input).toHaveValue('foo')
await userEvent.clear(input)
expect(input).toHaveValue('')
})
References:
userEvent.selectOptions
- Type:
(element: Element, values: HTMLElement | HTMLElement[] | string | string[], options?: UserEventSelectOptions) => Promise<void>
The userEvent.selectOptions
allows selecting a value in a <select>
element.
WARNING
If select element doesn't have multiple
attribute, Vitest will select only the first element in the array.
Unlike @testing-library
, Vitest doesn't support listbox at the moment, but we plan to add support for it in the future.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('clears input', async () => {
const select = screen.getByRole('select')
await userEvent.selectOptions(select, 'Option 1')
expect(select).toHaveValue('option-1')
await userEvent.selectOptions(select, 'option-1')
expect(select).toHaveValue('option-1')
await userEvent.selectOptions(select, [
screen.getByRole('option', { name: 'Option 1' }),
screen.getByRole('option', { name: 'Option 2' }),
])
expect(select).toHaveValue(['option-1', 'option-2'])
})
WARNING
webdriverio
provider doesn't support selecting multiple elements because it doesn't provide API to do so.
References:
- Playwright
locator.selectOption
API - WebdriverIO
element.selectByIndex
API - testing-library
selectOptions
API
userEvent.hover
- Type:
(element: Element, options?: UserEventHoverOptions) => Promise<void>
This method moves the cursor position to the selected element. Please refer to your provider's documentation for detailed explanation about how this method works.
WARNING
If you are using webdriverio
provider, the cursor will move to the center of the element by default.
If you are using playwright
provider, the cursor moves to "some" visible point of the element.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('hovers logo element', async () => {
const logo = screen.getByRole('img', { name: /logo/ })
await userEvent.hover(logo)
})
References:
userEvent.unhover
- Type:
(element: Element, options?: UserEventHoverOptions) => Promise<void>
This works the same as userEvent.hover
, but moves the cursor to the document.body
element instead.
WARNING
By default, the cursor position is in the center (in webdriverio
provider) or in "some" visible place (in playwright
provider) of the body element, so if the currently hovered element is already in the same position, this method will have no effect.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
test('unhover logo element', async () => {
const logo = screen.getByRole('img', { name: /logo/ })
await userEvent.unhover(logo)
})
References:
userEvent.dragAndDrop
- Type:
(source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>
Drags the source element on top of the target element. Don't forget that the source
element has to have the draggable
attribute set to true
.
import { userEvent } from '@vitest/browser/context'
import { screen } from '@testing-library/dom'
import '@testing-library/jest-dom' // adds support for "toHaveTextContent"
test('drag and drop works', async () => {
const source = screen.getByRole('img', { name: /logo/ })
const target = screen.getByTestId('logo-target')
await userEvent.dragAndDrop(source, target)
expect(target).toHaveTextContent('Logo is processed')
})
WARNING
This API is not supported by the default preview
provider.
References: