Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with Jest and Fake Indexed DB #105

Closed
abhi747 opened this issue Mar 2, 2021 · 3 comments
Closed

Testing with Jest and Fake Indexed DB #105

abhi747 opened this issue Mar 2, 2021 · 3 comments

Comments

@abhi747
Copy link

abhi747 commented Mar 2, 2021

This is not an issue but a query.

I need to write unit tests for some functionalities with regards to idb-keyval library that I have integrated.

I am using jest and fake-indexeddb packages for testing.

As per my knowledge, idb-keyval doesn't use concept of indexes. Correct me if wrong.

But for unit testing, I need to setup indexed DB with Fake Indexed DB library, which requires setting up an index with the createIndex method.

How do I simulate same data format stored by idb-keyval with the Fake Indexed DB library, any idea?

@jakearchibald
Copy link
Owner

As per my knowledge, idb-keyval doesn't use concept of indexes.

Not sure what you mean? It doesn't add anything IDB would consider an index.

But for unit testing, I need to setup indexed DB with Fake Indexed DB library, which requires setting up an index with the createIndex method.

Why does it need an index?

(closing, but I'll continue replying)

@abhi747
Copy link
Author

abhi747 commented Mar 2, 2021

Not sure what you mean? It doesn't add anything IDB would consider an index.

Thanks for the quick response. Yes, I meant, that we don't have to set up an index explicitly when working with idb-keyval.

Why does it need an index?

Yes, you are right, index is not mandatory, but when I remove index and add keypath for Fake Indexed DB as follow, something seems wrong in setting up the DB as compared to DB set by idb-keyval, and as a result, my unit test is failing.

const store = db.createObjectStore('keyval', {
          keyPath: 'timeStamp', // 'timeStamp' is used as key while adding records to DB with idb-keyval. So setting it as keyPath here.
        });

The DB setup in the browser is as below. Does anything seem wrong with my approach?

image

@jakearchibald
Copy link
Owner

The stuff in https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md might help, but once you need to do more complex stuff with the database, you might be better off with idb-keyval's sister library: https://www.npmjs.com/package/idb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants