From 9cdb1cbf02b3e71b492c13e62399734db9ad52fd Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 21 Feb 2023 17:14:11 -0800 Subject: [PATCH] fix tests on node 14 --- index.js | 3 ++- test/fetch.ts | 8 ++++++-- test/fixtures/expose.ts | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 8a4c2300..7da9daef 100644 --- a/index.js +++ b/index.js @@ -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, diff --git a/test/fetch.ts b/test/fetch.ts index 72ef0d9f..01b44ba2 100644 --- a/test/fetch.ts +++ b/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 '../' @@ -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) diff --git a/test/fixtures/expose.ts b/test/fixtures/expose.ts index f66e4b6d..b26b101d 100644 --- a/test/fixtures/expose.ts +++ b/test/fixtures/expose.ts @@ -7,6 +7,7 @@ export const exposeStatics = (LRU: typeof LRUCache) => { } export const expose = (cache: LRUCache) => { return cache as unknown as { + isStale: (n: number) => boolean isBackgroundFetch: (v: any) => boolean backgroundFetch: ( v: any,