Skip to content

Commit ebb4a6b

Browse files
eps1lonkentcdodds
andauthored
docs(dom): Document breaking changes of v8 (#866)
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
1 parent 70b61a5 commit ebb4a6b

File tree

2 files changed

+12
-166
lines changed

2 files changed

+12
-166
lines changed

docs/dom-testing-library/api-async.mdx

Lines changed: 3 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ await waitFor(() => expect(mockAPI).toHaveBeenCalledTimes(1))
5555
// ...
5656
```
5757

58-
`waitFor` may run the callback a variable number of times.
58+
`waitFor` may run the callback a number of times until the timeout is reached.
59+
Note that the number of calls is constrained by the `timeout` and `interval` options.
5960

6061
This can be useful if you have a unit test that mocks API calls and you need to
6162
wait for your mock promises to all resolve.
@@ -71,8 +72,7 @@ wait for are descendants of `container`.
7172
The default `interval` is `50ms`. However it will run your callback immediately
7273
before starting the intervals.
7374

74-
The default `timeout` is `1000ms` which will keep you under
75-
[Jest's default timeout of `5000ms`](https://jestjs.io/docs/en/jest-object.html#jestsettimeouttimeout).
75+
The default `timeout` is `1000ms`.
7676

7777
The default `onTimeout` takes the error and appends the `container`'s printed
7878
state to the error message which should hopefully make it easier to track down
@@ -141,164 +141,3 @@ waitForElementToBeRemoved(() => getByText(/not here/i)).catch((err) =>
141141
```
142142

143143
The options object is forwarded to `waitFor`.
144-
145-
## Deprecated Methods
146-
147-
`wait`, `waitForDomChange`, and `waitForElement` have been combined into the
148-
`waitFor` method.
149-
150-
<details>
151-
152-
<br />
153-
154-
<summary>Deprecated Methods</summary>
155-
156-
### `wait`
157-
158-
> (DEPRECATED, use `waitFor` instead)
159-
160-
```typescript
161-
function wait<T>(
162-
callback: () => void,
163-
options?: {
164-
container?: HTMLElement
165-
timeout?: number
166-
interval?: number
167-
mutationObserverOptions?: MutationObserverInit
168-
}
169-
): Promise<T>
170-
```
171-
172-
Previously, wait was a wrapper around wait-for-expect and used polling instead
173-
of a MutationObserver to look for changes. It is now an alias to waitFor and
174-
will be removed in a future release.
175-
176-
Unlike wait, the callback parameter is mandatory in waitFor. Although you can
177-
migrate an existing `wait()` call to `waitFor( () => {} )`, it is considered bad
178-
practice to use an empty callback because it will make the tests more fragile.
179-
180-
### `waitForDomChange`
181-
182-
> (DEPRECATED, use `waitFor` instead)
183-
184-
```typescript
185-
function waitForDomChange<T>(options?: {
186-
container?: HTMLElement
187-
timeout?: number
188-
mutationObserverOptions?: MutationObserverInit
189-
}): Promise<T>
190-
```
191-
192-
When in need to wait for the DOM to change you can use `waitForDomChange`. The
193-
`waitForDomChange` function is a small wrapper around the
194-
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
195-
196-
Here is an example where the promise will be resolved because the container is
197-
changed:
198-
199-
```javascript
200-
const container = document.createElement('div')
201-
waitForDomChange({ container })
202-
.then(() => console.log('DOM changed!'))
203-
.catch((err) => console.log(`Error you need to deal with: ${err}`))
204-
container.append(document.createElement('p'))
205-
// if 👆 was the only code affecting the container and it was not run,
206-
// waitForDomChange would throw an error
207-
```
208-
209-
The promise will resolve with a
210-
[`mutationsList`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver)
211-
which you can use to determine what kind of a change (or changes) affected the
212-
container
213-
214-
```javascript
215-
const container = document.createElement('div')
216-
container.setAttribute('data-cool', 'true')
217-
waitForDomChange({ container }).then((mutationsList) => {
218-
const mutation = mutationsList[0]
219-
console.log(
220-
`was cool: ${mutation.oldValue}\ncurrently cool: ${mutation.target.dataset.cool}`
221-
)
222-
})
223-
container.setAttribute('data-cool', 'false')
224-
/*
225-
logs:
226-
was cool: true
227-
currently cool: false
228-
*/
229-
```
230-
231-
The default `container` is the global `document`. Make sure the elements you
232-
wait for are descendants of `container`.
233-
234-
The default `timeout` is `1000ms` which will keep you under
235-
[Jest's default timeout of `5000ms`](https://jestjs.io/docs/en/jest-object.html#jestsettimeouttimeout).
236-
237-
The default `mutationObserverOptions` is
238-
`{subtree: true, childList: true, attributes: true, characterData: true}` which
239-
will detect additions and removals of child elements (including text nodes) in
240-
the `container` and any of its descendants. It will also detect attribute
241-
changes.
242-
243-
### `waitForElement`
244-
245-
> (DEPRECATED, use `find*` queries or `waitFor`)
246-
247-
```typescript
248-
function waitForElement<T>(
249-
callback: () => T,
250-
options?: {
251-
container?: HTMLElement
252-
timeout?: number
253-
mutationObserverOptions?: MutationObserverInit
254-
}
255-
): Promise<T>
256-
```
257-
258-
When in need to wait for DOM elements to appear, disappear, or change you can
259-
use `waitForElement`. The `waitForElement` function is a small wrapper around
260-
the
261-
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
262-
263-
Here's a simple example:
264-
265-
```javascript
266-
// ...
267-
// Wait until the callback does not throw an error and returns a truthy value. In this case, that means
268-
// it'll wait until we can get a form control with a label that matches "username".
269-
// Previously, the difference from `wait` is that rather than running your callback on
270-
// an interval, it's run as soon as there are DOM changes in the container
271-
// and returns the value returned by the callback.
272-
const usernameElement = await waitForElement(
273-
() => getByLabelText(container, 'username'),
274-
{ container }
275-
)
276-
usernameElement.value = 'chucknorris'
277-
// ...
278-
```
279-
280-
You can also wait for multiple elements at once:
281-
282-
```javascript
283-
const [usernameElement, passwordElement] = await waitForElement(
284-
() => [
285-
getByLabelText(container, 'username'),
286-
getByLabelText(container, 'password'),
287-
],
288-
{ container }
289-
)
290-
```
291-
292-
The default `container` is the global `document`. Make sure the elements you
293-
wait for will be attached to it, or set a different `container`.
294-
295-
The default `timeout` is `4500ms` which will keep you under
296-
[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).
297-
298-
The default `mutationObserverOptions` is
299-
`{subtree: true, childList: true, attributes: true, characterData: true}` which
300-
will detect additions and removals of child elements (including text nodes) in
301-
the `container` and any of its descendants. It will also detect attribute
302-
changes.
303-
304-
</details>

docs/dom-testing-library/api-debugging.mdx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ tree of a node. This can be helpful for instance when debugging tests.
5050
It is defined as:
5151

5252
```typescript
53+
interface Options extends prettyFormat.OptionsReceived {
54+
filterNode?: (node: Node) => boolean;
55+
}
56+
5357
function prettyDOM(
5458
node: HTMLElement,
5559
maxLength?: number,
@@ -59,10 +63,13 @@ function prettyDOM(
5963

6064
It receives the root node to print out, an optional extra parameter to limit the
6165
size of the resulting string, for cases when it becomes too large. It has a last
62-
parameter which allows you to configure your formatting as defined in the
63-
[options](https://github.com/facebook/jest/tree/master/packages/pretty-format#usage-with-options)
66+
parameter which allows you to configure your formatting.
67+
In addition to the options listed you can also pass the [options](https://github.com/facebook/jest/tree/master/packages/pretty-format#usage-with-options)
6468
of `pretty-format`.
6569

70+
By default, `<style />`, `<script />` and comment nodes are ignored.
71+
You can configure this behavior by passing a custom `filterNode` function that should return `true` for every node that you wish to include in the output.
72+
6673
This function is usually used alongside `console.log` to temporarily print out
6774
DOM trees during tests for debugging purposes:
6875

0 commit comments

Comments
 (0)