Skip to content

Commit 7af237e

Browse files
authored
fix: async test warnings (#543)
fix: async test warnings
2 parents 3782dc7 + 4a7fe73 commit 7af237e

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/__tests__/useAsync.test.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ describe('useAsync', () => {
107107
return 'new value';
108108
};
109109

110-
beforeEach(() => {
110+
beforeEach(done => {
111111
callCount = 0;
112+
112113
hook = renderHook(({ fn }) => useAsync(fn, [fn]), {
113114
initialProps: { fn: initialFn },
114115
});
116+
117+
hook.waitForNextUpdate().then(done);
115118
});
116119

117120
it('renders the first value', () => {
@@ -140,7 +143,7 @@ describe('useAsync', () => {
140143
return `counter is ${counter} and callCount is ${callCount}`;
141144
};
142145

143-
beforeEach(() => {
146+
beforeEach(done => {
144147
callCount = 0;
145148
hook = renderHook(
146149
({ fn, counter }) => {
@@ -154,6 +157,8 @@ describe('useAsync', () => {
154157
},
155158
}
156159
);
160+
161+
hook.waitForNextUpdate().then(done);
157162
});
158163

159164
it('initial renders the first passed pargs', () => {

src/__tests__/useAsyncFn.test.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// does not automatically invoke the function
66
// and it can take arguments.
77

8-
import { renderHook } from '@testing-library/react-hooks';
8+
import { act, renderHook } from '@testing-library/react-hooks';
99
import useAsyncFn, { AsyncState } from '../useAsyncFn';
1010

1111
type AdderFn = (a: number, b: number) => Promise<number>;
@@ -17,27 +17,26 @@ describe('useAsyncFn', () => {
1717

1818
describe('the callback can be awaited and return the value', () => {
1919
let hook;
20-
let callCount = 0;
2120
const adder = async (a: number, b: number): Promise<number> => {
22-
callCount++;
2321
return a + b;
2422
};
2523

2624
beforeEach(() => {
2725
// NOTE: renderHook isn't good at inferring array types
2826
hook = renderHook<{ fn: AdderFn }, [AsyncState<number>, AdderFn]>(({ fn }) => useAsyncFn(fn), {
29-
initialProps: {
30-
fn: adder,
31-
},
27+
initialProps: { fn: adder },
3228
});
3329
});
3430

3531
it('awaits the result', async () => {
3632
expect.assertions(3);
3733

38-
const [s, callback] = hook.result.current;
34+
const [, callback] = hook.result.current;
35+
let result;
3936

40-
const result = await callback(5, 7);
37+
await act(async () => {
38+
result = await callback(5, 7);
39+
});
4140

4241
expect(result).toEqual(12);
4342

@@ -78,13 +77,15 @@ describe('useAsyncFn', () => {
7877
it('resolves a value derived from args', async () => {
7978
expect.assertions(4);
8079

81-
const [s, callback] = hook.result.current;
80+
const [, callback] = hook.result.current;
8281

83-
callback(2, 7);
82+
act(() => {
83+
callback(2, 7);
84+
});
8485
hook.rerender({ fn: adder });
8586
await hook.waitForNextUpdate();
8687

87-
const [state, c] = hook.result.current;
88+
const [state] = hook.result.current;
8889

8990
expect(callCount).toEqual(1);
9091
expect(state.loading).toEqual(false);

0 commit comments

Comments
 (0)