dumbmatter / fakeIndexedDB

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

Using for Dexie tests #39

Closed yankeeinlondon closed 4 years ago

yankeeinlondon commented 4 years ago

I'm using a fairly typical mocha/chai testing strategy which uses Typescript code instead of plain JS). I would like to test my Dexie implementation but when I try to open a connection to the database I get the following error:

indexedDB API not found. If using IE10+, make sure to run your code on a server URL (not locally). If using old Safari versions, make sure to include indexedDB polyfill.

To give context, in the unit test file I am adding the following to the top of the file:

import 'fake-indexeddb/auto';

this import reports no issues but the error above persists. I started to wonder if maybe I needed to adjust my tsconfig file but typically the following params do the trick:

{
    // ...
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
}

and those are both in my configuration.

Now while I'm quite comfortable with a lot of Typescript/Javascript things along with named portability between the browser and node, I must say I don't always catch everything when it comes to "global scope" and I'm not yet entirely sure what the /auto script is doing (will be looking soon I guess).

Anyway thought I'd try:

import indexedDB from "fake-indexeddb";
const window: IDictionary = { indexedDB };

This does of course complain about fake-indexeddb not being typed but I got around that by adding a simple declaration file:

declare module "fake-indexeddb";

This removes the Typescript error but as I was suspecting the underlying error is still thrown. As I finish writing this Issue, I am starting to suspect that I need to dig into Dexie a bit more to diagnose the problem but if you've had an exposure to using your library with Dexie I would sure love some help and for that matter I think a small part of the README might talk to integration strategies for the various IDB wrapper libraries (of which Dexie is just one).

yankeeinlondon commented 4 years ago

I'm still digging into how to explicitly set the global scope in Mocha but I see that your example config is using Jest which I know is the coolest flavor these days but my test library is huge and so I don't have the luxury of switch at the moment. Anyway, I do think that a config example for Mocha/Chai would be a good addition considering that this is still a VERY common tooling.

yankeeinlondon commented 4 years ago

It appears that buried deep in the issues of Dexie there appears a clear means to do this:

In some more digging this SO post is probably the best summary:

dumbmatter commented 4 years ago

https://github.com/dumbmatter/fakeIndexedDB/issues/38 shows a couple ways to use it with Dexie. Importing fake-indexeddb/auto is the easiest. If that's not working for you, can you link to an example?

As for how to explicitly set up the global scope in Mocha... I guess it could go in the README. I was somewhat ambivalent about adding it for Jest. But I don't really want to add it for all the testing libraries in the world, especially since it's probably better for people to be looking for this info in their testing library's documentation.

dumbmatter commented 4 years ago

I added explicit instructions for Dexie in the README: https://github.com/dumbmatter/fakeIndexedDB/commit/65fe0f7d14af31689433f77609369163cafcac8e