Comments (12)
Quick update! I've been trying to debug this issue assuming that we're using the native IndexedDB interface, while lamenting that we're not using the idb
package internally. However, I dug deeper and discovered that yes, we are in fact using the idb
package! Once I figured that out I was able to properly implement this in about 15 minutes lol.
TL;DR: Don't use oncomplete
when you're supposed to be using an async/await interface.
from fakeindexeddb.
from fakeindexeddb.
Can you provide a test case showing the problem?
from fakeindexeddb.
Yes sure,
tx.objectStore("test-key").index("contextLabel").openCursor().onsuccess = function (event) { var cursor = event.target.result; if (cursor) { console.log("From cursor:", cursor.value); cursor.continue(); } };
this is what I am using in my test case, as suggested by your npmjs page. But it is still not covering the lines.
from fakeindexeddb.
Thanks... but how about a complete script I can actually run? I ask because I am skeptical such a core feature is just completely broken. There are a ton of tests and a lot of people using fake-indexeddb. So it's likely some subtle issue specific to precisely how you're using it. Same for #59
from fakeindexeddb.
I'm getting a similar error using the default React testing library (i.e. react-scripts test
on a CRA app). When we initialize a cursor using FDBFactory
it does not seem to run cursors at all. See following:
// In tests before execution
indexedDB = new FDBFactory();
// In implementation
async function Migration(transaction) {
const albumCursor = await transaction
.objectStore('album')
.index('albumId')
.openCursor();
console.log(albumCursor); // FDBCursorWithValue { ... }
if (albumCursor) {
albumCursor.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
console.log('Got a value!'); // Never prints
}
else {
console.log('Finishing up!'); // Never prints
}
}
}
}
I would expect some sort of error to occur if we had set this up wrong but nothing happens.
from fakeindexeddb.
@icorbrey can you provide a self-contained example that I can run to see the problem? Without that it'd be a lot of guesswork for me to try to figure out what you're doing that is triggering a bug. There's already a lot of automated tests covering the basic functionality of cursors, so if there is a bug, I imagine it is some subtle interaction related to the specific way you're using it.
from fakeindexeddb.
Sure. Please note that idb()
is simply getting a reference to our IndexedDB instance, you'll need to graft an existing IDB instance on. Additionally, album
and photo
are both tables in our schema.
describe('The fake-indexeddb cursor', () => {
beforeEach(() => {
indexedDB = new FDBFactory();
});
it('runs', async () => {
const db = await idb();
const cursor = db
.transaction(['album', 'photo'], 'readwrite')
.objectStore('album')
.index('albumId')
.openCursor();
let didRun = false;
cursor.onsuccess = function () {
didRun = true;
console.log('Ran!');
};
expect(didRun).toBeTruthy();
});
});
from fakeindexeddb.
That test will always fail because onsuccess is executed asynchronously.
I added a unit test containing a working version of this code in commit c15b096
You can run it with yarn run build && yarn run test-mocha
. It does indeed work. Closing this issue for now, I don't think there is a bug here, but feel free to provide more information if anyone is still having trouble.
from fakeindexeddb.
Of course it runs asynchronously, but the issue is that it doesn't run. In our tests, on success is never actually called
from fakeindexeddb.
Okay, but like I said above, I tried filling in the blanks in your code in commit c15b096 and it works fine. Maybe there is something else going on that you did not include in your partial code? Not much more I can say without a complete example.
from fakeindexeddb.
Quick update! I've been trying to debug this issue assuming that we're using the native IndexedDB interface, while lamenting that we're not using the
idb
package internally. However, I dug deeper and discovered that yes, we are in fact using theidb
package! Once I figured that out I was able to properly implement this in about 15 minutes lol.TL;DR: Don't use
oncomplete
when you're supposed to be using an async/await interface.
Same thing happened to me when using idb
package. I used below method instead of onsuccess
event and it worked.
const db = await dbPromise(); // opens db
const tx = db.transaction(storeName, 'readonly');
const store = tx.objectStore(storeName);
let cursor = await store.openCursor();
while (cursor) {
// logic
cursor = await cursor.continue();
}
from fakeindexeddb.
Related Issues (20)
- 3.1.6 regression: `ReferenceError: setImmediate is not defined` HOT 1
- orderBy support HOT 4
- initialize the DB before the use. HOT 2
- Add observation for Dexie users HOT 3
- [BUG] Context loosing for `FakeDOMStringList` members while wrapping IDB
- [BUG] Invalid behavior for concurrent insertion data HOT 1
- Ref of Observable from liveQuery is not reactive HOT 7
- fake-indexeddb fails when happy-dom is loaded prior to fake-indexeddb HOT 3
- Running this in Chrome throws "Cannot set property indexedDB of #<Window> which has only a getter" HOT 3
- Release new version to npm with latest types fix HOT 3
- DataCloneError: The data being stored could not be cloned by the internal structured cloning algorithm. HOT 2
- Problem storing and retrieving CryptoKeys HOT 2
- structuredClone and jsdom HOT 7
- `Uint8Array` keys don't respect offsets from underlying ArrayBuffers HOT 3
- Serializing (and restoring) the state of Fake IDB HOT 1
- Unable to use in tests that mock timers (e.g. overriding `globalThis.setImmediate`) HOT 6
- `IDBObjectStore.count` is `O(n^2)` HOT 3
- Try using built-in Node.js Blob HOT 1
- How to read one of the chrome database HOT 1
- A value which is actually Date, but instanceOf Date equals false when use with jest+jsdom. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fakeindexeddb.