Skip to content

Commit b4bca76

Browse files
committed
jest matchers
1 parent 2e338c9 commit b4bca76

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

website/docs/14.x/docs/api/jest-matchers.mdx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Jest matchers
22

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.
44

55
## Setup
66

@@ -14,7 +14,7 @@ There is no need to set up the built-in matchers; they are automatically availab
1414
expect(element).toBeOnTheScreen();
1515
```
1616

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.
1818

1919
## Element Content
2020

@@ -30,7 +30,7 @@ expect(element).toHaveTextContent(
3030
)
3131
```
3232

33-
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`.
3434

3535
### `toContainElement()`
3636

@@ -40,15 +40,15 @@ expect(container).toContainElement(
4040
)
4141
```
4242

43-
Asserts whether the given container element contains another host element.
43+
Checks if a container element contains another host element.
4444

4545
### `toBeEmptyElement()`
4646

4747
```ts
4848
expect(element).toBeEmptyElement();
4949
```
5050

51-
Asserts whether the given element has no host child elements or text content.
51+
Checks if an element has no host child elements or text content.
5252

5353
## Checking element state
5454

@@ -64,7 +64,7 @@ expect(element).toHaveDisplayValue(
6464
)
6565
```
6666

67-
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`.
6868

6969
### `toHaveAccessibilityValue()`
7070

@@ -79,11 +79,11 @@ expect(element).toHaveAccessibilityValue(
7979
)
8080
```
8181

82-
Asserts whether the given element has a specified accessible value.
82+
Checks if an element has the specified accessible value.
8383

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.
8585

86-
When querying by `text` entry a string or `RegExp` might be used.
86+
For the `text` entry, you can use a string or `RegExp`.
8787

8888
### `toBeEnabled()` / `toBeDisabled` {#tobeenabled}
8989

@@ -92,10 +92,10 @@ expect(element).toBeEnabled();
9292
expect(element).toBeDisabled();
9393
```
9494

95-
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.
9696

9797
:::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()`.
9999
:::
100100

101101
### `toBeSelected()`
@@ -104,7 +104,7 @@ These matchers are the negation of each other, and both are provided to avoid do
104104
expect(element).toBeSelected();
105105
```
106106

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.
108108

109109
### `toBeChecked()` / `toBePartiallyChecked()` {#tobechecked}
110110

@@ -113,12 +113,12 @@ expect(element).toBeChecked();
113113
expect(element).toBePartiallyChecked();
114114
```
115115

116-
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.
117117

118118
:::note
119119

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.
122122

123123
:::
124124

@@ -129,10 +129,10 @@ expect(element).toBeExpanded();
129129
expect(element).toBeCollapsed();
130130
```
131131

132-
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.
133133

134134
:::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.
136136
:::
137137

138138
### `toBeBusy()`
@@ -141,7 +141,7 @@ These matchers are the negation of each other for expandable elements (elements
141141
expect(element).toBeBusy();
142142
```
143143

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.
145145

146146
## Checking element style
147147

@@ -151,9 +151,9 @@ Asserts whether the given element is busy from the user's perspective. It relies
151151
expect(element).toBeVisible();
152152
```
153153

154-
Asserts whether the given element is visible from the user's perspective.
154+
Checks if an element is visible.
155155

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.
157157

158158
### `toHaveStyle()`
159159

@@ -163,7 +163,7 @@ expect(element).toHaveStyle(
163163
)
164164
```
165165

166-
Asserts whether the given element has given styles.
166+
Checks if an element has the specified styles.
167167

168168
## Other matchers
169169

@@ -179,11 +179,11 @@ expect(element).toHaveAccessibleName(
179179
)
180180
```
181181

182-
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`.
183183

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.
185185

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.
187187

188188
### `toHaveProp()`
189189

@@ -194,8 +194,8 @@ expect(element).toHaveProp(
194194
)
195195
```
196196

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.
198198

199199
:::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.
201201
:::

0 commit comments

Comments
 (0)