Skip to content

Commit f3ce6df

Browse files
aw-davidsondavidsa
andauthored
Add docs for getElementError config option (testing-library#376)
Co-authored-by: davidsa <alec.davidson@adp.com>
1 parent c4c0ac2 commit f3ce6df

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

docs/dom-testing-library/api-configuration.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,28 @@ Configuration options:
2121
`testIdAttribute`: The attribute used by [`getByTestId`](api-queries#bytestid)
2222
and related queries. Defaults to `data-testid`.
2323

24+
`getElementError`: A function that returns the error used when
25+
[`getBy*`](api-queries#getby) or [`getAllBy*`](api-queries#getallby) fail. Takes
26+
the error message and container object as arguments.
27+
2428
<!--DOCUSAURUS_CODE_TABS-->
2529

2630
<!--Native-->
2731

2832
```js
2933
// setup-tests.js
3034
import { configure } from '@testing-library/dom'
31-
32-
configure({testIdAttribute: 'data-my-test-id'})
35+
import serialize from 'my-custom-dom-serializer'
36+
37+
configure({
38+
testIdAttribute: 'data-my-test-id',
39+
getElementError: (message, container) => {
40+
const customMessage = [message, serialize(container.firstChild)].join(
41+
'\n\n'
42+
)
43+
return new Error(customMessage)
44+
},
45+
})
3346
```
3447

3548
<!--React-->
@@ -38,7 +51,7 @@ configure({testIdAttribute: 'data-my-test-id'})
3851
// setup-tests.js
3952
import { configure } from '@testing-library/react'
4053

41-
configure({testIdAttribute: 'data-my-test-id'})
54+
configure({ testIdAttribute: 'data-my-test-id' })
4255
```
4356

4457
<!--Cypress-->
@@ -47,7 +60,7 @@ configure({testIdAttribute: 'data-my-test-id'})
4760
// setup-tests.js
4861
import { configure } from '@testing-library/cypress'
4962

50-
configure({testIdAttribute: 'data-my-test-id'})
63+
configure({ testIdAttribute: 'data-my-test-id' })
5164
```
5265

5366
<!--END_DOCUSAURUS_CODE_TABS-->

0 commit comments

Comments
 (0)