簡體   English   中英

使用 Jest 測試 React 組件時出現“ReferenceError: indexedDB is not defined”

[英]"ReferenceError: indexedDB is not defined" when testing React component with Jest

我使用 Create React App 創建了一個 React App,它使用 IndexedDB。

當我嘗試運行測試時,出現此錯誤:

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "ReferenceError: indexedDB is not defined".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

我了解測試套件是在沒有 IndexedDB 的無頭瀏覽器中運行的。

我該如何處理這種情況?

實際上,Jest 是用來模擬 memory 中的 DOM,而不是在實際的無頭瀏覽器中。 出於這個原因,您必須模擬這種 API 以使您的測試通過。

我發現一篇文章解釋了如何在 Jest 中模擬 indexedDB

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM