Skip to content

Commit 62928cf

Browse files
authored
Merge branch 'master' into feat/aria-selector
2 parents 0bad768 + 9d62141 commit 62928cf

File tree

9 files changed

+109
-18
lines changed

9 files changed

+109
-18
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,16 @@
879879
"contributions": [
880880
"code"
881881
]
882+
},
883+
{
884+
"login": "rahulchavan30",
885+
"name": "Rahul Suryakanth",
886+
"avatar_url": "https://avatars2.githubusercontent.com/u/5296464?v=4",
887+
"profile": "https://github.com/rahulchavan30",
888+
"contributions": [
889+
"code",
890+
"test"
891+
]
882892
}
883893
],
884894
"repoHost": "https://github.com"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Thanks goes to these people ([emoji key][emojis]):
245245
<td align="center"><a href="https://github.com/Yama-Tomo"><img src="https://avatars0.githubusercontent.com/u/4970917?v=4" width="100px;" alt=""/><br /><sub><b>Yama-Tomo</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Yama-Tomo" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=Yama-Tomo" title="Tests">⚠️</a></td>
246246
<td align="center"><a href="https://github.com/airjp73"><img src="https://avatars2.githubusercontent.com/u/25882770?v=4" width="100px;" alt=""/><br /><sub><b>Aaron Pettengill</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=airjp73" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=airjp73" title="Tests">⚠️</a></td>
247247
<td align="center"><a href="http://kwboyd.com"><img src="https://avatars0.githubusercontent.com/u/13855750?v=4" width="100px;" alt=""/><br /><sub><b>Kate W. Boyd</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=kwboyd" title="Code">💻</a></td>
248+
<td align="center"><a href="https://github.com/rahulchavan30"><img src="https://avatars2.githubusercontent.com/u/5296464?v=4" width="100px;" alt=""/><br /><sub><b>Rahul Suryakanth</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=rahulchavan30" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=rahulchavan30" title="Tests">⚠️</a></td>
248249
</tr>
249250
</table>
250251

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@
3535
"dist"
3636
],
3737
"dependencies": {
38-
"@babel/runtime": "^7.9.2",
39-
"@types/testing-library__dom": "^7.0.0",
38+
"@babel/runtime": "^7.9.6",
39+
"@types/testing-library__dom": "^7.0.2",
4040
"aria-query": "^4.0.2",
41-
"dom-accessibility-api": "^0.4.2",
42-
"pretty-format": "^25.1.0"
41+
"dom-accessibility-api": "^0.4.3",
42+
"pretty-format": "^26.0.1"
4343
},
4444
"devDependencies": {
45-
"@testing-library/jest-dom": "^5.1.1",
45+
"@testing-library/jest-dom": "^5.5.0",
4646
"jest-in-case": "^1.0.2",
4747
"jest-serializer-ansi": "^1.0.3",
4848
"jest-watch-select-projects": "^2.0.0",
49-
"jsdom": "^16.2.1",
50-
"kcd-scripts": "^5.6.0"
49+
"jsdom": "^16.2.2",
50+
"kcd-scripts": "^6.0.0"
5151
},
5252
"eslintConfig": {
5353
"extends": "./node_modules/kcd-scripts/eslint.js",

src/__tests__/misc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {render} from './helpers/test-utils'
21
import {queryByAttribute} from '..'
2+
import {render} from './helpers/test-utils'
33

44
// we used to use queryByAttribute internally, but we don't anymore. Some people
55
// use it as an undocumented part of the API, so we'll keep it around.

src/__tests__/role-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ test.each([
176176
['<div style="visibility: visible" />', false],
177177
['<div hidden />', true],
178178
['<div style="display: none;"/>', true],
179-
['<div style="visibility: hidden;"/>', false], // bug in jsdom < 15.2
179+
['<div style="visibility: hidden;"/>', true],
180180
['<div aria-hidden="true" />', true],
181181
])('shouldExcludeFromA11yTree for %s returns %p', (html, expected) => {
182182
const {container} = render(html)

src/__tests__/screen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {renderIntoDocument} from './helpers/test-utils'
21
import {screen} from '..'
2+
import {renderIntoDocument} from './helpers/test-utils'
33

44
beforeEach(() => {
55
jest.spyOn(console, 'log').mockImplementation(() => {})

src/__tests__/wait-for.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {renderIntoDocument} from './helpers/test-utils'
21
import {waitFor} from '../'
2+
import {renderIntoDocument} from './helpers/test-utils'
33

44
test('waits callback to not throw an error', async () => {
55
const spy = jest.fn()
@@ -23,7 +23,7 @@ test('can timeout after the given timeout time', async () => {
2323
expect(result).toBe(error)
2424
})
2525

26-
test('uses generic error if there was no last error', async () => {
26+
test('if no error is thrown then throws a timeout error', async () => {
2727
const result = await waitFor(
2828
() => {
2929
// eslint-disable-next-line no-throw-literal
@@ -34,6 +34,58 @@ test('uses generic error if there was no last error', async () => {
3434
expect(result).toMatchInlineSnapshot(`[Error: Timed out in waitFor.]`)
3535
})
3636

37+
test('if showOriginalStackTrace on a timeout error then the stack trace does not include this file', async () => {
38+
const result = await waitFor(
39+
() => {
40+
// eslint-disable-next-line no-throw-literal
41+
throw undefined
42+
},
43+
{timeout: 8, interval: 5, showOriginalStackTrace: true},
44+
).catch(e => e)
45+
expect(result.stack).not.toMatch(__dirname)
46+
})
47+
48+
test('uses full stack error trace when showOriginalStackTrace present', async () => {
49+
const error = new Error('Throws the full stack trace')
50+
// even if the error is a TestingLibraryElementError
51+
error.name = 'TestingLibraryElementError'
52+
const originalStackTrace = error.stack
53+
const result = await waitFor(
54+
() => {
55+
throw error
56+
},
57+
{timeout: 8, interval: 5, showOriginalStackTrace: true},
58+
).catch(e => e)
59+
expect(result.stack).toBe(originalStackTrace)
60+
})
61+
62+
test('does not change the stack trace if the thrown error is not a TestingLibraryElementError', async () => {
63+
const error = new Error('Throws the full stack trace')
64+
const originalStackTrace = error.stack
65+
const result = await waitFor(
66+
() => {
67+
throw error
68+
},
69+
{timeout: 8, interval: 5},
70+
).catch(e => e)
71+
expect(result.stack).toBe(originalStackTrace)
72+
})
73+
74+
test('provides an improved stack trace if the thrown error is a TestingLibraryElementError', async () => {
75+
const error = new Error('Throws the full stack trace')
76+
error.name = 'TestingLibraryElementError'
77+
const originalStackTrace = error.stack
78+
const result = await waitFor(
79+
() => {
80+
throw error
81+
},
82+
{timeout: 8, interval: 5},
83+
).catch(e => e)
84+
// too hard to test that the stack trace is what we want it to be
85+
// so we'll just make sure that it's not the same as the origianl
86+
expect(result.stack).not.toBe(originalStackTrace)
87+
})
88+
3789
test('throws nice error if provided callback is not a function', () => {
3890
const {queryByTestId} = renderIntoDocument(`
3991
<div data-testid="div"></div>

src/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let config = {
1616
asyncWrapper: cb => cb(),
1717
// default value for the `hidden` option in `ByRole` queries
1818
defaultHidden: false,
19+
//showOriginalStackTrace flag to show the full error stack traces for async errors
20+
showOriginalStackTrace: false,
1921

2022
// called when getBy* queries fail. (message, container) => Error
2123
getElementError(message, container) {

src/wait-for.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,32 @@ import {
88
} from './helpers'
99
import {getConfig} from './config'
1010

11+
// This is so the stack trace the developer sees is one that's
12+
// closer to their code (because async stack traces are hard to follow).
13+
function copyStackTrace(target, source) {
14+
target.stack = source.stack.replace(source.message, target.message)
15+
}
16+
1117
function waitFor(
1218
callback,
1319
{
1420
container = getDocument(),
1521
timeout = getConfig().asyncUtilTimeout,
22+
showOriginalStackTrace = getConfig().showOriginalStackTrace,
23+
stackTraceError,
1624
interval = 50,
1725
mutationObserverOptions = {
1826
subtree: true,
1927
childList: true,
2028
attributes: true,
2129
characterData: true,
2230
},
23-
} = {},
31+
},
2432
) {
2533
if (typeof callback !== 'function') {
2634
throw new TypeError('Received `callback` arg must be a function')
2735
}
2836

29-
// created here so we get a nice stacktrace
30-
const timedOutError = new Error('Timed out in waitFor.')
3137
if (interval < 1) interval = 1
3238
return new Promise((resolve, reject) => {
3339
let lastError
@@ -63,13 +69,33 @@ function waitFor(
6369
}
6470

6571
function onTimeout() {
66-
onDone(lastError || timedOutError, null)
72+
let error
73+
if (lastError) {
74+
error = lastError
75+
if (
76+
!showOriginalStackTrace &&
77+
error.name === 'TestingLibraryElementError'
78+
) {
79+
copyStackTrace(error, stackTraceError)
80+
}
81+
} else {
82+
error = new Error('Timed out in waitFor.')
83+
if (!showOriginalStackTrace) {
84+
copyStackTrace(error, stackTraceError)
85+
}
86+
}
87+
onDone(error, null)
6788
}
6889
})
6990
}
7091

71-
function waitForWrapper(...args) {
72-
return getConfig().asyncWrapper(() => waitFor(...args))
92+
function waitForWrapper(callback, options) {
93+
// create the error here so its stack trace is as close to the
94+
// calling code as possible
95+
const stackTraceError = new Error('STACK_TRACE_MESSAGE')
96+
return getConfig().asyncWrapper(() =>
97+
waitFor(callback, {stackTraceError, ...options}),
98+
)
7399
}
74100

75101
let hasWarned = false

0 commit comments

Comments
 (0)