|
| 1 | +--- |
| 2 | +id: jest-matchers |
| 3 | +title: Jest Matchers |
| 4 | +--- |
| 5 | + |
| 6 | +:::note |
| 7 | +Built-in Jest matchers require RNTL v12.4.0 or later. |
| 8 | +::: |
| 9 | + |
| 10 | +This guide describes built-in Jest matchers, we recommend using these matchers as they provide more readable tests, better accessibility support, and a better developer experience. |
| 11 | + |
| 12 | +If you are already using legacy Jest Native matchers we have a [migration guide](migration-jest-native) for moving to the built-in matchers. |
| 13 | + |
| 14 | +import TOCInline from '@theme/TOCInline'; |
| 15 | + |
| 16 | +<TOCInline toc={toc} /> |
| 17 | + |
| 18 | +## Element Existence |
| 19 | + |
| 20 | +### `toBeOnTheScreen()` |
| 21 | + |
| 22 | +```ts |
| 23 | +expect(element).toBeOnTheScreen() |
| 24 | +``` |
| 25 | + |
| 26 | +This allows you to assert whether an element is attached to the element tree or not. If you hold a reference to an element and it gets unmounted during the test it will no longer pass this assertion. |
| 27 | + |
| 28 | +## Element Content |
| 29 | + |
| 30 | +### `toHaveTextContent()` |
| 31 | + |
| 32 | +```ts |
| 33 | +expect(element).toHaveTextContent( |
| 34 | + text: string | RegExp, |
| 35 | + options?: { |
| 36 | + exact?: boolean; |
| 37 | + normalizer?: (text: string) => string; |
| 38 | + }, |
| 39 | +) |
| 40 | +``` |
| 41 | + |
| 42 | +This allows you to assert whether the given element has the given text content or not. It accepts either `string` or `RegExp` matchers, as well as [text match options](Queries.md#text-match-options) of `exact` and `normalizer`. |
| 43 | + |
| 44 | +### `toContainElement()` |
| 45 | + |
| 46 | +```ts |
| 47 | +expect(container).toContainElement( |
| 48 | + element: ReactTestInstance | null, |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +This allows you to assert whether the given container element does contain another host element. |
| 53 | + |
| 54 | +### `toBeEmptyElement()` |
| 55 | + |
| 56 | +```ts |
| 57 | +expect(element).toBeEmptyElement() |
| 58 | +``` |
| 59 | + |
| 60 | +This allows you to assert whether the given element does not have any host child elements or text content. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## Element State |
| 67 | + |
| 68 | +### `toHaveDisplayValue()` |
| 69 | + |
| 70 | +```ts |
| 71 | +expect(element).toHaveDisplayValue( |
| 72 | + value: string | RegExp, |
| 73 | + options?: { |
| 74 | + exact?: boolean; |
| 75 | + normalizer?: (text: string) => string; |
| 76 | + }, |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +This allows you to assert whether the given `TextInput` element has a specified display value. It accepts either `string` or `RegExp` matchers, as well as [text match options](Queries.md#text-match-options) of `exact` and `normalizer`. |
| 81 | + |
| 82 | +### `toHaveAccessibilityValue()` |
| 83 | + |
| 84 | +```ts |
| 85 | +expect(element).toHaveAccessibilityValue( |
| 86 | + value: { |
| 87 | + min?: number; |
| 88 | + max?: number; |
| 89 | + now?: number; |
| 90 | + text?: string | RegExp; |
| 91 | + }, |
| 92 | +) |
| 93 | +``` |
| 94 | + |
| 95 | +This allows you to assert whether the given element has a specified accessible value. |
| 96 | + |
| 97 | +This matcher will assert accessibility value based on `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext` and `accessibilityValue` props. Only defined value entries will be used in the assertion, the element might have additional accessibility value entries and still be matched. |
| 98 | + |
| 99 | +When querying by `text` entry a string or `RegExp` might be used. |
| 100 | + |
| 101 | + |
| 102 | +### `toBeEnabled()` / `toBeDisabled` {#tobeenabled} |
| 103 | + |
| 104 | +```ts |
| 105 | +expect(element).toBeEnabled() |
| 106 | +expect(element).toBeDisabled() |
| 107 | +``` |
| 108 | + |
| 109 | +These allow you to assert whether the given element is enabled or disabled from the user's perspective. It relies on the accessibility disabled state as set by `aria-disabled` or `accessibilityState.disabled` props. It will consider a given element disabled when it or any of its ancestors is disabled. |
| 110 | + |
| 111 | +:::note |
| 112 | +These matchers are the negation of each other, and both are provided to avoid double negations in your assertions. |
| 113 | +::: |
| 114 | + |
| 115 | + |
| 116 | +### `toBeSelected()` |
| 117 | + |
| 118 | +```ts |
| 119 | +expect(element).toBeSelected() |
| 120 | +``` |
| 121 | + |
| 122 | +This allows you to assert whether the given element is selected from the user's perspective. It relies on the accessibility selected state as set by `aria-selected` or `accessibilityState.selected` props. |
| 123 | + |
| 124 | + |
| 125 | +### `toBeChecked()` / `toBePartiallyChecked()` {#tobechecked} |
| 126 | + |
| 127 | +```ts |
| 128 | +expect(element).toBeChecked() |
| 129 | +expect(element).toBePartiallyChecked() |
| 130 | +``` |
| 131 | + |
| 132 | +These allow you to assert whether the given element is checked or partially checked from the user's perspective. It relies on the accessibility checked state as set by `aria-checked` or `accessibilityState.checked` props. |
| 133 | + |
| 134 | +:::note |
| 135 | +* `toBeChecked()` matcher works only on elements with the `checkbox` or `radio` role. |
| 136 | +* `toBePartiallyChecked()` matcher works only on elements with the `checkbox` role. |
| 137 | +::: |
| 138 | + |
| 139 | +### `toBeExpanded()` / `toBeCollapsed()` {#tobeexpanded} |
| 140 | + |
| 141 | +```ts |
| 142 | +expect(element).toBeExpanded() |
| 143 | +expect(element).toBeCollapsed() |
| 144 | +``` |
| 145 | + |
| 146 | +These allows you to assert whether the given element is expanded or collapsed from the user's perspective. It relies on the accessibility disabled state as set by `aria-expanded` or `accessibilityState.expanded` props. |
| 147 | + |
| 148 | +:::note |
| 149 | +These matchers are the negation of each other for expandable elements (elements with explicit `aria-expanded` or `accessibilityState.expanded` props). However, both won't pass for non-expandable elements (ones without explicit `aria-expanded` or `accessibilityState.expanded` props). |
| 150 | +::: |
| 151 | + |
| 152 | + |
| 153 | +### `toBeBusy()` |
| 154 | + |
| 155 | +```ts |
| 156 | +expect(element).toBeBusy() |
| 157 | +``` |
| 158 | + |
| 159 | +This allows you to assert whether the given element is busy from the user's perspective. It relies on the accessibility selected state as set by `aria-busy` or `accessibilityState.busy` props. |
| 160 | + |
| 161 | +## Element Styles |
| 162 | + |
| 163 | +### `toBeVisible()` |
| 164 | + |
| 165 | +```ts |
| 166 | +expect(element).toBeVisible() |
| 167 | +``` |
| 168 | + |
| 169 | +This allows you to assert whether the given element is visible from the user's perspective. |
| 170 | + |
| 171 | +The element is considered invisible when itself or any of its ancestors has `display: none` or `opacity: 0` styles, as well as when it's hidden from accessibility. |
| 172 | + |
| 173 | +### `toHaveStyle()` |
| 174 | + |
| 175 | +```ts |
| 176 | +expect(element).toHaveStyle( |
| 177 | + style: StyleProp<Style>, |
| 178 | +) |
| 179 | +``` |
| 180 | + |
| 181 | +This allows you to assert whether the given element has given styles. |
| 182 | + |
| 183 | +## Other |
| 184 | + |
| 185 | +### `toHaveAccessibleName()` |
| 186 | + |
| 187 | +```ts |
| 188 | +expect(element).toHaveAccessibleName( |
| 189 | + name?: string | RegExp, |
| 190 | + options?: { |
| 191 | + exact?: boolean; |
| 192 | + normalizer?: (text: string) => string; |
| 193 | + }, |
| 194 | +) |
| 195 | +``` |
| 196 | + |
| 197 | +This allows you to assert whether the given element has a specified accessible name. It accepts either `string` or `RegExp` matchers, as well as [text match options](Queries.md#text-match-options) of `exact` and `normalizer`. |
| 198 | + |
| 199 | +The accessible name will be computed based on `aria-labelledby`, `accessibilityLabelledBy`, `aria-label`, and `accessibilityLabel` props, in the absence of these props, the element text content will be used. |
| 200 | + |
| 201 | +When the `name` parameter is `undefined` it will only check if the element has any accessible name. |
| 202 | + |
| 203 | +### `toHaveProp()` |
| 204 | + |
| 205 | +```ts |
| 206 | +expect(element).toHaveProp( |
| 207 | + name: string, |
| 208 | + value?: unknown, |
| 209 | +) |
| 210 | +``` |
| 211 | + |
| 212 | +This allows you to assert whether the given element has a given prop. When the `value` parameter is `undefined` it will only check for existence of the prop, and when `value` is defined it will check if the actual value matches passed value. |
| 213 | + |
| 214 | +:::note |
| 215 | +This matcher should be treated as an escape hatch to be used when all other matchers are not suitable. |
| 216 | +::: |
0 commit comments