soso-tip icon indicating copy to clipboard operation
soso-tip copied to clipboard

react-testing-library/react-hooks with swr

Open SoYoung210 opened this issue 3 years ago • 0 comments

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,
}
);

renderHookwrapper로 넘겨준다.

그리고..

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설치했는지 살펴보자..

SoYoung210 avatar Mar 07 '21 07:03 SoYoung210