diff --git a/src/__tests__/useAsync.test.tsx b/src/__tests__/useAsync.test.tsx index 4e5ca61f68..db780bd966 100644 --- a/src/__tests__/useAsync.test.tsx +++ b/src/__tests__/useAsync.test.tsx @@ -107,11 +107,14 @@ describe('useAsync', () => { return 'new value'; }; - beforeEach(() => { + beforeEach(done => { callCount = 0; + hook = renderHook(({ fn }) => useAsync(fn, [fn]), { initialProps: { fn: initialFn }, }); + + hook.waitForNextUpdate().then(done); }); it('renders the first value', () => { @@ -140,7 +143,7 @@ describe('useAsync', () => { return `counter is ${counter} and callCount is ${callCount}`; }; - beforeEach(() => { + beforeEach(done => { callCount = 0; hook = renderHook( ({ fn, counter }) => { @@ -154,6 +157,8 @@ describe('useAsync', () => { }, } ); + + hook.waitForNextUpdate().then(done); }); it('initial renders the first passed pargs', () => { diff --git a/src/__tests__/useAsyncFn.test.tsx b/src/__tests__/useAsyncFn.test.tsx index f9c7d238f6..fdcbe3a625 100644 --- a/src/__tests__/useAsyncFn.test.tsx +++ b/src/__tests__/useAsyncFn.test.tsx @@ -5,7 +5,7 @@ // does not automatically invoke the function // and it can take arguments. -import { renderHook } from '@testing-library/react-hooks'; +import { act, renderHook } from '@testing-library/react-hooks'; import useAsyncFn, { AsyncState } from '../useAsyncFn'; type AdderFn = (a: number, b: number) => Promise; @@ -17,27 +17,26 @@ describe('useAsyncFn', () => { describe('the callback can be awaited and return the value', () => { let hook; - let callCount = 0; const adder = async (a: number, b: number): Promise => { - callCount++; return a + b; }; beforeEach(() => { // NOTE: renderHook isn't good at inferring array types hook = renderHook<{ fn: AdderFn }, [AsyncState, AdderFn]>(({ fn }) => useAsyncFn(fn), { - initialProps: { - fn: adder, - }, + initialProps: { fn: adder }, }); }); it('awaits the result', async () => { expect.assertions(3); - const [s, callback] = hook.result.current; + const [, callback] = hook.result.current; + let result; - const result = await callback(5, 7); + await act(async () => { + result = await callback(5, 7); + }); expect(result).toEqual(12); @@ -78,13 +77,15 @@ describe('useAsyncFn', () => { it('resolves a value derived from args', async () => { expect.assertions(4); - const [s, callback] = hook.result.current; + const [, callback] = hook.result.current; - callback(2, 7); + act(() => { + callback(2, 7); + }); hook.rerender({ fn: adder }); await hook.waitForNextUpdate(); - const [state, c] = hook.result.current; + const [state] = hook.result.current; expect(callCount).toEqual(1); expect(state.loading).toEqual(false);