Skip to content

Commit 672f231

Browse files
authored
feat(waitFor): improve error message for non-function callbacks (#515)
* feat(waitFor): improve error message for non-function callbacks * refactor: address pr feedback
1 parent f3f84ce commit 672f231

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/__tests__/wait-for.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {renderIntoDocument} from './helpers/test-utils'
12
import {waitFor} from '../'
23

34
test('waits callback to not throw an error', async () => {
@@ -32,3 +33,13 @@ test('uses generic error if there was no last error', async () => {
3233
).catch(e => e)
3334
expect(result).toMatchInlineSnapshot(`[Error: Timed out in waitFor.]`)
3435
})
36+
37+
test('throws nice error if provided callback is not a function', () => {
38+
const {queryByTestId} = renderIntoDocument(`
39+
<div data-testid="div"></div>
40+
`)
41+
const someElement = queryByTestId('div')
42+
expect(() => waitFor(someElement)).toThrow(
43+
'Received `callback` arg must be a function',
44+
)
45+
})

src/wait-for.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function waitFor(
2222
},
2323
} = {},
2424
) {
25+
if (typeof callback !== 'function') {
26+
throw new TypeError('Received `callback` arg must be a function')
27+
}
28+
2529
// created here so we get a nice stacktrace
2630
const timedOutError = new Error('Timed out in waitFor.')
2731
if (interval < 1) interval = 1

0 commit comments

Comments
 (0)