Skip to content

feat(waitFor): improve error message for non-function callbacks #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/__tests__/wait-for.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {renderIntoDocument} from './helpers/test-utils'
import {waitFor} from '../'

test('waits callback to not throw an error', async () => {
Expand Down Expand Up @@ -32,3 +33,13 @@ test('uses generic error if there was no last error', async () => {
).catch(e => e)
expect(result).toMatchInlineSnapshot(`[Error: Timed out in waitFor.]`)
})

test('throws nice error if provided callback is not a function', () => {
const {queryByTestId} = renderIntoDocument(`
<div data-testid="div"></div>
`)
const someElement = queryByTestId('div')
expect(() => waitFor(someElement)).toThrow(
'Received `callback` arg must be a function',
)
})
4 changes: 4 additions & 0 deletions src/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function waitFor(
},
} = {},
) {
if (typeof callback !== 'function') {
throw new TypeError('Received `callback` arg must be a function')
}

// created here so we get a nice stacktrace
const timedOutError = new Error('Timed out in waitFor.')
if (interval < 1) interval = 1
Expand Down