dumbmatter / fakeIndexedDB

A pure JS in-memory implementation of the IndexedDB API
Apache License 2.0
562 stars 69 forks source link

Add observation for Dexie users #75

Open hyphenized opened 1 year ago

hyphenized commented 1 year ago

When using Dexie.js, it caches the global indexedDB variable, so resetting the db state won't work as expected. An easy fix is to manually handle each constructor call to explicitly use a new instance of fakeIndexedDB or add a getter to override the value in your test setup like this:

Object.defineProperty(Dexie.dependencies, "indexedDB", {
  get: () => indexedDB,
})

So far I've tracked this back to 3.0.3 which is the version we're currently using.

dumbmatter commented 1 year ago

Another solution would be to add some non-standard functions to indexedDB that would be useful in testing. Like indexedDB.reset() could delete all the object stores. And other stuff like #50 and #51.

JJosephttg commented 1 year ago

That works, but I agree, we should have an easier way of doing this! I also had a type error with the 'auto' import in my Angular Jasmine project. It was complaining that indexedDB can't be assigned and that it is a getter on window. I managed to get passed that typeerror by doing Object.defineProperty(window, 'indexedDB', { value: indexedDb, writable: true }); BEFORE the import from auto happens. although in retrospect you may or may not need the writable true part, and maybe this solution should also be included in the auto logic as a part of this library

dumbmatter commented 1 year ago

@JJosephttg do you have an example repo that is showing an error for the auto import? I was not aware of that.