Closed ZYinMD closed 4 years ago
Possibly using real ES modules rather than compiling them to CommonJS would help it be smart enough not to execute that code containing indexeddb, but I'm not sure. If not, then your only options would be to further decouple the functions into separate files, or to mock indexeddb. You could use fake-indexeddb for the latter purpose, like described in the README here. It's kind of overkill if you're not actually testing IndexedDB, but it would work.
Thanks! I tried following the README and set setupFiles: ['fake-indexeddb/auto']
in jest.config.js
, it works in my simple demo project, but doesn't work in create-react-app project (test still fails). Is there some other settings I could do?
demo repo: https://github.com/ZYinMD/jest-indexeddb-undefined-demo
I encountered an "indexeddb is not defined" issue in jest with a create-react-app project. I tried using this package to fix it, but had no luck. Can someone shed some light?
I think it's because jest traverses all files that are connected to the test file via import/export, and also all files connected to those files, and so on.
Is there an easy fix for things like this?