Skip to content

Commit 02a5b82

Browse files
author
Kent C. Dodds
authored
feat(waitFor*): improve stacktrace for timeout errors (#492)
* feat(waitFor*): improve stacktrace for timeout errors * Update src/wait-for.js Co-Authored-By: Tim Deschryver <[email protected]> * update snapshots Co-authored-by: Tim Deschryver <[email protected]> Closes #491
1 parent 976675b commit 02a5b82

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/__tests__/wait-for-element-to-be-removed.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ test('rethrows non-testing-lib errors', () => {
141141
).rejects.toBe(error)
142142
})
143143

144+
test('logs timeout error when it times out', async () => {
145+
const div = document.createElement('div')
146+
await expect(
147+
waitForElementToBeRemoved(() => div, {timeout: 1}),
148+
).rejects.toThrowErrorMatchingInlineSnapshot(
149+
`"Timed out in waitForElementToBeRemoved."`,
150+
)
151+
})
152+
144153
test('accepts an element as an argument and waits for it to be removed from its top-most parent', async () => {
145154
const {queryByTestId} = renderIntoDocument(`
146155
<div data-testid="div"></div>

src/__tests__/wait-for.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ test('can timeout after the given timeout time', async () => {
2121
).catch(e => e)
2222
expect(result).toBe(error)
2323
})
24+
25+
test('uses generic error if there was no last error', async () => {
26+
const result = await waitFor(
27+
() => {
28+
// eslint-disable-next-line no-throw-literal
29+
throw undefined
30+
},
31+
{timeout: 8, interval: 5},
32+
).catch(e => e)
33+
expect(result).toMatchInlineSnapshot(`[Error: Timed out in waitFor.]`)
34+
})

src/wait-for-element-to-be-removed.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ function initialCheck(elements) {
1313
}
1414

1515
async function waitForElementToBeRemoved(callback, options) {
16+
// created here so we get a nice stacktrace
17+
const timeoutError = new Error('Timed out in waitForElementToBeRemoved.')
1618
if (typeof callback !== 'function') {
17-
// await waitForElementToBeRemoved(getAllByText('Hello'))
1819
initialCheck(callback)
1920
const elements = Array.isArray(callback) ? callback : [callback]
2021
const getRemainingElements = elements.map(element => {
@@ -38,7 +39,7 @@ async function waitForElementToBeRemoved(callback, options) {
3839
throw error
3940
}
4041
if (!isRemoved(result)) {
41-
throw new Error('Timed out in waitForElementToBeRemoved.')
42+
throw timeoutError
4243
}
4344
return true
4445
}, options)

src/wait-for.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function waitFor(
2222
},
2323
} = {},
2424
) {
25+
// created here so we get a nice stacktrace
26+
const timedOutError = new Error('Timed out in waitFor.')
2527
if (interval < 1) interval = 1
2628
return new Promise((resolve, reject) => {
2729
let lastError
@@ -57,7 +59,7 @@ function waitFor(
5759
}
5860

5961
function onTimeout() {
60-
onDone(lastError || new Error('Timed out in wait.'), null)
62+
onDone(lastError || timedOutError, null)
6163
}
6264
})
6365
}

0 commit comments

Comments
 (0)