Skip to content

Commit 0d7294f

Browse files
authored
fix: element error delegation (#777)
1 parent 9b08b70 commit 0d7294f

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/__tests__/wait-for.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {waitFor} from '../'
2+
import {configure, getConfig} from '../config'
23
import {renderIntoDocument} from './helpers/test-utils'
34

45
test('waits callback to not throw an error', async () => {
@@ -125,3 +126,22 @@ test('timeout logs a pretty DOM', async () => {
125126
</html>"
126127
`)
127128
})
129+
130+
test('should delegate to config.getElementError', async () => {
131+
const originalConfig = getConfig()
132+
const elementError = new Error('Custom element error')
133+
const getElementError = jest.fn().mockImplementation(() => elementError)
134+
configure({getElementError})
135+
136+
renderIntoDocument(`<div id="pretty">how pretty</div>`)
137+
const error = await waitFor(
138+
() => {
139+
throw new Error('always throws')
140+
},
141+
{timeout: 1},
142+
).catch(e => e)
143+
144+
expect(getElementError).toBeCalledTimes(1)
145+
expect(error.message).toMatchInlineSnapshot(`"Custom element error"`)
146+
configure(originalConfig)
147+
})

src/query-helpers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {prettyDOM} from './pretty-dom'
21
import {getSuggestedQuery} from './suggestions'
32
import {fuzzyMatches, matches, makeNormalizer} from './matches'
43
import {waitFor} from './wait-for'
@@ -46,7 +45,9 @@ function makeSingleQuery(allQuery, getMultipleError) {
4645
return (container, ...args) => {
4746
const els = allQuery(container, ...args)
4847
if (els.length > 1) {
49-
const elementStrings = els.map(element => prettyDOM(element)).join('\n\n')
48+
const elementStrings = els
49+
.map(element => getElementError(null, element).message)
50+
.join('\n\n')
5051

5152
throw getMultipleElementsFoundError(
5253
`${getMultipleError(container, ...args)}

src/wait-for.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
clearTimeout,
1111
} from './helpers'
1212
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config'
13-
import {prettyDOM} from './pretty-dom'
1413

1514
// This is so the stack trace the developer sees is one that's
1615
// closer to their code (because async stack traces are hard to follow).
@@ -27,7 +26,10 @@ function waitFor(
2726
stackTraceError,
2827
interval = 50,
2928
onTimeout = error => {
30-
error.message = `${error.message}\n\n${prettyDOM(container)}`
29+
error.message = getConfig().getElementError(
30+
error.message,
31+
container,
32+
).message
3133
return error
3234
},
3335
mutationObserverOptions = {

0 commit comments

Comments
 (0)