简体   繁体   中英

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

I have created a React App using Create React App, it uses IndexedDB.

When I try to run a test I'm getting this error:

[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'
}

I understand that the testing suit is run in a headless browser that doesn't have IndexedDB.

How can I handle this case?

Actually, Jest is used to simulate the DOM in memory, not in an actual headless browser. For that reason, you have to mock that kind of APIs in order to make your tests pass.

I found an article that explains how to mock indexedDB in Jest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM