From 08417f193ca04077154458e0fa7eede99b2c386e Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 27 May 2020 17:37:41 +0200 Subject: [PATCH 1/2] feat: Improve performance of ByRole in waitFor* --- src/config.js | 10 ++++++++++ src/queries/role.js | 4 ++++ src/wait-for.js | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index 248c9008..c93ad92e 100644 --- a/src/config.js +++ b/src/config.js @@ -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) { diff --git a/src/queries/role.js b/src/queries/role.js index b0e4c4f5..f7eeff18 100644 --- a/src/queries/role.js +++ b/src/queries/role.js @@ -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, { diff --git a/src/wait-for.js b/src/wait-for.js index 931a01a7..ea99eae6 100644 --- a/src/wait-for.js +++ b/src/wait-for.js @@ -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). @@ -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. From da1f85642ee571443089856e5412ee772705b729 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 27 May 2020 17:59:21 +0200 Subject: [PATCH 2/2] Add test --- src/__tests__/role.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/__tests__/role.js b/src/__tests__/role.js index a2964893..ba1cb817 100644 --- a/src/__tests__/role.js +++ b/src/__tests__/role.js @@ -343,6 +343,12 @@ Here are the accessible roles: `) }) +test('has no useful error message in findBy', async () => { + const {findByRole} = render(`
  • `) + + await expect(findByRole('option', {timeout: 1})).rejects.toThrow('Unable to find role="option"') +}) + describe('configuration', () => { let originalConfig beforeEach(() => {