soso-tip
soso-tip copied to clipboard
react-testing-library/react-hooks with swr
cache clear안될때
import React, { ReactNode } from 'react';
import { SWRConfig } from 'swr';
/**
* @description https://github.com/vercel/swr/pull/231#issuecomment-591614747
*/
const SWRNoCache = ({ children }: { children: ReactNode }) => (
<SWRConfig value={{ dedupingInterval: 0 }}>{children}</SWRConfig>
);
export default SWRNoCache;
no cache wrapper를 추가하고,
const { result, waitForNextUpdate } = renderHook(useFoo, {
wrapper: SWRNoCache,
}
);
renderHook
의 wrapper
로 넘겨준다.
그리고..
import { waitFor } from '@testing-library/react';
import { cache } from 'swr';
afterEach(async () => {
await waitFor(() => cache.clear());
});
잊지말고 cache clear
Reference
- https://github.com/vercel/swr/issues/781#issuecomment-742743248
- https://github.com/vercel/swr/pull/231#issuecomment-591614747
간단한 비동기 액션인데 잘 안될때
react-test-renderer
등 peerDependency설치했는지 살펴보자..