Skip to content

Commit 51618bd

Browse files
committed
Merge branch 'feature/suggestions' of https://github.com/benmonro/dom-testing-library into feature/suggestions
2 parents 7ec0b97 + 8861c24 commit 51618bd

File tree

8 files changed

+35
-4
lines changed

8 files changed

+35
-4
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,16 @@
954954
"code",
955955
"test"
956956
]
957+
},
958+
{
959+
"login": "pelotom",
960+
"name": "Tom Crockett",
961+
"avatar_url": "https://avatars2.githubusercontent.com/u/128019?v=4",
962+
"profile": "https://twitter.com/pelotom",
963+
"contributions": [
964+
"code",
965+
"test"
966+
]
957967
}
958968
],
959969
"repoHost": "https://github.com"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ Thanks goes to these people ([emoji key][emojis]):
255255
<td align="center"><a href="https://github.com/Lagily"><img src="https://avatars2.githubusercontent.com/u/42535205?v=4" width="100px;" alt=""/><br /><sub><b>Ali Nasserzadeh</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Lagily" title="Code">💻</a></td>
256256
<td align="center"><a href="https://darekkay.com"><img src="https://avatars0.githubusercontent.com/u/3101914?v=4" width="100px;" alt=""/><br /><sub><b>Darek Kay</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=darekkay" title="Documentation">📖</a></td>
257257
<td align="center"><a href="https://github.com/Lukas-Kullmann"><img src="https://avatars0.githubusercontent.com/u/387547?v=4" width="100px;" alt=""/><br /><sub><b>Lukas</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Lukas-Kullmann" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=Lukas-Kullmann" title="Tests">⚠️</a></td>
258+
<td align="center"><a href="https://twitter.com/pelotom"><img src="https://avatars2.githubusercontent.com/u/128019?v=4" width="100px;" alt=""/><br /><sub><b>Tom Crockett</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=pelotom" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=pelotom" title="Tests">⚠️</a></td>
258259
</tr>
259260
</table>
260261

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/__tests__/wait-for-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('waits for element to appear in the document', async () => {
2222
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
2323
Array [
2424
Array [
25-
"\`waitForElement\` has been deprecated. Use a \`find*\` query (preferred: https://testing-library.com/docs/dom-testing-library/api-queries#findby) or use \`waitFor\` instead (it's the same API, so you can find/replace): https://testing-library.com/docs/dom-testing-library/api-async#waitfor",
25+
"\`waitForElement\` has been deprecated. Use a \`find*\` query (preferred: https://testing-library.com/docs/dom-testing-library/api-queries#findby) or use \`waitFor\` instead: https://testing-library.com/docs/dom-testing-library/api-async#waitfor",
2626
],
2727
]
2828
`)

src/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ let config = {
3030
error.name = 'TestingLibraryElementError'
3131
return error
3232
},
33+
_disableExpensiveErrorDiagnostics: false,
34+
}
35+
36+
export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
37+
try {
38+
config._disableExpensiveErrorDiagnostics = true
39+
return callback()
40+
} finally {
41+
config._disableExpensiveErrorDiagnostics = false
42+
}
3343
}
3444

3545
export function configure(newConfig) {

src/queries/role.js

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

src/wait-for-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function waitForElement(callback, options) {
99
if (!hasWarned) {
1010
hasWarned = true
1111
console.warn(
12-
`\`waitForElement\` has been deprecated. Use a \`find*\` query (preferred: https://testing-library.com/docs/dom-testing-library/api-queries#findby) or use \`waitFor\` instead (it's the same API, so you can find/replace): https://testing-library.com/docs/dom-testing-library/api-async#waitfor`,
12+
`\`waitForElement\` has been deprecated. Use a \`find*\` query (preferred: https://testing-library.com/docs/dom-testing-library/api-queries#findby) or use \`waitFor\` instead: https://testing-library.com/docs/dom-testing-library/api-async#waitfor`,
1313
)
1414
}
1515
if (!callback) {

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).
@@ -61,7 +61,7 @@ function waitFor(
6161

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

0 commit comments

Comments
 (0)