Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

like image 355
user5507535 Avatar asked Feb 17 '26 07:02

user5507535


1 Answers

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

like image 131
teo-garcia Avatar answered Feb 19 '26 20:02

teo-garcia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!