id | title |
---|---|
api | API |
Vue Testing Library
re-exports everything from DOM Testing Library
.
It also exposes these methods:
The render
function is the only way of rendering components in Vue Testing Library.
It takes up to 2 parameters and returns an object with some helper methods.
functionrender(Component,options){return{ ...DOMTestingLibraryQueries, container, baseElement,debug(element), unmount, html, emitted,rerender(props),}}
The valid Vue Component to be tested.
An object containing additional information to be passed to @vue/test-utils
mount.
Additionally, the following options can also be provided:
The object definition of a Vuex store. If a store
object is provided, Vue Testing Library
will import and configure a Vuex store. If an instantiated Vuex store is passed, it will be used.
A set of routes for Vue Router. If routes
is provided, the library will import and configure Vue Router. If an instantiated Vue Router is passed, it will be used.
It will be merged with propsData
.
By default, Vue Testing Library
will create a div
and append it to the baseElement
. This is where your component will be rendered. If you provide your own HTMLElement
container via this option, it will not be appended to the baseElement
automatically.
For example: If you are unit testing a tablebody
element, it cannot be a child of a div
. In this case, you can specify a table
as the render container
.
consttable=document.createElement('table')const{container}=render(TableBody,{ props,container: document.body.appendChild(table),})
If the container
is specified, then this defaults to that, otherwise this defaults to document.body
. baseElement
is used as the base element for the queries as well as what is printed when you use debug()
.
The render
method returns an object that has a few properties:
The most important feature of render
is that the queries from DOM Testing Library are automatically returned with their first argument bound to the baseElement, which defaults to document.body
.
See Queries for a complete list.
const{getByLabelText, queryAllByTestId}=render(Component)
The containing DOM node of your rendered Vue Component. By default it's a div
. This is a regular DOM node, so you can call container.querySelector
etc. to inspect the children.
Tip: To get the root element of your rendered element, use
container.firstChild
.
🚨 If you find yourself using
container
to query for rendered elements then you should reconsider! The other queries are designed to be more resilient to changes that will be made to the component you're testing. Avoid usingcontainer
to query for elements!
The containing DOM node where your Vue Component is rendered in the container
. If you don't specify the baseElement
in the options of render
, it will default to document.body
.
This is useful when the component you want to test renders something outside the container div
, e.g. when you want to snapshot test your portal component which renders its HTML directly in the body.
Note: the queries returned by the
render
looks intobaseElement
, so you can use queries to test your portal component without thebaseElement
.
This method is a shortcut for console.log(prettyDOM(element))
.
import{render}from'@testing-library/vue'constHelloWorldComponent={template: `<h1>Hello World</h1>`,}const{debug}=render(HelloWorldComponent)debug()// <div>// <h1>Hello World</h1>// </div>
This is a simple wrapper around prettyDOM
which is also exposed and comes from DOM Testing Library
.
An alias for @vue/test-utils
destroy.
An alias for @vue/test-utils
html.
An alias for @vue/test-utils
emitted.
An alias for @vue/test-utils
setProps.
It returns a Promise through so you can await rerender(...)
.
Because Vue applies DOM updates asynchronously during re-renders, the fireEvent tools are re-exported as async
functions. To ensure that the DOM is properly updated in response to an event in a test, it's recommended to always await
fireEvent
.
awaitfireEvent.click(getByText('Click me'))
Additionally, Vue Testing Library exposes two useful methods:
It triggers both focus()
and blur()
events.
awaitfireEvent.touch(getByLabelText('username'))// Same as:awaitfireEvent.focus(getByLabelText('username'))awaitfireEvent.blur(getByLabelText('username'))
Properly handles inputs controlled by v-model
. It updates the input/select/textarea inner value while emitting the appropriate native event.
See a working example of update
in the v-model example test.
Unmounts Vue trees that were mounted with render.
This is called automatically if your testing framework (such as mocha, Jest or Jasmine) injects a global
afterEach()
function into the testing environment. If not, you will need to callcleanup()
after each test.
Failing to call cleanup
when you've called render
could result in a memory leak and tests which are not idempotent (which can lead to difficult to debug errors in your tests).