Skip to content

Commit 709044b

Browse files
eps1lonkentcdodds
andauthored
feat: Improve performance of ByRole in waitFor* (#590)
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
1 parent 2a366f8 commit 709044b

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

src/__tests__/role.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ Here are the accessible roles:
343343
`)
344344
})
345345

346+
test('has no useful error message in findBy', async () => {
347+
const {findByRole} = render(`<li />`)
348+
349+
await expect(findByRole('option', {timeout: 1})).rejects.toThrow('Unable to find role="option"')
350+
})
351+
346352
test('explicit role is most specific', () => {
347353
const {getByRole} = renderIntoDocument(
348354
`<button role="tab" aria-label="my-tab" />`,

src/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ let config = {
2727
error.name = 'TestingLibraryElementError'
2828
return error
2929
},
30+
_disableExpensiveErrorDiagnostics: false,
31+
}
32+
33+
export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
34+
try {
35+
config._disableExpensiveErrorDiagnostics = true
36+
return callback()
37+
} finally {
38+
config._disableExpensiveErrorDiagnostics = false
39+
}
3040
}
3141

3242
export function configure(newConfig) {

src/queries/role.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ const getMissingError = (
112112
role,
113113
{hidden = getConfig().defaultHidden, name} = {},
114114
) => {
115+
if (getConfig()._disableExpensiveErrorDiagnostics) {
116+
return `Unable to find role="${role}"`
117+
}
118+
115119
let roles = ''
116120
Array.from(container.children).forEach(childElement => {
117121
roles += prettyRoles(childElement, {

src/wait-for.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
clearTimeout,
77
runWithRealTimers,
88
} from './helpers'
9-
import {getConfig} from './config'
9+
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config'
1010

1111
// This is so the stack trace the developer sees is one that's
1212
// closer to their code (because async stack traces are hard to follow).
@@ -60,7 +60,7 @@ function waitFor(
6060

6161
function checkCallback() {
6262
try {
63-
onDone(null, callback())
63+
onDone(null, runWithExpensiveErrorDiagnosticsDisabled(callback))
6464
// If `callback` throws, wait for the next mutation or timeout.
6565
} catch (error) {
6666
// Save the callback error to reject the promise with it.

0 commit comments

Comments
 (0)