Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add note for throwSuggestions issue #1306 #1425

Merged
30 changes: 30 additions & 0 deletions docs/dom-testing-library/api-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ option.
screen.getByTestId('foo', {suggest: false}) // will not throw a suggestion
```

:::note

When this option is enabled, the library may provide suggestions that lack an
intuitive implementation, this typically occurs for
[roles which can not be named](https://w3c.github.io/aria/#namefromprohibited),
most notably paragraphs. For instance, if you attempt to use
[`getByText`](queries/bytext.mdx), you may encounter the following error:
enmanuelduran marked this conversation as resolved.
Show resolved Hide resolved

```
TestingLibraryElementError: A better query is available, try this:
getByRole('paragraph')
```

However, there is no direct way to query paragraphs using an object as the
second parameter, such as in
`getByRole('paragraph', { name: 'Hello World' })`.
enmanuelduran marked this conversation as resolved.
Show resolved Hide resolved

To address this issue, you can leverage a custom function to validate the
element's structure and suppress the error, see
[#1306](https://github.com/testing-library/dom-testing-library/issues/1306)
and the example below:
enmanuelduran marked this conversation as resolved.
Show resolved Hide resolved

```js
getByRole('paragraph', {
name: (_, element) => element.textContent === 'Hello world',
})
```

:::

### `testIdAttribute`

The attribute used by [`getByTestId`](queries/bytestid.mdx) and related queries.
Expand Down