@@ -63,26 +63,58 @@ The valid Vue Component to be tested.
63
63
An object containing additional information to be passed to ` @vue/test-utils `
64
64
[ mount] ( https://vue-test-utils.vuejs.org/api/options.html#mounting-options ) .
65
65
66
- Additionally, the options object can also include the following three keys :
66
+ Additionally, the following options can also be provided :
67
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 ` .
68
+ ##### ` store ` (` Object ` )
71
69
72
- If a ` store ` object is provided, ` Vue Testing Library ` will import and configure
73
- a Vuex store.
70
+ The object definition of a [ Vuex] ( https://vuex.vuejs.org/ ) store. If a ` store `
71
+ object is provided, ` Vue Testing Library ` will import and configure a Vuex
72
+ store.
74
73
75
- Similarly, if ` routes ` is provided, the library will import and configure Vue
76
- Router.
74
+ ##### ` routes ` (` Array ` )
75
+
76
+ A set of routes for [ Vue Router] ( https://router.vuejs.org/ ) . If ` routes ` is
77
+ provided, the library will import and configure Vue Router.
78
+
79
+ ##### ` props ` (` Object ` )
80
+
81
+ It will be merged with ` propsData ` .
82
+
83
+ ##### ` container ` (` HTMLElement ` )
84
+
85
+ By default, ` Vue Testing Library ` will create a ` div ` and append it to the
86
+ ` baseElement ` . This is where your component will be rendered. If you provide
87
+ your own ` HTMLElement ` container via this option, it will not be appended to the
88
+ ` baseElement ` automatically.
89
+
90
+ For example: If you are unit testing a ` tablebody ` element, it cannot be a child
91
+ of a ` div ` . In this case, you can specify a ` table ` as the render ` container ` .
92
+
93
+ ``` js
94
+ const table = document .createElement (' table' )
95
+
96
+ const { container } = render (TableBody, {
97
+ props,
98
+ container: document .body .appendChild (table),
99
+ })
100
+ ```
101
+
102
+ ##### ` baseElement ` (` HTMLElement ` )
103
+
104
+ If the ` container ` is specified, then this defaults to that, otherwise this
105
+ defaults to ` document.body ` . ` baseElement ` is used as the base element for the
106
+ queries as well as what is printed when you use ` debug() ` .
77
107
78
108
#### Callback Function
79
109
80
110
``` js
81
111
function callbackFunction (vueInstance , vuexStore , router ) {}
82
112
```
83
113
84
- A callback function that receives the Vue instance as a parameter. Its return value is merged with [ options] ( #options ) , allowing
85
- 3rd party plugins to be installed prior to mount. To see how this works, see the example using [ ` vue-i18n ` ] ( https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vue-i18n.js ) .
114
+ A callback function that receives the Vue instance as a parameter. Its return
115
+ value is merged with [ options] ( #options ) , allowing 3rd party plugins to be
116
+ installed prior to mount. To see how this works, see the example using
117
+ [ ` vue-i18n ` ] ( https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vue-i18n.js ) .
86
118
87
119
The function will also receive the Store or the Router object if the associated
88
120
option was passed in during render.
@@ -94,9 +126,9 @@ The `render` method returns an object that has a few properties:
94
126
#### ` ...queries `
95
127
96
128
The most important feature of ` render ` is that the queries from
97
- [ DOM Testing Library] ( queries/about.mdx ) are automatically
98
- returned with their first argument bound to the [ baseElement] ( #baseelement ) ,
99
- which defaults to ` document.body ` .
129
+ [ DOM Testing Library] ( queries/about.mdx ) are automatically returned with their
130
+ first argument bound to the [ baseElement] ( #baseelement ) , which defaults to
131
+ ` document.body ` .
100
132
101
133
See [ Queries] ( queries/about.mdx ) for a complete list.
102
134
@@ -106,30 +138,30 @@ const { getByLabelText, queryAllByTestId } = render(Component)
106
138
107
139
#### ` container `
108
140
109
- By default, ` Vue Testing Library ` will create a ` div ` and append it to the
110
- ` baseElement ` . This is where your component will be rendered. If you provide
111
- your own HTMLElement container via this option, it will not be appended to the
112
- ` baseElement ` automatically.
113
-
114
- ``` js
115
- const table = document .createElement (' table' )
116
-
117
- const { container } = render (TableBody, {
118
- container: document .body .appendChild (table),
119
- })
120
- ```
141
+ The containing DOM node of your rendered Vue Component. By default it's a ` div ` .
142
+ This is a regular DOM node, so you can call ` container.querySelector ` etc. to
143
+ inspect the children.
121
144
122
145
> Tip: To get the root element of your rendered element, use
123
146
> ` container.firstChild ` .
124
147
148
+ > 🚨 If you find yourself using ` container ` to query for rendered elements then
149
+ > you should reconsider! The other queries are designed to be more resilient to
150
+ > changes that will be made to the component you're testing. Avoid using
151
+ > ` container ` to query for elements!
152
+
125
153
#### ` baseElement `
126
154
127
- ` baseElement ` is used as the base element for the queries as well as what is
128
- printed when you use ` debug() ` .
155
+ The containing DOM node where your Vue Component is rendered in the ` container ` .
156
+ If you don't specify the ` baseElement ` in the options of ` render ` , it will
157
+ default to ` document.body ` .
129
158
130
- It matches ` container ` if no custom ` baseElement ` is provided. If neither
131
- ` baseElement ` or ` container ` options are provided, ` baseElement ` defaults to
132
- ` document.body ` .
159
+ This is useful when the component you want to test renders something outside the
160
+ container ` div ` , e.g. when you want to snapshot test your portal component which
161
+ renders its HTML directly in the body.
162
+
163
+ > Note: the queries returned by the ` render ` looks into ` baseElement ` , so you
164
+ > can use queries to test your portal component without the ` baseElement ` .
133
165
134
166
#### ` debug(element) `
135
167
0 commit comments