You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/14.x/docs/api/jest-matchers.mdx
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Jest matchers
2
2
3
-
This guide describes built-in Jest matchers, we recommend using these matchers as they provide readable tests, accessibility support, and a better developer experience.
3
+
This guide covers the built-in Jest matchers. These matchers make your tests easier to read and work better with accessibility features.
4
4
5
5
## Setup
6
6
@@ -14,7 +14,7 @@ There is no need to set up the built-in matchers; they are automatically availab
14
14
expect(element).toBeOnTheScreen();
15
15
```
16
16
17
-
Asserts whether an element is attached to the element tree. If you hold a reference to an element and it gets unmounted during the test, it will no longer pass this assertion.
17
+
Checks if an element is attached to the element tree. If you have a reference to an element and it gets unmounted during the test, this assertion will fail.
Asserts whether the given element has the given text content. It accepts either `string` or `RegExp` matchers, as well as [text match options](/docs/api/queries#text-match-options)of`exact` and `normalizer`.
33
+
Checks if an element has the specified text content. You can pass a `string` or `RegExp`, plus [text match options](/docs/api/queries#text-match-options)like`exact` and `normalizer`.
Asserts whether the given `TextInput` element has a specified display value. It accepts either `string` or `RegExp` matchers, as well as [text match options](/docs/api/queries#text-match-options)of`exact` and `normalizer`.
67
+
Checks if a `TextInput` element has the specified display value. You can pass a `string` or `RegExp`, plus [text match options](/docs/api/queries#text-match-options)like`exact` and `normalizer`.
Asserts whether the given element has a specified accessible value.
82
+
Checks if an element has the specified accessible value.
83
83
84
-
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.
84
+
The matcher checks accessibility values from `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext`, and `accessibilityValue` props. It only checks the values you specify—the element can have other accessibility value entries and still match.
85
85
86
-
When querying by `text` entrya string or `RegExp` might be used.
86
+
For the `text` entry, you can use a string or `RegExp`.
Asserts 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 considers an element disabled when it or any of its ancestors is disabled.
95
+
Checks if an element is enabled or disabled. Uses the accessibility disabled state from `aria-disabled` or `accessibilityState.disabled` props. An element is considered disabled if it or any of its ancestors is disabled.
96
96
97
97
:::note
98
-
These matchers are the negation of each other, and both are provided to avoid double negations in your assertions.
98
+
These matchers are opposites. Both are available so you can avoid double negations like `expect(element).not.toBeDisabled()`.
99
99
:::
100
100
101
101
### `toBeSelected()`
@@ -104,7 +104,7 @@ These matchers are the negation of each other, and both are provided to avoid do
104
104
expect(element).toBeSelected();
105
105
```
106
106
107
-
Asserts 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.
107
+
Checks if an element is selected. Uses the accessibility selected state from`aria-selected` or `accessibilityState.selected` props.
Asserts 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.
116
+
Checks if an element is checked or partially checked. Uses the accessibility checked state from`aria-checked` or `accessibilityState.checked` props.
117
117
118
118
:::note
119
119
120
-
-`toBeChecked()`matcher works only on `Switch` host elements and accessibility elements with `checkbox`, `radio` or `switch` role.
121
-
-`toBePartiallyChecked()`matcher works only on elements with `checkbox` role.
120
+
-`toBeChecked()`only works on `Switch` host elements and elements with `checkbox`, `radio`, or `switch` role.
121
+
-`toBePartiallyChecked()`only works on elements with `checkbox` role.
Asserts whether the given element is expanded or collapsed from the user's perspective. It relies on the accessibility expanded state as set by`aria-expanded` or `accessibilityState.expanded` props.
132
+
Checks if an element is expanded or collapsed. Uses the accessibility expanded state from`aria-expanded` or `accessibilityState.expanded` props.
133
133
134
134
:::note
135
-
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).
135
+
These matchers are opposites for expandable elements (those with explicit `aria-expanded` or `accessibilityState.expanded` props). For non-expandable elements, neither matcher will pass.
136
136
:::
137
137
138
138
### `toBeBusy()`
@@ -141,7 +141,7 @@ These matchers are the negation of each other for expandable elements (elements
141
141
expect(element).toBeBusy();
142
142
```
143
143
144
-
Asserts whether the given element is busy from the user's perspective. It relies on the accessibility busy state as set by`aria-busy` or `accessibilityState.busy` props.
144
+
Checks if an element is busy. Uses the accessibility busy state from`aria-busy` or `accessibilityState.busy` props.
145
145
146
146
## Checking element style
147
147
@@ -151,9 +151,9 @@ Asserts whether the given element is busy from the user's perspective. It relies
151
151
expect(element).toBeVisible();
152
152
```
153
153
154
-
Asserts whether the given element is visible from the user's perspective.
154
+
Checks if an element is visible.
155
155
156
-
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.
156
+
An element is considered invisible if it or any of its ancestors has `display: none` or `opacity: 0` styles, or if it's hidden from accessibility.
157
157
158
158
### `toHaveStyle()`
159
159
@@ -163,7 +163,7 @@ expect(element).toHaveStyle(
163
163
)
164
164
```
165
165
166
-
Asserts whether the given element has given styles.
Asserts whether the given element has a specified accessible name. It accepts either `string` or `RegExp` matchers, as well as [text match options](/docs/api/queries#text-match-options)of`exact` and `normalizer`.
182
+
Checks if an element has the specified accessible name. You can pass a `string` or `RegExp`, plus [text match options](/docs/api/queries#text-match-options)like`exact` and `normalizer`.
183
183
184
-
The accessible name will be computed based on `aria-labelledby`, `accessibilityLabelledBy`, `aria-label`, and `accessibilityLabel` props. For `Image` elements, the `alt` prop will also be considered. In the absence of these props, the element text content will be used.
184
+
The accessible name comes from `aria-labelledby`, `accessibilityLabelledBy`, `aria-label`, and `accessibilityLabel` props. For `Image` elements, the `alt` prop is also used. If none of these are present, the element's text content is used.
185
185
186
-
When the `name` parameter is `undefined` it will only check if the element has any accessible name.
186
+
If you don't pass a `name` parameter (or pass `undefined`), it only checks whether the element has any accessible name.
187
187
188
188
### `toHaveProp()`
189
189
@@ -194,8 +194,8 @@ expect(element).toHaveProp(
194
194
)
195
195
```
196
196
197
-
Asserts whether the given element has a given prop. When the `value`parameter is`undefined`, it only checks for prop existence. When `value` is defined, it checks if the actual value matches the passed value.
197
+
Checks if an element has a prop. If you don't pass a `value`(or pass`undefined`), it only checks if the prop exists. If you pass a `value`, it checks if the prop's value matches.
198
198
199
199
:::note
200
-
This matcher should be treated as an escape hatch to be used when all other matchers are not suitable.
200
+
Use this matcher as a last resort when other matchers don't fit your needs.
0 commit comments