Skip to content

Commit

Permalink
fix tests on node 14
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 22, 2023
1 parent a568b5d commit 9cdb1cb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -18,7 +18,8 @@ const AC = hasAbortController
this.signal = new AS()
}
abort(reason = new Error('This operation was aborted')) {
this.signal.reason = reason
this.signal.reason = this.signal.reason || reason
this.signal.aborted = true
this.signal.dispatchEvent({
type: 'abort',
target: this.signal,
Expand Down
8 changes: 6 additions & 2 deletions test/fetch.ts
@@ -1,3 +1,6 @@
if (typeof performance === 'undefined') {
global.performance = require('perf_hooks').performance
}
import t from 'tap'
import type { Fetcher } from '../'
import LRUCache from '../'
Expand Down Expand Up @@ -754,17 +757,18 @@ t.test('background update on timeout, return stale', async t => {
})
},
})
const e = expose(c)
c.set(1, 10)
clock.advance(100)
const ac = new AbortController()
const p = c.fetch(1, { signal: ac.signal })
const e = expose(c)
await new Promise(res => setImmediate(res))
t.match(e.valList[0], { __staleWhileFetching: 10 })
ac.abort(new Error('gimme the stale value'))
t.equal(await p, 10)
t.equal(c.get(1, { allowStale: true }), 10)
clock.advance(200)
await new Promise(res => setImmediate(res))
await new Promise(res => setImmediate(res)).then(() => {})
t.equal(e.valList[0], 1, 'got updated value later')

c.set(1, 99)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/expose.ts
Expand Up @@ -7,6 +7,7 @@ export const exposeStatics = (LRU: typeof LRUCache) => {
}
export const expose = (cache: LRUCache<any, any>) => {
return cache as unknown as {
isStale: (n: number) => boolean
isBackgroundFetch: (v: any) => boolean
backgroundFetch: (
v: any,
Expand Down

0 comments on commit 9cdb1cb

Please sign in to comment.