單元測試踩坑

問題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

問題2

描述:測試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ù)的過程,所以這篇文章會(huì)不斷更新,將我做單元測試遇到的所有問題進(jìn)行匯總。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 本文使用的項(xiàng)目代碼,是基于之前文章使用的Spring Boot項(xiàng)目: Spring Boot數(shù)據(jù)庫交互之My...
    狄仁杰666閱讀 6,574評(píng)論 0 4
  • 1、IDEA無法下載JUnitGenerator插件 報(bào)錯(cuò): PluginJUnitGeneratorV2.0 w...
    天下不喵閱讀 10,586評(píng)論 0 0
  • 實(shí)習(xí)期間做單元測試,一開始都靠往數(shù)據(jù)庫硬塞數(shù)據(jù),后來發(fā)現(xiàn)這樣做就算本地能通過,在線上通不過,故開始學(xué)習(xí),發(fā)現(xiàn)遇到的...
    衣錦晝行閱讀 242評(píng)論 0 0
  • React單元測試(Jest + Enzyme) 前提: 使用create-react-app生成一個(gè)項(xiàng)目。使用...
    白霽閱讀 2,079評(píng)論 0 3
  • Jest是facebook出品的JavaScript單元測試軟件. 最常用的代碼: expect(變量).toEq...
    Metoo丶淡然閱讀 13,746評(píng)論 1 12

友情鏈接更多精彩內(nèi)容