@@ -4,24 +4,13 @@ title: API
4
4
sidebar_label : API
5
5
---
6
6
7
- ## ` @testing-library/svelte `
7
+ ` @testing-library/svelte ` re-exports everything from
8
+ [ ` @testing-library/dom ` ] [ @testing-library/dom ] , as well as:
8
9
9
- This library re-exports everything from DOM Testing Library. Check the
10
- [ ` @testing-library/dom ` API documentation] [ @testing-library/dom ] to see what
11
- goodies you can use.
12
-
13
- :::info
14
-
15
- Unlike other methods which are passed through unmodified,
16
- [ ` fireEvent ` ] ( #fireevent-async ) is an ` async ` method when imported from
17
- ` @testing-library/svelte ` to ensure Svelte applies new changes to the DOM after
18
- the event is fired.
19
-
20
- :::
21
-
22
- ``` js
23
- import {screen , within , fireEvent /* ... */ } from ' @testing-library/svelte'
24
- ```
10
+ - [ ` render ` ] ( #render )
11
+ - [ ` act ` ] ( #act )
12
+ - [ ` cleanup ` ] ( #cleanup )
13
+ - [ ` fireEvent ` (async)] ( #fireevent-async )
25
14
26
15
[ @testing-library/dom ] : ../dom-testing-library/api.mdx
27
16
@@ -94,31 +83,32 @@ Prior to `@testing-library/
[email protected] `, the `baseElement` option was named
94
83
95
84
### Render Results
96
85
97
- | Result | Description |
98
- | ----------------------------- | ----------------------------------------------------- |
99
- | [ ` baseElement ` ] ( #baseelement ) | The base DOM element used for queries. |
100
- | [ ` component ` ] ( #component ) | The created Svelte component. |
101
- | [ ` container ` ] ( #container ) | The DOM element the component is mounted to. |
102
- | [ ` debug ` ] ( #debug ) | Logs the ` baseElement ` using [ prettyDom ] [ pretty-dom ] . |
103
- | [ ` rerender ` ] ( #rerender ) | Update the component's props . |
104
- | [ ` unmount ` ] ( #unmount ) | Unmount and destroy the component. |
105
- | [ ` ...queries ` ] ( #queries ) | [ Query functions] [ queries ] bound to ` baseElement ` . |
86
+ | Result | Description |
87
+ | ----------------------------- | ---------------------------------------------------------- |
88
+ | [ ` baseElement ` ] ( #baseelement ) | The base DOM element used for queries. |
89
+ | [ ` component ` ] ( #component ) | The mounted Svelte component. |
90
+ | [ ` container ` ] ( #container ) | The DOM element the component is mounted to. |
91
+ | [ ` debug ` ] ( #debug ) | Log elements using [ ` prettyDOM ` ] [ pretty-dom ] . |
92
+ | [ ` rerender ` ] ( #rerender ) | Update the component's props. |
93
+ | [ ` unmount ` ] ( #unmount ) | Unmount and destroy the component. |
94
+ | [ ` ...queries ` ] ( #queries ) | [ Query functions] [ query-functions ] bound to ` baseElement ` . |
106
95
107
96
[ pretty-dom ] : ../dom-testing-library/api-debugging.mdx#prettydom
108
- [ queries ] : ../queries/about.mdx
97
+ [ query-functions ] : ../queries/about.mdx
109
98
110
99
#### ` baseElement `
111
100
112
101
_ Added in
` @testing-library/[email protected] ` _
113
102
114
103
The base DOM element that queries are bound to. Corresponds to
115
- ` renderOptions.baseElement ` .
104
+ ` renderOptions.baseElement ` . If you do not use ` componentOptions.target ` nor
105
+ ` renderOptions.baseElement ` , this will be ` document.body ` .
116
106
117
107
#### ` container `
118
108
119
109
The DOM element the component is mounted in. Corresponds to
120
- ` componentOptions.target ` . In general, avoid using ` container ` to query for
121
- elements; use [ testing-library queries] [ queries ] instead.
110
+ ` componentOptions.target ` . In general, avoid using ` container ` directly to query
111
+ for elements; use [ testing-library queries] [ query-functions ] instead.
122
112
123
113
:::tip
124
114
@@ -129,8 +119,8 @@ Use `container.firstChild` to get the first rendered element of your component.
129
119
:::caution
130
120
131
121
Prior to
` @testing-library/[email protected] ` ,
` container ` was set to the base
132
- element, which made the first rendered element of the component
133
- ` container.firstChild.firstChild ` .
122
+ elemen. Use ` container.firstChild.firstChild ` to get the first rendered element
123
+ of the component in earlier versions .
134
124
135
125
:::
136
126
@@ -142,51 +132,48 @@ API][svelte-component-api] for more details.
142
132
:::tip
143
133
144
134
Avoid using ` component ` except to test developer facing API's, like exported
145
- functions. Instead, prefer of interacting with the DOM. Read [ Avoid the Test
146
- User ] [ test-user ] by Kent C. Dodds to understand the difference between the ** end
147
- user ** and ** developer user** .
135
+ functions. Instead, interact with the DOM. Read [ Avoid the Test User ] [ test-user ]
136
+ by Kent C. Dodds to understand the difference between the ** end user ** and
137
+ ** developer user** .
148
138
149
139
:::
150
140
151
141
[ test-user ] : https://kentcdodds.com/blog/avoid-the-test-user
152
142
153
143
#### ` debug `
154
144
155
- Log any passed element, or the ` baseElement ` by default, using
156
- [ prettyDOM] [ pretty-dom ] .
145
+ Log the ` baseElement ` or a given element using [ ` prettyDOM ` ] [ pretty-dom ] .
157
146
158
147
:::tip
159
148
160
149
If your ` baseElement ` is the default ` document.body ` , we recommend using
161
- [ ` screen.debug ` ] [ screen-debug ] rather than the bound ` debug ` ..
162
-
163
- ``` js
164
- import {render , screen } from ' @testing-library/svelte'
165
-
166
- render (MyComponent, {myProp: ' value' })
167
-
168
- screen .debug ()
169
- ```
150
+ [ ` screen.debug ` ] [ screen-debug ] .
170
151
171
152
:::
172
153
173
154
``` js
155
+ import {render , screen } from ' @testing-library/svelte'
156
+
174
157
const {debug } = render (MyComponent, {myProp: ' value' })
175
158
176
159
const button = screen .getByRole (' button' )
177
160
178
- // log the baseElement
161
+ // log `document.body`
162
+ screen .debug ()
163
+
164
+ // log your custom `target` or `baseElement`
179
165
debug ()
180
166
181
167
// log a specific element
168
+ screen .debug (button)
182
169
debug (button)
183
170
```
184
171
185
172
[ screen-debug ] : ../dom-testing-library/api-debugging.mdx#screendebug
186
173
187
174
#### ` rerender `
188
175
189
- Update one or more of the component's props.
176
+ Update one or more of the component's props and wait for Svelte to update .
190
177
191
178
``` js
192
179
const {rerender } = render (MyComponent, {myProp: ' value' })
@@ -196,9 +183,9 @@ await rerender({myProp: 'new value'}))
196
183
197
184
:::caution
198
185
199
- Prior to
` @testing-library/[email protected] ` ,
` rerender ` would
umount the component.
200
- To update props witohut unmounting in earlier versions of
201
- ` @testing-library/svelte ` , use ` component.$set ` and [ ` act ` ] ( #act-async ) :
186
+ Prior to
` @testing-library/[email protected] ` ,
calling ` rerender ` would
destroy and
187
+ remount the component. Use ` component.$set ` and [ ` act ` ] ( #act ) to update props in
188
+ earlier versions :
202
189
203
190
``` js
204
191
const {component } = render (MyComponent, {myProp: ' value' })
@@ -220,54 +207,52 @@ unmount()
220
207
221
208
#### Queries
222
209
223
- [ Query functions] [ queries ] bound to the ` baseElement ` . If you passed [ custom
224
- queries] [ custom-queries ] to ` render ` , those will be available instead of the
225
- default queries.
210
+ [ Query functions] [ query-functions ] bound to the [ ` baseElement ` ] ( #baseelement ) .
211
+ If you passed [ custom queries] [ custom-queries ] to ` render ` , those will be
212
+ available instead of the default queries.
226
213
227
214
:::tip
228
215
229
- If your ` baseElement ` is the default ` document.body ` , we recommend using
230
- [ ` screen ` ] [ screen ] rather than bound queries.
216
+ If your [ ` baseElement ` ] ( #baseelement ) is the default ` document.body ` , we
217
+ recommend using [ ` screen ` ] [ screen ] rather than bound queries.
218
+
219
+ :::
231
220
232
221
``` js
233
222
import {render , screen } from ' @testing-library/svelte'
234
223
235
- render (MyComponent, {myProp: ' value' })
224
+ const { getByRole } = render (MyComponent, {myProp: ' value' })
236
225
226
+ // query `document.body`
237
227
const button = screen .getByRole (' button' )
238
- ```
239
-
240
- :::
241
-
242
- ``` js
243
- const {getByRole } = render (MyComponent, {myProp: ' value' })
244
228
229
+ // query using a custom `target` or `baseElement`
245
230
const button = getByRole (' button' )
246
231
```
247
232
248
233
[ screen ] : ../queries/about.mdx#screen
249
234
250
235
## ` cleanup `
251
236
252
- Unmount all components and remove any elements added to ` document.body ` .
237
+ Destroy all components and remove any elements added to ` document.body ` .
253
238
254
239
:::info
255
240
256
241
` cleanup ` is called automatically if your testing framework adds a global
257
242
` afterEach ` method (e.g. Mocha, Jest, or Jasmine), or if you use
258
243
` import '@testing-library/svelte/vitest' ` in your [ Vitest setup
259
- file] [ vitest-setup ] .
244
+ file] [ vitest-setup ] . Usually, you shouldn't need to call ` cleanup ` yourself.
260
245
261
- If you'd like to disable automatic cleanup in a framework that uses the
262
- ` afterEach ` global method, set ` process.env.STL_SKIP_AUTO_CLEANUP ` .
246
+ If you'd like to disable automatic cleanup in a framework that uses a global
247
+ ` afterEach ` method, set ` process.env.STL_SKIP_AUTO_CLEANUP ` .
263
248
264
249
:::
265
250
266
251
``` js
267
252
import {render , cleanup } from ' @testing-library/svelte'
268
253
269
254
// Default: runs after each test
270
- afterEach (async () => {
255
+ afterEach (() => {
271
256
cleanup ()
272
257
})
273
258
@@ -279,20 +264,20 @@ cleanup()
279
264
280
265
[ vitest-setup ] : ./setup.mdx#vitest
281
266
282
- ## ` act ` (async)
267
+ ## ` act `
283
268
284
269
Ensure all pending updates from Svelte are applied to the DOM. Optionally, you
285
- may pass a function to be called before flushing updates. If the function
286
- returns a ` Promise ` , that promise will be resolved before flushing updates .
270
+ may pass a function to be called before updates are applied . If the function
271
+ returns a ` Promise ` , it will be resolved first .
287
272
288
- Uses Svelte's [ ` tick ` ] [ svelte-tick ] method to flush updates.
273
+ Uses Svelte's [ ` tick ` ] [ svelte-tick ] method to apply updates.
289
274
290
275
``` js
291
276
import {act , render } from ' @testing-library/svelte'
292
277
293
278
const {component } = render (MyComponent)
294
279
295
- act (() => {
280
+ await act (() => {
296
281
component .updateSomething ()
297
282
})
298
283
```
@@ -301,8 +286,8 @@ act(() => {
301
286
302
287
## ` fireEvent ` (async)
303
288
304
- Call DOM Testing Library's [ ` fireEvent ` ] [ fire-event ] , wrapped in
305
- [ ` act ` ] ( #act-async ) to ensure Svelte updates the DOM .
289
+ Fire an event and wait for Svelte to update the DOM. An asynchronous wrapper of
290
+ DOM Testing Library's [ ` fireEvent ` ] [ fire-event ] .
306
291
307
292
:::tip
308
293
0 commit comments