dumbmatter / fakeIndexedDB

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

[BUG] Context loosing for `FakeDOMStringList` members while wrapping IDB #76

Closed vitonsky closed 1 year ago

vitonsky commented 1 year ago

I have a critical bug that block me to write tests for IDB.

I use:

When i run test https://github.com/translate-tools/linguist/blob/7ac3f7766dbf2be3002bd489ccfd4d069ad37c2f/src/requests/backend/translations/data.test.ts i got error (logs included).

I've investigated this problem, cause in file https://github.com/dumbmatter/fakeIndexedDB/blob/master/src/lib/FakeDOMStringList.ts

A FakeDOMStringList methods lost context of this class, when idb bind another context here:

https://github.com/jakearchibald/idb/blob/303fa24af0faba002b2bed7060193f946b02e6dc/build/esm/wrap-idb-value.js#L150

It been fixed in older versions of idb as i see, because version 7.1.1 works fine for me, but i have 2 projects required idb version 6.1.2, so i can't test indexedDB there.

I suggest to refactor methods in this class to use arrow functions to keep context. I've tested it on the fly and it works fine.

Error log ``` FAIL src/requests/backend/translations/data.test.ts ✕ translations data handler (8 ms) ● translations data handler TypeError: this.objectStoreNames._push is not a function 14 | apply: (db) => { 15 | // Create store > 16 | db.createObjectStore('translations', { | ^ 17 | keyPath: 'id', 18 | autoIncrement: true, 19 | }); at FDBDatabase.Object..FDBDatabase.createObjectStore (node_modules/fake-indexeddb/build/cjs/FDBDatabase.js:124:27) at Proxy. (node_modules/idb/build/esm/wrap-idb-value.js:150:26) at Object.apply (src/requests/backend/translations/idb/schema/v1.ts:16:6) at src/lib/idb/manager.ts:71:18 at step (src/lib/idb/manager.ts:33:23) at Object.next (src/lib/idb/manager.ts:14:53) at src/lib/idb/manager.ts:8:71 at Object..__awaiter (src/lib/idb/manager.ts:4:12) at src/lib/idb/manager.ts:29:60 at FDBOpenDBRequest. (node_modules/idb/build/esm/index.js:16:13) at invokeEventListeners (node_modules/fake-indexeddb/build/cjs/lib/FakeEventTarget.js:26:23) at FDBOpenDBRequest.Object..FakeEventTarget.dispatchEvent (node_modules/fake-indexeddb/build/cjs/lib/FakeEventTarget.js:99:7) at waitForOthersClosed (node_modules/fake-indexeddb/build/cjs/FDBFactory.js:145:13) at runVersionchangeTransaction (node_modules/fake-indexeddb/build/cjs/FDBFactory.js:171:3) at openDatabase (node_modules/fake-indexeddb/build/cjs/FDBFactory.js:194:5) at Immediate. (node_modules/fake-indexeddb/build/cjs/FDBFactory.js:261:7) ● translations data handler AbortError: AbortError at FDBTransaction.error (node_modules/idb/build/esm/wrap-idb-value.js:77:32) at invokeEventListeners (node_modules/fake-indexeddb/build/cjs/lib/FakeEventTarget.js:26:23) at FDBTransaction.Object..FakeEventTarget.dispatchEvent (node_modules/fake-indexeddb/build/cjs/lib/FakeEventTarget.js:99:7) at Immediate. (node_modules/fake-indexeddb/build/cjs/FDBTransaction.js:82:12) ● translations data handler AbortError: at node_modules/fake-indexeddb/build/cjs/FDBFactory.js:265:27 at node_modules/fake-indexeddb/build/cjs/FDBFactory.js:198:16 at Immediate. (node_modules/fake-indexeddb/build/cjs/FDBFactory.js:154:9) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 3.429 s Ran all test suites. ```