繁体   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