Skip to content

Commit

Permalink
format tests why not
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 16, 2023
1 parent 1d6f638 commit 3d828a0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 61 deletions.
16 changes: 9 additions & 7 deletions .prettierignore
@@ -1,7 +1,9 @@
node_modules
tap-snapshots
coverage
.nyc_output
bench-lru
.github
scripts
/node_modules
/tap-snapshots
/coverage
/.nyc_output
/bench-lru
/.github
/scripts
/CHANGELOG.md
/docs
12 changes: 9 additions & 3 deletions test/basic.ts
Expand Up @@ -147,9 +147,15 @@ t.test('setting maxSize with non-integer values', t => {
t.throws(() => new LRU({ max: 10, maxSize: -10 }), TypeError)
t.throws(() => new LRU({ max: 10, maxEntrySize: 10.5 }), TypeError)
t.throws(() => new LRU({ max: 10, maxEntrySize: -10 }), TypeError)
// @ts-expect-error
t.throws(() => new LRU({ max: 10, maxEntrySize: 'banana' }), TypeError)
t.throws(() => new LRU({ max: 10, maxEntrySize: Infinity }), TypeError)
t.throws(
// @ts-expect-error
() => new LRU({ max: 10, maxEntrySize: 'banana' }),
TypeError
)
t.throws(
() => new LRU({ max: 10, maxEntrySize: Infinity }),
TypeError
)
// @ts-expect-error
t.throws(() => new LRU({ max: 10, maxSize: 'banana' }), TypeError)
t.throws(() => new LRU({ max: 10, maxSize: Infinity }), TypeError)
Expand Down
71 changes: 37 additions & 34 deletions test/deprecations.ts
@@ -1,7 +1,7 @@
import t from 'tap'
import LRU from '../'

const warnings:any[] = []
const warnings: any[] = []
process.emitWarning = (...w) => warnings.push(w)

t.test('warns exactly once for a given deprecation', t => {
Expand Down Expand Up @@ -44,39 +44,42 @@ t.test('warns exactly once for a given deprecation', t => {
t.end()
})

t.test('does not do deprecation warning without process object', t => {
// set process to null (emulate a browser)
const proc = global.process
const {error} = console
t.teardown(() => {
global.process = proc
console.error = error
})
const consoleErrors:any[] = []
console.error = (...a) => consoleErrors.push(a)
// @ts-ignore
global.process = {
...proc,
t.test(
'does not do deprecation warning without process object',
t => {
// set process to null (emulate a browser)
const proc = global.process
const { error } = console
t.teardown(() => {
global.process = proc
console.error = error
})
const consoleErrors: any[] = []
console.error = (...a) => consoleErrors.push(a)
// @ts-ignore
emitWarning: null,
}
const LRU = t.mock('../', {})
const c = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: () => 1,
})
c.reset()
t.equal(c.length, 0)
t.equal(c.prune, c.purgeStale)
t.equal(c.reset, c.clear)
t.equal(c.del, c.delete)
global.process = proc
global.process = {
...proc,
// @ts-ignore
emitWarning: null,
}
const LRU = t.mock('../', {})
const c = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: () => 1,
})
c.reset()
t.equal(c.length, 0)
t.equal(c.prune, c.purgeStale)
t.equal(c.reset, c.clear)
t.equal(c.del, c.delete)
global.process = proc

t.strictSame(warnings, [], 'no process exists')
t.matchSnapshot(consoleErrors, 'warnings sent to console.error')
t.strictSame(warnings, [], 'no process exists')
t.matchSnapshot(consoleErrors, 'warnings sent to console.error')

t.end()
})
t.end()
}
)
36 changes: 25 additions & 11 deletions test/fetch.ts
@@ -1,7 +1,7 @@
import t from 'tap'
import LRUCache from '../'
import type { Fetcher } from '../'
import { exposeStatics, expose } from './fixtures/expose'
import LRUCache from '../'
import { expose, exposeStatics } from './fixtures/expose'

