dumbmatter / fakeIndexedDB

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

MissingAPIError: IndexedDB API missing #63

Closed staff0rd closed 3 years ago

staff0rd commented 3 years ago

I tried using with latest dexie inside a CRA, but it doesn't seem to work:

import Dexie, { DexieOptions } from "dexie";
import indexedDB from "fake-indexeddb";
import IDBKeyRange from "fake-indexeddb/lib/FDBKeyRange";

interface IMessage {
  id?: number;
  message: string;
}
class TestDatabase extends Dexie {
  messages: Dexie.Table<IMessage, number>;

  constructor(options?: DexieOptions) {
    super("Database", options);
    this.version(1).stores({
      messages: "++id",
    });
    this.messages = this.table("messages");
  }
}

test("should pass", async () => {
  const db = new TestDatabase({
    indexedDB: indexedDB,
    IDBKeyRange: IDBKeyRange,
  });
  const result = await db.messages.put({ message: "a message" });
  console.warn(result);
  expect(result).not.toBeUndefined();
});

Fails with:

FAIL  src/db.test.ts
✕ should pass (22 ms)

● should pass

  MissingAPIError: IndexedDB API missing. Please visit https://tinyurl.com/y2uuvskb

    at cmp (node_modules/dexie/src/functions/cmp.ts:9:25)
        at Array.forEach (<anonymous>)
    From previous:
    From previous:

repro

dumbmatter commented 3 years ago

I cloned the repo, ran npm ci, and the npm test. This is with Node v16.1.0:

 PASS  src/App.test.tsx
 FAIL  src/db.test.ts
  ● Test suite failed to run

    "messageParent" can only be used inside a worker

      at messageParent (node_modules/jest-worker/build/workers/messageParent.js:46:11)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.056 s
Ran all test suites.

Watch Usage: Press w to show more.

Not sure why it's a different error than yours. Regardless, I think it's likely that your error message is not actually related to fake-indexeddb and is probably instead related to either Dexie or Jest. See the README for some examples that work with either Dexie or Jest.

staff0rd commented 3 years ago

Oddly I was getting that too but not always, I hit enter during watch when it happened and it ran the test with the original error.

The code in the repo (see the commit linked) is based on the readme.

On 13 May 2021, at 3:00 am, Jeremy Scheff @.***> wrote:

 I cloned the repo, ran npm ci, and the npm test. This is with Node v16.1.0:

PASS src/App.test.tsx FAIL src/db.test.ts ● Test suite failed to run

"messageParent" can only be used inside a worker

  at messageParent (node_modules/jest-worker/build/workers/messageParent.js:46:11)

Test Suites: 1 failed, 1 passed, 2 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.056 s Ran all test suites.

Watch Usage: Press w to show more. Not sure why it's a different error than yours. Regardless, I think it's likely that your error message is not actually related to fake-indexeddb and is probably instead related to either Dexie or Jest. See the README for some examples that work with either Dexie or Jest.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

muttli commented 3 years ago

@staff0rd did you find a solution, or a cause, since you closed this issue?

staff0rd commented 3 years ago

If I remember correctly, I did not find the direct cause, but I worked around my need for the library by mocking my data access functions instead of Dexie/indexedDb.