Skip to content

Commit 77a909c

Browse files
eunwoo-leviclaude
andcommitted
test(core/hooks): add test for useAsyncEffect cleanup on early unmount
Covers the case where the component unmounts before the async effect's Promise resolves — the previously missing scenario that allowed the race condition bug to go undetected. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d293fe0 commit 77a909c

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

packages/core/src/hooks/useAsyncEffect/useAsyncEffect.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ describe('useAsyncEffect', () => {
8989
expect(cleanup).toHaveBeenCalled();
9090
});
9191

92+
it('should call cleanup even when component unmounts before async effect resolves', async () => {
93+
const cleanup = vi.fn();
94+
const { unmount } = await renderHookSSR(() =>
95+
useAsyncEffect(async () => {
96+
await new Promise(resolve => setTimeout(resolve, 1000));
97+
return cleanup;
98+
}, [])
99+
);
100+
101+
await flushPromises();
102+
unmount();
103+
expect(cleanup).not.toHaveBeenCalled();
104+
105+
vi.advanceTimersByTime(1000);
106+
await flushPromises();
107+
108+
expect(cleanup).toHaveBeenCalledTimes(1);
109+
});
110+
92111
it('should call effect every rerender when deps are undefined', async () => {
93112
const effect = vi.fn().mockResolvedValue(undefined);
94113

0 commit comments

Comments
 (0)