Skip to content

Commit

Permalink
feat(waitFor*): improve stacktrace for timeout errors (#492)
Browse files Browse the repository at this point in the history
* feat(waitFor*): improve stacktrace for timeout errors

* Update src/wait-for.js

Co-Authored-By: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com>

* update snapshots

Co-authored-by: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com>

Closes #491
  • Loading branch information
Kent C. Dodds committed Mar 20, 2020
1 parent 976675b commit 02a5b82
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/wait-for-element-to-be-removed.js
Expand Up @@ -141,6 +141,15 @@ test('rethrows non-testing-lib errors', () => {
).rejects.toBe(error)
})

test('logs timeout error when it times out', async () => {
const div = document.createElement('div')
await expect(
waitForElementToBeRemoved(() => div, {timeout: 1}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Timed out in waitForElementToBeRemoved."`,
)
})

test('accepts an element as an argument and waits for it to be removed from its top-most parent', async () => {
const {queryByTestId} = renderIntoDocument(`
<div data-testid="div"></div>
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/wait-for.js
Expand Up @@ -21,3 +21,14 @@ test('can timeout after the given timeout time', async () => {
).catch(e => e)
expect(result).toBe(error)
})

test('uses generic error if there was no last error', async () => {
const result = await waitFor(
() => {
// eslint-disable-next-line no-throw-literal
throw undefined
},
{timeout: 8, interval: 5},
).catch(e => e)
expect(result).toMatchInlineSnapshot(`[Error: Timed out in waitFor.]`)
})
5 changes: 3 additions & 2 deletions src/wait-for-element-to-be-removed.js
Expand Up @@ -13,8 +13,9 @@ function initialCheck(elements) {
}

async function waitForElementToBeRemoved(callback, options) {
// created here so we get a nice stacktrace
const timeoutError = new Error('Timed out in waitForElementToBeRemoved.')
if (typeof callback !== 'function') {
// await waitForElementToBeRemoved(getAllByText('Hello'))
initialCheck(callback)
const elements = Array.isArray(callback) ? callback : [callback]
const getRemainingElements = elements.map(element => {
Expand All @@ -38,7 +39,7 @@ async function waitForElementToBeRemoved(callback, options) {
throw error
}
if (!isRemoved(result)) {
throw new Error('Timed out in waitForElementToBeRemoved.')
throw timeoutError
}
return true
}, options)
Expand Down
4 changes: 3 additions & 1 deletion src/wait-for.js
Expand Up @@ -22,6 +22,8 @@ function waitFor(
},
} = {},
) {
// created here so we get a nice stacktrace
const timedOutError = new Error('Timed out in waitFor.')
if (interval < 1) interval = 1
return new Promise((resolve, reject) => {
let lastError
Expand Down Expand Up @@ -57,7 +59,7 @@ function waitFor(
}

function onTimeout() {
onDone(lastError || new Error('Timed out in wait.'), null)
onDone(lastError || timedOutError, null)
}
})
}
Expand Down

0 comments on commit 02a5b82

Please sign in to comment.