This issue is due to Dexie expecting window.indexedDB
to be defined, this is not the case when running in a headless mode (using Jest) that does not have a true DOM or window
scope.
Found a solution deep in the Dexie git issues which suggests:
const Dexie = require('dexie')
Dexie.dependencies.indexedDB = require('fake-indexeddb')
Dexie.dependencies.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange')
We have also had success with:
import Dexie from 'dexie';
import indexedDB from 'fake-indexeddb';
Dexie.dependencies.indexedDB = indexedDB;
Link to the original issue:
https://github.com/dfahlander/Dexie.js/issues/495
Or according to the documentation, you can provide the indexedDB option like:
import Dexie from 'dexie';
import indexedDB from 'fake-indexeddb';
var db = new Dexie("MyDatabase", { indexedDB: indexedDB });
Link to documentation: http://dexie.org/docs/Dexie/Dexie
FDBKeyRange
dependency (versus just theindexedDB
? – Counterchange