|
| 1 | +--- |
| 2 | +id: api |
| 3 | +title: API |
| 4 | +--- |
| 5 | + |
| 6 | +`Vue Testing Library` re-exports everything from `DOM Testing Library`. |
| 7 | + |
| 8 | +It also exposes these methods: |
| 9 | + |
| 10 | +- [`render(Component, options, callback)`](#rendercomponent-options-callback) |
| 11 | + - [Parameters](#parameters) |
| 12 | + - [Component](#component) |
| 13 | + - [Options](#options) |
| 14 | + - [Callback Function](#callback-function) |
| 15 | + - [`render` result](#render-result) |
| 16 | + - [`...queries`](#queries) |
| 17 | + - [`container`](#container) |
| 18 | + - [`baseElement`](#baseelement) |
| 19 | + - [`debug()`](#debug) |
| 20 | + - [`unmount()`](#unmount) |
| 21 | + - [`isUnmounted()`](#isunmounted) |
| 22 | + - [`html()`](#html) |
| 23 | + - [`emitted()`](#emitted) |
| 24 | + - [`updateProps(props)`](#updatepropsprops) |
| 25 | +- [`fireEvent`](#fireevent) |
| 26 | + - [`touch(elem)`](#touchelem) |
| 27 | + - [`update(elem, value)`](#updateelem-value) |
| 28 | +- [`cleanup`](#cleanup) |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## `render(Component, options, callback)` |
| 33 | + |
| 34 | +The `render` function is the only way of rendering components in Vue Testing |
| 35 | +Library. |
| 36 | + |
| 37 | +It takes up to 3 parameters and returns an object with some helper methods. |
| 38 | + |
| 39 | +```js |
| 40 | +function render(Component, options, callbackFunction) { |
| 41 | + return { |
| 42 | + ...DOMTestingLibraryQueries, |
| 43 | + container, |
| 44 | + baseElement, |
| 45 | + debug, |
| 46 | + unmount, |
| 47 | + isUnmounted, |
| 48 | + html, |
| 49 | + emitted, |
| 50 | + updateProps, |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Parameters |
| 56 | + |
| 57 | +#### Component |
| 58 | + |
| 59 | +The valid Vue Component to be tested. |
| 60 | + |
| 61 | +#### Options |
| 62 | + |
| 63 | +An object containing additional information to be passed to `@vue/test-utils` |
| 64 | +[mount](https://vue-test-utils.vuejs.org/api/options.html#context). |
| 65 | + |
| 66 | +Additionally, the options object can also include the following three keys: |
| 67 | + |
| 68 | +1. `store` - The object definition of a [Vuex](https://vuex.vuejs.org/) store. |
| 69 | +2. `routes` - A set of routes for [Vue Router](https://router.vuejs.org/). |
| 70 | +3. `props` - It will be merged with `propsData`. |
| 71 | + |
| 72 | +If a `store` object is provided, `Vue Testing Library` will import and configure |
| 73 | +a Vuex store. |
| 74 | + |
| 75 | +Similarly, if `routes` is provided, the library will import and configure Vue |
| 76 | +Router. |
| 77 | + |
| 78 | +#### Callback Function |
| 79 | + |
| 80 | +```js |
| 81 | +function callbackFunction(vueInstance, vuexStore, router) {} |
| 82 | +``` |
| 83 | + |
| 84 | +A callback function that receives the Vue instance as a parameter. This allows |
| 85 | +3rd party plugins to be installed prior to mount. |
| 86 | + |
| 87 | +The function will also receive the Store or the Router object if the associated |
| 88 | +option was passed in during render. |
| 89 | + |
| 90 | +### `render` result |
| 91 | + |
| 92 | +The `render` method returns an object that has a few properties: |
| 93 | + |
| 94 | +#### `...queries` |
| 95 | + |
| 96 | +The most important feature of `render` is that the queries from |
| 97 | +[DOM Testing Library](dom-testing-library/api-queries.md) are automatically |
| 98 | +returned with their first argument bound to the [baseElement](#baseelement), |
| 99 | +which defaults to `document.body`. |
| 100 | + |
| 101 | +See [Queries](dom-testing-library/api-queries.md) for a complete list of |
| 102 | +available methods. |
| 103 | + |
| 104 | +```js |
| 105 | +const { getByLabelText, queryAllByTestId } = render(Component) |
| 106 | +``` |
| 107 | + |
| 108 | +#### `container` |
| 109 | + |
| 110 | +The containing DOM node of your rendered Vue Component. It's a `div`. This is a |
| 111 | +regular DOM node, so you can call `container.querySelector` etc. to inspect the |
| 112 | +children. |
| 113 | + |
| 114 | +> Tip: To get the root element of your rendered element, use |
| 115 | +> `container.firstChild`. |
| 116 | +
|
| 117 | +#### `baseElement` |
| 118 | + |
| 119 | +Returns `document.body`, the DOM node where your Vue component is rendered. |
| 120 | + |
| 121 | +#### `debug()` |
| 122 | + |
| 123 | +This method is a shortcut for `console.log(prettyDOM(baseElement))`. |
| 124 | + |
| 125 | +```jsx |
| 126 | +import { render } from '@testing-library/vue' |
| 127 | + |
| 128 | +const HelloWorldComponent { |
| 129 | + template: `<h1>Hello World</h1>` |
| 130 | +} |
| 131 | + |
| 132 | +const { debug } = render(HelloWorldComponent) |
| 133 | +debug() |
| 134 | +// <div> |
| 135 | +// <h1>Hello World</h1> |
| 136 | +// </div> |
| 137 | + |
| 138 | +// you can also pass an element: debug(getByTestId('messages')) |
| 139 | +``` |
| 140 | + |
| 141 | +This is a simple wrapper around `prettyDOM` which is also exposed and comes from |
| 142 | +[`DOM Testing Library`](https://github.com/testing-library/dom-testing-library/blob/master/README.md#prettydom). |
| 143 | + |
| 144 | +#### `unmount()` |
| 145 | + |
| 146 | +An alias for `@vue/test-utils` |
| 147 | +[destroy](https://vue-test-utils.vuejs.org/api/wrapper/#destroy). |
| 148 | + |
| 149 | +#### `isUnmounted()` |
| 150 | + |
| 151 | +Returns whether if a Vue component has been destroyed. |
| 152 | + |
| 153 | +#### `html()` |
| 154 | + |
| 155 | +An alias for `@vue/test-utils` |
| 156 | +[html](https://vue-test-utils.vuejs.org/api/wrapper/#html). |
| 157 | + |
| 158 | +#### `emitted()` |
| 159 | + |
| 160 | +An alias for `@vue/test-utils` |
| 161 | +[emitted](https://vue-test-utils.vuejs.org/api/wrapper/#emitted). |
| 162 | + |
| 163 | +#### `updateProps(props)` |
| 164 | + |
| 165 | +An alias for `@vue/test-utils` |
| 166 | +[setProps](https://vue-test-utils.vuejs.org/api/wrapper/#setprops). |
| 167 | + |
| 168 | +It returns a Promise through `wait()`, so you can `await updateProps(...)`. |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## `fireEvent` |
| 173 | + |
| 174 | +Vue Testing Library re-exports all DOM Testing Library |
| 175 | +[firing events](https://deploy-preview-132--testing-library.netlify.com/docs/dom-testing-library/api-events). |
| 176 | +However, it alters them so that all events are async. |
| 177 | + |
| 178 | +```js |
| 179 | +await fireEvent.click(getByText('Click me')) |
| 180 | +``` |
| 181 | + |
| 182 | +Additionally, Vue Testing Library exposes two useful methods: |
| 183 | + |
| 184 | +### `touch(elem)` |
| 185 | + |
| 186 | +It triggers both `focus()` and `blur()` events. |
| 187 | + |
| 188 | +```js |
| 189 | +await fireEvent.touch(getByLabelText('username')) |
| 190 | + |
| 191 | +// Same as: |
| 192 | +await fireEvent.focus(getByLabelText('username')) |
| 193 | +await fireEvent.blur(getByLabelText('username')) |
| 194 | +``` |
| 195 | + |
| 196 | +### `update(elem, value)` |
| 197 | + |
| 198 | +Properly handles inputs controlled by `v-model`. It updates the |
| 199 | +input/select/textarea inner value while emitting the appropiate native event. |
| 200 | + |
| 201 | +See a working example of `update` in the |
| 202 | +[v-model example test](/docs/vue-testing-library/examples#example-using-v-model). |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## `cleanup` |
| 207 | + |
| 208 | +Unmounts Vue trees that were mounted with [render](#render). |
| 209 | + |
| 210 | +```jsx |
| 211 | +import { cleanup, render } from '@testing-library/vue' |
| 212 | +import Component from './Component.vue' |
| 213 | + |
| 214 | +afterEach(cleanup) // <-- add this |
| 215 | + |
| 216 | +test('renders into document', () => { |
| 217 | + render(Component) |
| 218 | + // ... |
| 219 | +}) |
| 220 | + |
| 221 | +// ... more tests ... |
| 222 | +``` |
| 223 | + |
| 224 | +Failing to call `cleanup` when you've called `render` could result in a memory |
| 225 | +leak and tests which are not "idempotent" (which can lead to difficult to debug |
| 226 | +errors in your tests). |
| 227 | + |
| 228 | +**If you don't want to add this to _every single test file_** then we recommend |
| 229 | +that you configure your test framework to run a file before your tests which |
| 230 | +does this automatically. See the [setup](./setup) section for guidance on how to |
| 231 | +set up your framework. |
0 commit comments