問(wèn)題1
描述:使用react-test-renderer渲染組件,生成快照時(shí)報(bào)錯(cuò)Unable to find node on an unmounted component.
解決: react-test-renderer版本降級(jí)豹悬,我之前用的是17.0.0谤祖,降級(jí)到16.14.0
yarn upgrade react-test-renderer@16.14.0
問(wèn)題2
描述:測(cè)試antd組件時(shí)些楣,報(bào)錯(cuò)useLayoutEffect does nothing on the server掂碱。
Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://fb.me/react-uselayouteffect-ssr for common fixes.
in CSSMotion
in LoadingIcon
in button
in Context.Consumer
in Wave
in Button
in ExportButton
解決:
jest.mock('react', () => ({
...jest.requireActual('react'),
useLayoutEffect: jest.requireActual('react').useEffect,
}));
踩坑是一個(gè)持續(xù)的過(guò)程躏敢,所以這篇文章會(huì)不斷更新商佛,將我做單元測(cè)試遇到的所有問(wèn)題進(jìn)行匯總喉钢。