const fn: Fetcher<any, any> = async (_, v) =>
new Promise(res =>
Expand Down Expand Up @@ -214,8 +214,9 @@ t.test('fetch options, signal', async t => {

t.test('fetch options, signal, with polyfill', async t => {
const { AbortController, AbortSignal } = global
// @ts-expect-error
t.teardown(() => Object.assign(global, { AbortController, AbortSignal }))
t.teardown(() => {
Object.assign(global, { AbortController, AbortSignal })
})
// @ts-expect-error
global.AbortController = undefined
// @ts-expect-error
Expand Down Expand Up @@ -491,9 +492,15 @@ t.test('fetchContext', async t => {
let expectContext = 'default context'
t.strictSame(await cache.fetch('x'), ['x', 'default context'])
expectContext = 'overridden'
t.strictSame(await cache.fetch('y', { fetchContext: 'overridden' }), ['y', 'overridden'])
t.strictSame(
await cache.fetch('y', { fetchContext: 'overridden' }),
['y', 'overridden']
)
// if still in cache, doesn't call fetchMethod again
t.strictSame(await cache.fetch('x', { fetchContext: 'ignored' }), ['x', 'default context'])
t.strictSame(await cache.fetch('x', { fetchContext: 'ignored' }), [
'x',
'default context',
])
})

t.test('forceRefresh', async t => {
Expand All @@ -502,10 +509,14 @@ t.test('forceRefresh', async t => {
allowStale: true,
ttl: 100,
fetchMethod: async (k, _, { options }) => {
//@ts-expect-error
t.equal(options.forceRefresh, undefined, 'do not expose forceRefresh')
t.equal(
//@ts-expect-error
options.forceRefresh,
undefined,
'do not expose forceRefresh'
)
return k
}
},
})

// put in some values that don't match what fetchMethod returns
Expand All @@ -522,7 +533,10 @@ t.test('forceRefresh', async t => {

cache.set(1, 100)
t.equal(await cache.fetch(1, { allowStale: false }), 100)
t.equal(await cache.fetch(1, { forceRefresh: true, allowStale: false }), 1)
t.equal(
await cache.fetch(1, { forceRefresh: true, allowStale: false }),
1
)
})

t.test('allowStaleOnFetchRejection', async t => {
Expand All @@ -534,7 +548,7 @@ t.test('allowStaleOnFetchRejection', async t => {
fetchMethod: async k => {
if (fetchFail) throw new Error('fetch rejection')
return k
}
},
})
t.equal(await c.fetch(1), 1)
clock.advance(11)
Expand Down
10 changes: 5 additions & 5 deletions test/size-calculation.ts
@@ -1,12 +1,12 @@
import t from 'tap'
import LRU from '../'

const checkSize = (c:LRU<any,any>) => {
const checkSize = (c: LRU<any, any>) => {
const sizes = (c as unknown as { sizes: number[] }).sizes
const {calculatedSize, maxSize} = c
const { calculatedSize, maxSize } = c
const sum = [...sizes].reduce((a, b) => a + b, 0)
if (sum !== calculatedSize) {
console.error({sum, calculatedSize, sizes})
console.error({ sum, calculatedSize, sizes })
throw new Error('calculatedSize does not equal sum of sizes')
}
if (calculatedSize > maxSize) {
Expand Down Expand Up @@ -157,7 +157,7 @@ t.test('large item falls out of cache, sizes are kept correct', t => {
maxSize: 10,
sizeCalculation: () => 100,
})
const sizes:number[] = (c as unknown as { sizes: number[] }).sizes
const sizes: number[] = (c as unknown as { sizes: number[] }).sizes

checkSize(c)
t.equal(c.size, 0)
Expand Down Expand Up @@ -203,7 +203,7 @@ t.test('large item falls out of cache because maxEntrySize', t => {
maxEntrySize: 10,
sizeCalculation: () => 100,
})
const sizes:number[] = (c as unknown as { sizes: number[] }).sizes
const sizes: number[] = (c as unknown as { sizes: number[] }).sizes

checkSize(c)
t.equal(c.size, 0)
Expand Down
5 changes: 4 additions & 1 deletion test/ttl.ts
Expand Up @@ -433,7 +433,10 @@ const runTests = (LRU: typeof LRUCache, t: Tap.Test) => {
t.equal(c.has(1), false)
t.equal(c.get(1), undefined)
t.equal(c.get(1, { allowStale: true }), 1)
t.equal(c.get(1, { allowStale: true, noDeleteOnStaleGet: false }), 1)
t.equal(
c.get(1, { allowStale: true, noDeleteOnStaleGet: false }),
1
)
t.equal(c.get(1, { allowStale: true }), undefined)
t.end()
})
Expand Down

0 comments on commit 3d828a0

Please sign in to comment.