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

feat: Improve performance of ByRole in waitFor* #590

Merged
merged 3 commits into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/__tests__/role.js
Expand Up @@ -343,6 +343,12 @@ Here are the accessible roles:
`)
})

test('has no useful error message in findBy', async () => {
const {findByRole} = render(`<li />`)

await expect(findByRole('option', {timeout: 1})).rejects.toThrow('Unable to find role="option"')
})

describe('configuration', () => {
let originalConfig
beforeEach(() => {
Expand Down
10 changes: 10 additions & 0 deletions src/config.js
Expand Up @@ -27,6 +27,16 @@ let config = {
error.name = 'TestingLibraryElementError'
return error
},
_disableExpensiveErrorDiagnostics: false,
}

export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
try {
config._disableExpensiveErrorDiagnostics = true
return callback()
} finally {
config._disableExpensiveErrorDiagnostics = false
}
}

export function configure(newConfig) {
Expand Down
4 changes: 4 additions & 0 deletions src/queries/role.js
Expand Up @@ -112,6 +112,10 @@ const getMissingError = (
role,
{hidden = getConfig().defaultHidden, name} = {},
) => {
if (getConfig()._disableExpensiveErrorDiagnostics) {
return `Unable to find role="${role}"`
}

let roles = ''
Array.from(container.children).forEach(childElement => {
roles += prettyRoles(childElement, {
Expand Down
4 changes: 2 additions & 2 deletions src/wait-for.js
Expand Up @@ -6,7 +6,7 @@ import {
clearTimeout,
runWithRealTimers,
} from './helpers'
import {getConfig} from './config'
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config'

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

function checkCallback() {
try {
onDone(null, callback())
onDone(null, runWithExpensiveErrorDiagnosticsDisabled(callback))
// If `callback` throws, wait for the next mutation or timeout.
} catch (error) {
// Save the callback error to reject the promise with it.
Expand Down