From bde4cf5656f48b8678f39cfd5307edad535b0574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltra=CC=81n=20Alarco=CC=81n?= Date: Tue, 31 Mar 2020 11:31:42 +0200 Subject: [PATCH 1/2] feat(waitFor): improve error message for non-function callbacks --- src/__tests__/wait-for.js | 6 ++++++ src/wait-for.js | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/__tests__/wait-for.js b/src/__tests__/wait-for.js index f4cf0a6c..0c1db336 100644 --- a/src/__tests__/wait-for.js +++ b/src/__tests__/wait-for.js @@ -32,3 +32,9 @@ 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', () => { + expect(() => waitFor('not a function')).toThrow( + 'Received `callback` arg must be a function', + ) +}) diff --git a/src/wait-for.js b/src/wait-for.js index 376594d5..9055113b 100644 --- a/src/wait-for.js +++ b/src/wait-for.js @@ -22,6 +22,10 @@ function waitFor( }, } = {}, ) { + if (typeof callback !== 'function') { + throw new Error('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 From 05aa661570720b5c9fffc486564b0b9fce490ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltra=CC=81n=20Alarco=CC=81n?= Date: Tue, 31 Mar 2020 12:10:05 +0200 Subject: [PATCH 2/2] refactor: address pr feedback --- src/__tests__/wait-for.js | 7 ++++++- src/wait-for.js | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/__tests__/wait-for.js b/src/__tests__/wait-for.js index 0c1db336..4ba4702d 100644 --- a/src/__tests__/wait-for.js +++ b/src/__tests__/wait-for.js @@ -1,3 +1,4 @@ +import {renderIntoDocument} from './helpers/test-utils' import {waitFor} from '../' test('waits callback to not throw an error', async () => { @@ -34,7 +35,11 @@ test('uses generic error if there was no last error', async () => { }) test('throws nice error if provided callback is not a function', () => { - expect(() => waitFor('not a function')).toThrow( + const {queryByTestId} = renderIntoDocument(` +
+ `) + const someElement = queryByTestId('div') + expect(() => waitFor(someElement)).toThrow( 'Received `callback` arg must be a function', ) }) diff --git a/src/wait-for.js b/src/wait-for.js index 9055113b..a7a35f68 100644 --- a/src/wait-for.js +++ b/src/wait-for.js @@ -23,7 +23,7 @@ function waitFor( } = {}, ) { if (typeof callback !== 'function') { - throw new Error('Received `callback` arg must be a function') + throw new TypeError('Received `callback` arg must be a function') } // created here so we get a nice stacktrace