--- id: api title: API --- `Vue Testing Library` re-exports everything from `DOM Testing Library`. It also exposes these methods: - [`render(Component, options)`](#rendercomponent-options) - [Parameters](#parameters) - [Component](#component) - [Options](#options) - [`render` result](#render-result) - [`...queries`](#queries) - [`container`](#container) - [`baseElement`](#baseelement) - [`debug`](#debugelement) - [`unmount`](#unmount) - [`html`](#html) - [`emitted`](#emitted) - [`rerender`](#rerenderprops) - [`fireEvent`](#fireevent) - [`touch(elem)`](#touchelem) - [`update(elem, value)`](#updateelem-value) - [`cleanup`](#cleanup) --- ## `render(Component, options)` 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. ```js function render(Component, options) { return { ...DOMTestingLibraryQueries, container, baseElement, debug(element), unmount, html, emitted, rerender(props), } } ``` ### Parameters #### Component The valid Vue Component to be tested. #### Options An object containing additional information to be passed to `@vue/test-utils` [mount](https://vue-test-utils.vuejs.org/api/options.html#mounting-options). Additionally, the following options can also be provided: ##### `store` (`Object` | `Store`) The object definition of a [Vuex](https://vuex.vuejs.org/) 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. ##### `routes` (`Array` | `VueRouter`) A set of routes for [Vue Router](https://router.vuejs.org/). If `routes` is provided, the library will import and configure Vue Router. If an instantiated Vue Router is passed, it will be used. ##### `props` (`Object`) It will be merged with `propsData`. ##### `container` (`HTMLElement`) 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`. ```js const table = document.createElement('table') const {container} = render(TableBody, { props, container: document.body.appendChild(table), }) ``` ##### `baseElement` (`HTMLElement`) 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()`. ### `render` result The `render` method returns an object that has a few properties: #### `...queries` The most important feature of `render` is that the queries from [DOM Testing Library](queries/about.mdx) are automatically returned with their first argument bound to the [baseElement](#baseelement), which defaults to `document.body`. See [Queries](queries/about.mdx) for a complete list. ```js const {getByLabelText, queryAllByTestId} = render(Component) ``` #### `container` 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 using > `container` to query for elements! #### `baseElement` 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 into `baseElement`, so you > can use queries to test your portal component without the `baseElement`. #### `debug(element)` This method is a shortcut for `console.log(prettyDOM(element))`. ```jsx import {render} from '@testing-library/vue' const HelloWorldComponent = { template: `