fakeIndexedDB
fakeIndexedDB copied to clipboard
Unable to use in tests that mock timers (e.g. overriding `globalThis.setImmediate`)
https://github.com/dumbmatter/fakeIndexedDB/blob/4c01ec36ed2500842ad17fb606a2c6b435349ec3/src/lib/scheduling.ts#L20-L29
The code persists a reference to globalThis.setImmediate
(which is always defined in our test setup). We have other logic (custom logic, but conceptually similar to jest's fake timers: https://jestjs.io/docs/timer-mocks) that will replace globalThis.setImmediate
to give tests more control over timing. However, fake-indexeddb
will continue to use the original setImmediate
and thus break our testing logic.
I can solve this in our setup by patching the code with something akin to
export const queueTask = (fn: () => void): void => {
const setImmediate = globalThis.setImmediate ||
getSetImmediateFromJsdom() ||
((fn: () => void) => setTimeout(fn, 0));
setImmediate(fn);
}
so it's respecting the current value of setImmediate
rather than keeping a cached reference.
I'll go ahead and create a PR with that concept as a starting point for discussion, but let me know if it's flawed in some way (e.g. perhaps it's not performant enough).