[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]
[](#contributors-)
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Watch on GitHub][github-watch-badge]][github-watch]
[![Star on GitHub][github-star-badge]][github-star]
[![Tweet][twitter-badge]][twitter]
## The problem
You want to use [jest][] to write tests that assert various things about the
state of a DOM. As part of that goal, you want to avoid all the repetitive
patterns that arise in doing so. Checking for an element's attributes, its text
content, its css classes, you name it.
## This solution
The `@testing-library/jest-dom` library provides a set of custom jest matchers
that you can use to extend jest. These will make your tests more declarative,
clear to read and to maintain.
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Custom matchers](#custom-matchers)
- [`toBeDisabled`](#tobedisabled)
- [`toBeEnabled`](#tobeenabled)
- [`toBeEmpty`](#tobeempty)
- [`toBeInTheDocument`](#tobeinthedocument)
- [`toBeInvalid`](#tobeinvalid)
- [`toBeRequired`](#toberequired)
- [`toBeValid`](#tobevalid)
- [`toBeVisible`](#tobevisible)
- [`toContainElement`](#tocontainelement)
- [`toContainHTML`](#tocontainhtml)
- [`toHaveAttribute`](#tohaveattribute)
- [`toHaveClass`](#tohaveclass)
- [`toHaveFocus`](#tohavefocus)
- [`toHaveFormValues`](#tohaveformvalues)
- [`toHaveStyle`](#tohavestyle)
- [`toHaveTextContent`](#tohavetextcontent)
- [`toHaveValue`](#tohavevalue)
- [`toBeChecked`](#tobechecked)
- [Deprecated matchers](#deprecated-matchers)
- [`toBeInTheDOM`](#tobeinthedom)
- [Inspiration](#inspiration)
- [Other Solutions](#other-solutions)
- [Guiding Principles](#guiding-principles)
- [Contributors](#contributors)
- [LICENSE](#license)
## Installation
This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `devDependencies`:
```
npm install --save-dev @testing-library/jest-dom
```
> Note: We also recommend installing the jest-dom eslint plugin which provides auto-fixable lint rules
> that prevent false positive tests and improve test readability by ensuring you are using the right
> matchers in your tests. More details can be found at
> [eslint-plugin-jest-dom](https://github.com/testing-library/eslint-plugin-jest-dom).
## Usage
Import `@testing-library/jest-dom/extend-expect` once (for instance in your
[tests setup file][]) and you're good to go:
[tests setup file]:
https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array
```javascript
import '@testing-library/jest-dom/extend-expect'
```
> Note: If you're using TypeScript, make sure your setup file is a `.ts` and not
> a `.js` to include the necessary types.
Alternatively, you can selectively import only the matchers you intend to use,
and extend jest's `expect` yourself:
```javascript
import {toBeInTheDocument, toHaveClass} from '@testing-library/jest-dom'
expect.extend({toBeInTheDocument, toHaveClass})
```
> Note: when using TypeScript, this way of importing matchers won't provide the
> necessary type definitions. More on this
> [here](https://github.com/testing-library/jest-dom/pull/11#issuecomment-387817459).
## Custom matchers
`@testing-library/jest-dom` can work with any library or framework that returns
DOM elements from queries. The custom matcher examples below demonstrate using
`document.querySelector` and
[DOM Testing Library](https://github.com/testing-library/dom-testing-library)
for querying DOM elements.
### `toBeDisabled`
```typescript
toBeDisabled()
```
This allows you to check whether an element is disabled from the user's
perspective.
It matches if the element is a form control and the `disabled` attribute is
specified on this element or the element is a descendant of a form element with
a `disabled` attribute.
According to the specification, the following elements can be
[actually disabled](https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements):
`button`, `input`, `select`, `textarea`, `optgroup`, `option`, `fieldset`.
#### Examples
```html
link
```
##### Using document.querySelector
```javascript
expect(document.querySelector('[data-testid="button"]')).toBeDisabled()
expect(document.querySelector('[data-testid="input"]')).toBeDisabled()
expect(document.querySelector('a')).not.toBeDisabled()
```
##### Using DOM Testing Library
```javascript
expect(getByTestId(container, 'button')).toBeDisabled()
expect(getByTestId(container, 'input')).toBeDisabled()
expect(getByText(container, 'link')).not.toBeDisabled()
```
### `toBeEnabled`
```typescript
toBeEnabled()
```
This allows you to check whether an element is not disabled from the user's
perspective.
It works like `not.toBeDisabled()`. Use this matcher to avoid double negation in
your tests.
### `toBeEmpty`
```typescript
toBeEmpty()
```
This allows you to assert whether an element has content or not.
#### Examples
```html
```
##### Using document.querySelector
```javascript
expect(document.querySelector('[data-testid="empty"]').toBeEmpty()
expect(document.querySelector('[data-testid="not-empty"]').not.toBeEmpty()
```
##### Using DOM Testing Library
```javascript
expect(queryByTestId(container, 'empty')).toBeEmpty()
expect(queryByTestId(container, 'not-empty')).not.toBeEmpty()
```
### `toBeInTheDocument`
```typescript
toBeInTheDocument()
```
This allows you to assert whether an element is present in the document or not.
#### Examples
```html
Html Element
```
##### Using document.querySelector
```javascript
const htmlElement = document.querySelector('[data-testid="html-element"]')
const svgElement = document.querySelector('[data-testid="svg-element"]')
const nonExistantElement = document.querySelector('does-not-exist')
const detachedElement = document.createElement('div')
expect(htmlElement).toBeInTheDocument()
expect(svgElement).toBeInTheDocument()
expect(nonExistantElement).not.toBeInTheDocument()
expect(detachedElement).not.toBeInTheDocument()
```
##### Using DOM Testing Library
```javascript
expect(
queryByTestId(document.documentElement, 'html-element'),
).toBeInTheDocument()
expect(
queryByTestId(document.documentElement, 'svg-element'),
).toBeInTheDocument()
expect(
queryByTestId(document.documentElement, 'does-not-exist'),
).not.toBeInTheDocument()
```
> Note: This matcher does not find detached elements. The element must be added
> to the document to be found by toBeInTheDocument. If you desire to search in a
> detached element please use: [`toContainElement`](#tocontainelement)
### `toBeInvalid`
```typescript
toBeInvalid()
```
This allows you to check if a form element, or the entire `form`, is currently
invalid.
An `input`, `select`, `textarea`, or `form` element is invalid if it has an
[`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute)
with no value or a value of `"true"`, or if the result of
[`checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation)
is `false`.
#### Examples
```html
```
##### Using document.querySelector
```javascript
expect(queryByTestId('no-aria-invalid')).not.toBeInvalid()
expect(queryByTestId('aria-invalid')).toBeInvalid()
expect(queryByTestId('aria-invalid-value')).toBeInvalid()
expect(queryByTestId('aria-invalid-false')).not.toBeInvalid()
expect(queryByTestId('valid-form')).not.toBeInvalid()
expect(queryByTestId('invalid-form')).toBeInvalid()
```
##### Using DOM Testing Library
```javascript
expect(getByTestId(container, 'no-aria-invalid')).not.toBeInvalid()
expect(getByTestId(container, 'aria-invalid')).toBeInvalid()
expect(getByTestId(container, 'aria-invalid-value')).toBeInvalid()
expect(getByTestId(container, 'aria-invalid-false')).not.toBeInvalid()
expect(getByTestId(container, 'valid-form')).not.toBeInvalid()
expect(getByTestId(container, 'invalid-form')).toBeInvalid()
```
### `toBeRequired`
```typescript
toBeRequired()
```
This allows you to check if an form element is currently required.
An element is required if it is having a `required` or `aria-required="true"`
attribute.
#### Examples
```html
```
##### Using document.querySelector
```javascript
expect(document.querySelector('[data-testid="required-input"]')).toBeRequired()
expect(
document.querySelector('[data-testid="aria-required-input"]'),
).toBeRequired()
expect(
document.querySelector('[data-testid="conflicted-input"]'),
).toBeRequired()
expect(
document.querySelector('[data-testid="aria-not-required-input"]'),
).not.toBeRequired()
expect(
document.querySelector('[data-testid="unsupported-type"]'),
).not.toBeRequired()
expect(document.querySelector('[data-testid="select"]')).toBeRequired()
expect(document.querySelector('[data-testid="textarea"]')).toBeRequired()
expect(
document.querySelector('[data-testid="supported-role"]'),
).not.toBeRequired()
expect(
document.querySelector('[data-testid="supported-role-aria"]'),
).toBeRequired()
```
##### Using DOM Testing Library
```javascript
expect(getByTestId(container, 'required-input')).toBeRequired()
expect(getByTestId(container, 'aria-required-input')).toBeRequired()
expect(getByTestId(container, 'conflicted-input')).toBeRequired()
expect(getByTestId(container, 'aria-not-required-input')).not.toBeRequired()
expect(getByTestId(container, 'optional-input')).not.toBeRequired()
expect(getByTestId(container, 'unsupported-type')).not.toBeRequired()
expect(getByTestId(container, 'select')).toBeRequired()
expect(getByTestId(container, 'textarea')).toBeRequired()
expect(getByTestId(container, 'supported-role')).not.toBeRequired()
expect(getByTestId(container, 'supported-role-aria')).toBeRequired()
```
### `toBeValid`
```typescript
toBeValid()
```
This allows you to check if the value of a form element, or the entire `form`,
is currently valid.
An `input`, `select`, `textarea`, or `form` element is valid if it has no
[`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute)
or an attribute value of `"false"`. The result of
[`checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation)
must also be `true`.
#### Examples
```html
```
##### Using document.querySelector
```javascript
expect(queryByTestId('no-aria-invalid')).toBeValid()
expect(queryByTestId('aria-invalid')).not.toBeValid()
expect(queryByTestId('aria-invalid-value')).not.toBeValid()
expect(queryByTestId('aria-invalid-false')).toBeValid()
expect(queryByTestId('valid-form')).toBeValid()
expect(queryByTestId('invalid-form')).not.toBeValid()
```
##### Using DOM Testing Library
```javascript
expect(getByTestId(container, 'no-aria-invalid')).toBeValid()
expect(getByTestId(container, 'aria-invalid')).not.toBeValid()
expect(getByTestId(container, 'aria-invalid-value')).not.toBeValid()
expect(getByTestId(container, 'aria-invalid-false')).toBeValid()
expect(getByTestId(container, 'valid-form')).toBeValid()
expect(getByTestId(container, 'invalid-form')).not.toBeValid()
```
### `toBeVisible`
```typescript
toBeVisible()
```
This allows you to check if an element is currently visible to the user.
An element is visible if **all** the following conditions are met:
- it does not have its css property `display` set to `none`
- it does not have its css property `visibility` set to either `hidden` or
`collapse`
- it does not have its css property `opacity` set to `0`
- its parent element is also visible (and so on up to the top of the DOM tree)
- it does not have the
[`hidden`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden)
attribute
#### Examples
```html
Zero Opacity Example
Visibility Hidden Example
Display None Example
Hidden Parent Example
Visible Example
Hidden Attribute Example
```
##### Using document.querySelector
```javascript
expect(document.querySelector('[data-testid="zero-opacity"]')).not.toBeVisible()
expect(
document.querySelector('[data-testid="visibility-hidden"]'),
).not.toBeVisible()
expect(document.querySelector('[data-testid="display-none"]')).not.toBeVisible()
expect(
document.querySelector('[data-testid="hidden-parent"]'),
).not.toBeVisible()
expect(document.querySelector('[data-testid="visible"]')).toBeVisible()
expect(
document.querySelector('[data-testid="hidden-attribute"]'),
).not.toBeVisible()
```
##### Using DOM Testing Library
```javascript
expect(getByText(container, 'Zero Opacity Example')).not.toBeVisible()
expect(getByText(container, 'Visibility Hidden Example')).not.toBeVisible()
expect(getByText(container, 'Display None Example')).not.toBeVisible()
expect(getByText(container, 'Hidden Parent Example')).not.toBeVisible()
expect(getByText(container, 'Visible Example')).toBeVisible()
expect(getByText(container, 'Hidden Attribute Example')).not.toBeVisible()
```
### `toContainElement`
```typescript
toContainElement(element: HTMLElement | SVGElement | null)
```
This allows you to assert whether an element contains another element as a
descendant or not.
#### Examples
```html
```
##### Using document.querySelector
```javascript
const ancestor = document.querySelector('[data-testid="ancestor"]')
const descendant = document.querySelector('[data-testid="descendant"]')
const nonExistantElement = document.querySelector(
'[data-testid="does-not-exist"]',
)
expect(ancestor).toContainElement(descendant)
expect(descendant).not.toContainElement(ancestor)
expect(ancestor).not.toContainElement(nonExistantElement)
```
##### Using DOM Testing Library
```javascript
const {queryByTestId} = render(/* Rendered HTML */)
const ancestor = queryByTestId(container, 'ancestor')
const descendant = queryByTestId(container, 'descendant')
const nonExistantElement = queryByTestId(container, 'does-not-exist')
expect(ancestor).toContainElement(descendant)
expect(descendant).not.toContainElement(ancestor)
expect(ancestor).not.toContainElement(nonExistantElement)
```
### `toContainHTML`
```typescript
toContainHTML(htmlText: string)
```
Assert whether a string representing a HTML element is contained in another
element:
#### Examples
```html
```
##### Using document.querySelector
```javascript
expect(document.querySelector('[data-testid="parent"]')).toContainHTML(
'',
)
```
##### Using DOM Testing Library
```javascript
expect(getByTestId(container, 'parent')).toContainHTML(
'',
)
```
> Chances are you probably do not need to use this matcher. We encourage testing
> from the perspective of how the user perceives the app in a browser. That's
> why testing against a specific DOM structure is not advised.
>
> It could be useful in situations where the code being tested renders html that
> was obtained from an external source, and you want to validate that that html
> code was used as intended.
>
> It should not be used to check DOM structure that you control. Please use
> [`toContainElement`](#tocontainelement) instead.
### `toHaveAttribute`
```typescript
toHaveAttribute(attr: string, value?: any)
```
This allows you to check whether the given element has an attribute or not. You
can also optionally check that the attribute has a specific expected value or
partial match using
[expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring)/[expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp)
#### Examples
```html
```
##### Using document.querySelector
```javascript
const button = document.querySelector('[data-testid="ok-button"]')
expect(button).toHaveAttribute('disabled')
expect(button).toHaveAttribute('type', 'submit')
expect(button).not.toHaveAttribute('type', 'button')
```
##### Using DOM Testing Library
```javascript
const button = getByTestId(container, 'ok-button')
expect(button).toHaveAttribute('disabled')
expect(button).toHaveAttribute('type', 'submit')
expect(button).not.toHaveAttribute('type', 'button')
expect(button).toHaveAttribute('type', expect.stringContaining('sub'))
expect(button).toHaveAttribute('type', expect.not.stringContaining('but'))
```
### `toHaveClass`
```typescript
toHaveClass(...classNames: string[])
```
This allows you to check whether the given element has certain classes within
its `class` attribute.
You must provide at least one class, unless you are asserting that an element
does not have any classes.
#### Examples
```html
```
##### Using document.querySelector
```javascript
const deleteButton = document.querySelector('[data-testid="delete-button"]')
const noClasses = document.querySelector('[data-testid="no-classes"]')
expect(deleteButton).toHaveClass('extra')
expect(deleteButton).toHaveClass('btn-danger btn')
expect(deleteButton).toHaveClass('btn-danger', 'btn')
expect(deleteButton).not.toHaveClass('btn-link')
expect(noClasses).not.toHaveClass()
```
##### Using DOM Testing Library
```javascript
const deleteButton = getByTestId(container, 'delete-button')
const noClasses = getByTestId(container, 'no-classes')
expect(deleteButton).toHaveClass('extra')
expect(deleteButton).toHaveClass('btn-danger btn')
expect(deleteButton).toHaveClass('btn-danger', 'btn')
expect(deleteButton).not.toHaveClass('btn-link')
expect(noClasses).not.toHaveClass()
```
### `toHaveFocus`
```typescript
toHaveFocus()
```
This allows you to assert whether an element has focus or not.
#### Examples
```html
```
##### Using document.querySelector
```javascript
const input = document.querySelector(['data-testid="element-to-focus"'])
input.focus()
expect(input).toHaveFocus()
input.blur()
expect(input).not.toHaveFocus()
```
##### Using DOM Testing Library
```javascript
const input = queryByTestId(container, 'element-to-focus')
fireEvent.focus(input)
expect(input).toHaveFocus()
fireEvent.blur(input)
expect(input).not.toHaveFocus()
```
### `toHaveFormValues`
```typescript
toHaveFormValues(expectedValues: {
[name: string]: any
})
```
This allows you to check if a form or fieldset contains form controls for each
given name, and having the specified value.
> It is important to stress that this matcher can only be invoked on a [form][]
> or a [fieldset][] element.
>
> This allows it to take advantage of the [.elements][] property in `form` and
> `fieldset` to reliably fetch all form controls within them.
>
> This also avoids the possibility that users provide a container that contains
> more than one `form`, thereby intermixing form controls that are not related,
> and could even conflict with one another.
This matcher abstracts away the particularities with which a form control value
is obtained depending on the type of form control. For instance, ``
elements have a `value` attribute, but `