Description
We are using @sinonjs/fake-timers
and have gotten an issue where items with a ttl
doesn’t expire as we progress the time. It seems like lru-cache
will not use the fake timers unless they are installed before the first require/import of lru-cache
. Since it can be quite hard to control the order of how code is loaded/imported in larger projects this can create some subtle bugs within tests.
Digging into the code I assume the issue stems from lru-cache
saving a reference to performance
or Date
when you load the module the first time and this will reference the real functions if you haven’t installed the fakers yet.
This issue might just be related to our setup where we install and uninstall the fake timers depending on if the test needs them and keeping the servers alive between tests to save time.
Reproduction repository: https://github.com/Milkywire/node-lru-cache-test-issue
A possible solution could be an function that allows you to update or refresh the internal reference something like #refreshTTLTimerRefrence
that just reruns the code to find the correct timer.
#refreshTTLTimerReference() {
perf = typeof performance === 'object' &&
performance &&
typeof performance.now === 'function'
? performance
: Date;
}
This could then be part of unsafeExposeInternals
and used if needed.
What do you think?