docs/vue-testing-library/cheatsheet.mdx
A short guide to all the exported functions in Vue Testing Library.
Difference from DOM Testing Library
The queries returned from
renderinVue Testing Libraryare the same asDOM Testing Library. Yet, they have the first argument bound to the document, so instead ofgetByText(node, 'text')you writegetByText('text').
| Return if no match | Return if 1 match | Return if 1+ match | Await? | |
|---|---|---|---|---|
| getBy... | throw | return | throw | No |
| findBy... | throw | return | throw | Yes |
| queryBy... | null | return | throw | No |
| getAllBy... | throw | array | array | No |
| findAllBy... | throw | array | array | Yes |
| queryAllBy... | [] | array | array | No |
| finds by... | DOM example | |
|---|---|---|
| ...ByLabelText | label or aria-label content | <label for="element" /> |
| ...ByPlaceholderText | input placeholder value | <input placeholder="name" /> |
| ...ByText | element text content | <p>Lorem ipsum</p> |
| ...ByDisplayValue | form element current value | Current value of input element |
| ...ByAltText | img alt attribute | `` |
| ...ByTitle | title attribute or svg title tag | <span title="Add" /> or <title /> |
| ...ByRole | ARIA role | <div role="dialog" /> |
| ...ByTestId | data-testid attribute | <div data-testid="some-message" /> |
You can write any combination of Search variants and Search types.
getByLabelText('Username') will search for a <label> element that contains
the string "Username", and will return the associated input. In case of not
finding any, or finding more than one, it will throw an error.
queryAllByRole('nav') will synchronously look for all elements with a
role="nav" attribute, and return an array with the results (or an empty array
if no results were found).
For more information, see Which query should I use?.
findBy and findAllBy queries are async and retry until either a timeout or
if the query returns successfully; they wrap waitForElement.For more information, see DOM Testing Library Async API.
Remember to
awaitor.then()the result of async functions in your tests!
fireEvent(node, event)fireEvent.click(node)fireEvent.input(node, event)For more information, see Events API
Difference from DOM Testing Library
The events returned from
Vue Testing Libraryare all async, so you shouldawaitorthen()the result.VTL also exposes
fireEvent.update(node, value)event to deal withv-model. See the API for more details.
within(getByTestId("global-header")).getByText("hello").configure({testIdAttribute: 'my-test-id'}).For more information, see Querying Within Elements and Config API.
Given the following HTML:
<div>Hello World</div>
All these matchers will find the element:
// Matching a string:
getByText('Hello World') // full string match
getByText('llo Worl', {exact: false}) // substring match
getByText('hello world', {exact: false}) // ignore case
// Matching a regex:
getByText(/World/) // substring match
getByText(/world/i) // substring match, ignore case
getByText(/^hello world$/i) // full string match, ignore case
getByText(/Hello W?oRlD/i) // advanced regex
// Matching with a custom function:
getByText((content, element) => content.startsWith('Hello'))