dumbmatter / fakeIndexedDB

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

Unable to cover onError scenarios while testing #59

Closed BrinsterAman closed 3 years ago

BrinsterAman commented 3 years ago

For our indexedDB usage in app, we have written graceful error handling for indexedDB failure, ex - transaction.onerror, .onerror handler for indexedDB.open() function, .error for read write db. These error handlers are not getting covered while using fake-indexedDB. Can you suggest how to cover them, we are using React testing library with Jest.

dumbmatter commented 3 years ago

Can you provide a test case showing the problem?

BrinsterAman commented 3 years ago

Sure but being proprietary code I cant reveal all of them, still pasting a part of it which will give you a fair idea.

sessionStorage.fromStore = "test-tab"; let request = indexedDB.open("test-tab", 3); request.onerror = function (event) { console.log("Error in performing db operations"); };

these lines of code are followed by render method and some expects.

mmnoo commented 3 years ago

Im also trying to solve this issue and am not finding anything helpful in documentation on simulating errors for tests. Im using Dexie, and have a test utility that is feeling too hacky for my liking (and I am pretty new to indexedDB, so am not confident the utility wouldnt produce false positives or false negatives). Would be nice to have something explicit to rely on for error scenarios.

const getMockDexieInstanceThatProducesErrors = () => {
  // produces an error in all cases so far because there are no stores defined
  // may need to explore better way to simulate indexeddb error
  return new Dexie('error', {
    indexedDB: fakeIndexedDB,
  })
}
dumbmatter commented 3 years ago

Currently there's nothing special beyond what's in the IndexedDB API. If you want to simulate an error, do a database operation that causes an error. If that's not sufficient, you'll have to give me a bit more detail to explain what you're trying to do. If your question is actually about Dexie (as in, you have the same problem whether you're using real IndexedDB or fake-indexeddb), then you should ask there, because they'll be able to answer it better than me.

mmnoo commented 3 years ago

Im just trying to implement configuration for a fake indexedDB for testing that will return an error no matter what operation is performed. So that I dont have to manually mock every little operation.

This seems to do the trick for the way I am using indexedDB. Dexie errors because there are no stores defined, but it falls back on erroring because the IndexedDb API is undefined.

Sorry, Im in the process of learning (or avoiding learning) indexedDB, so separating what is Dexie and what is indexedDB a bit challenging. Thanks for providing your library so there is one less thing I have to do!

const getMockDexieInstanceThatProducesErrors = () => {
  return new Dexie('error', {
    indexedDB: undefined,
  })
}