Skip to content

Commit 2a93f29

Browse files
authored
docs: Better document toBeInTheDOM deprecation
Closes #41
1 parent 58ca030 commit 2a93f29

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

README.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,35 @@ expect(getByText(container, 'LINK')).not.toBeDisabled()
380380

381381
### `toBeInTheDOM`
382382

383-
> Note: The differences between `toBeInTheDOM` and `toBeInTheDocument` are significant. Replacing all uses of `toBeInTheDOM` with `toBeInTheDocument` will likely cause unintended consequences in your tests. Please make sure when replacing `toBeInTheDOM` to read through the replacements below to see which use case works better for your needs.
383+
```typescript
384+
toBeInTheDOM()
385+
```
386+
387+
This allows you to check whether a value is a DOM element, or not.
388+
389+
Contrary to what its name implies, this matcher only checks that you passed to
390+
it a valid DOM element. It does not have a clear definition of that "the DOM"
391+
is. Therefore, it does not check wether that element is contained anywhere.
384392

385-
> Please use [`toContainElement`](#tocontainelement) for searching a specific container.
393+
This is the main reason why this matcher is deprecated, and will be removed in
394+
the next major release. You can follow the discussion around this decision in
395+
more detail [here](https://github.com/gnapse/jest-dom/issues/34).
396+
397+
As an alternative, you can use [`toBeInTheDocument`](#tobeinthedocument)
398+
or [`toContainElement`](#tocontainelement). Or if you just want to check if a
399+
value is indeed an `HTMLElement` you can always use some of
400+
[jest's built-in matchers](https://jestjs.io/docs/en/expect#tobeinstanceofclass):
401+
402+
```js
403+
expect(document.querySelector('.ok-button')).toBeInstanceOf(HTMLElement)
404+
expect(document.querySelector('.cancel-button')).toBeThruthy();
405+
```
386406

387-
> Please use [`toBeInTheDocument`](#tobeinthedocument) for searching the entire document.
407+
> Note: The differences between `toBeInTheDOM` and `toBeInTheDocument` are
408+
> significant. Replacing all uses of `toBeInTheDOM` with `toBeInTheDocument`
409+
> will likely cause unintended consequences in your tests. Please make sure when
410+
> replacing `toBeInTheDOM` to read through the documentation of the proposed
411+
> alternatives to see which use case works better for your needs.
388412
389413
## Inspiration
390414

0 commit comments

Comments
 (0)