Skip to content

Commit 2a05008

Browse files
committed
add test for synchronous errors
1 parent dd46c49 commit 2a05008

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/useAsync.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface StarwarsHero {
66
}
77

88
export const generateMockResponseData = (amount: number = 5): StarwarsHero[] =>
9+
// @ts-ignore
910
[...Array(amount).keys()].map(n => ({
1011
id: n + 1,
1112
name: `Starwars Hero ${n + 1}`,
@@ -127,4 +128,31 @@ describe('useAync', () => {
127128
expect(onSuccess).not.toHaveBeenCalled();
128129
expect(onError).toHaveBeenCalled();
129130
});
131+
132+
it('should set error detail for error thrown synchronously (like when preparing/formatting a payload)', async () => {
133+
const onSuccess = jest.fn();
134+
const onError = jest.fn();
135+
136+
const { result, waitForNextUpdate } = renderHook(() =>
137+
useAsync(
138+
() => {
139+
throw new Error('something went wrong');
140+
},
141+
[],
142+
{
143+
onSuccess: () => onSuccess(),
144+
onError: () => onError(),
145+
}
146+
)
147+
);
148+
149+
await waitForNextUpdate();
150+
151+
expect(result.current.error).toBeDefined();
152+
expect(result.current.error!.message).toBe('something went wrong');
153+
expect(result.current.loading).toBe(false);
154+
expect(result.current.result).toBeUndefined();
155+
expect(onSuccess).not.toHaveBeenCalled();
156+
expect(onError).toHaveBeenCalled();
157+
});
130158
});

0 commit comments

Comments
 (0)